我有2d char数组。我存储了传递给我自己的shell的20个命令的历史记录。但我遇到了麻烦。当我使用CodeBlocks在Windows上编译它时一切都很顺利
MAXHIS = 20 commandTable是[20] [100]:
void addHistory(char *command) //add command to 2d table
{
int i;
char *tempCommand = command;
if(tempHis < MAXHIS)
{
strcpy(commandTable[tempHis], tempCommand);
tempHis++;
}
else
for(i = 0; i < MAXHIS; i++)
{
if(i<MAXHIS-1)
{
strcpy(commandTable[i], commandTable[i+1]);
}
else
{
strcpy(commandTable[i], tempCommand);
}
}
}
void loadHistory() //load history from file
{
FILE *file= fopen(filename, "r");
int fileEnd = 0;
int i, j;
char c;
while(1)
{
if((c = fgetc(file)) != EOF)
{
if(c == '\n')
{
i++;
j = 0;
tempHis++;
}
commandTable[i][j] = c;
j++;
}
else
{
fileEnd = 1;
}
if (fileEnd == 1)
{
break;
}
}
}
void printHistory(void) //display history on the shell
{
int i;
printf("\n");
for(i = 0; i < tempHis; i++)
printf("Command %d: %s \n",i+1, commandTable[i]);
}
void writeHistory(FILE *file) //write history to the file
{
int i, j;
for(i = 0; i < tempHis; i++)
{
for(j = 0; j < MAX; j++)
{
fprintf(file, "%c", commandTable[i][j]);
}
fprintf(file, "\n");
}
fclose(file);
}
和int Main:
int main(void) {
setFilepath();
FILE *historyFile= fopen(filename, "r+");
if (signal(SIGQUIT, sigHandler) == SIG_ERR)
printf("SIGQUIT ERROR");
loadHistory();
//some code here
return 0;
}
用于操纵我的历史记录功能的命令:
int lineInMenu(char* line, char** argsList, FILE *file)
{
if(strcmp(line, "exit")==0)
{
writeHistory(file);
exit(0);
}
else if(strcmp(line, "history") ==0)
{
printHistory();
return 1;
}
else if(strcmp(argsList[0],"cd")==0)
{
chdir(argsList[1]);
return 1;
}
else return 0;
}
在我第一次关闭shell之前,一切都很好(使用&#34;打印历史记录&#34;)它看起来像这样:
Command 1: ls
Command 2: ls
Command 3: history
当我再次编译shell(来自文件的loadHistory())时,我得到一些空的空白,它看起来像:
Command 1: ls
Command 2:
Command 3:
ls
Command 4:
history
Command 5 : history
当我再试一次时,我会得到更多的空白空间。有人可以检查我的代码告诉我,我做错了什么?
如果它有帮助,.txt文件看起来像(当我第一次关闭shell时):
ls\00\00\00\.....
\00\00\00\00...
\00\00\00\00..
history\00\00\..
\00\00\00\..
\00\00\00..
最好的嘘声
答案 0 :(得分:0)
函数:loadHistory()
fopen()
fgetc()
int
和char
警告:我还没有检查过其他功能:
以下提议的代码: 纠正了函数的明显问题:
建议使用类似于:
的内容#include <stdio.h> // fopen(), fgetc(), FILE, EOF
#include <string.h> // memset()
#include <stdlib.h> // EXIT_FAILURE EXIT_SUCCESS
#define MAX_ROWS 20
#define MAX_COLS 80
// prototypes
int loadHistory( char * );
extern int tempHis;
extern char commandTable[ MAX_ROWS ][ MAX_COLS ];
int loadHistory( char *filename ) //load history from file
{
FILE *file= fopen(filename, "r");
if( !file )
{
perror( "fopen for history file failed" );
return EXIT_FAILURE;
}
// implied else, fopen successful
int row = 0;
int column = 0;
int c;
tempHis = 0;
memset( &commandTable, '\0', sizeof( commandTable ) );
while( (c = fgetc(file)) != EOF )
{
commandTable[ row ][ column ] = (char)c;
column++;
if( column >= MAX_COLS );
{
fclose( file );
return EXIT_FAILURE;
}
if(c == '\n')
{
row++;
if( row >= MAX_ROWS )
{
fclose( file );
return EXIT_FAILURE;
}
column = 0;
tempHis++;
}
}
fclose( file );
return EXIT_SUCCESS;
}
答案 1 :(得分:0)
几天前问题解决了。我刚刚在“LoadHistory”方法中添加了“Continue”(如果在ifi的末尾if(c == \ n),那么)