c - 我从 C - Linux 中的 .txt 文件中得到错误的输出

标签 c linux file

我有二维字符数组。我存储了传递到我自己的 shell 的 20 个命令的历史记录。但我有麻烦了。当我在 Windows 上使用 CodeBlocks 编译它时,一切正常

马克斯 = 20 命令表是 [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 之前,一切都很好(使用“print History”),它看起来像这样:

Command 1: ls
Command 2: ls
Command 3: history

当我再次编译 shell(从文件中加载历史记录())时,我得到了一些空白,它看起来像:

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..

祝愿

最佳答案

函数:loadHistory()

  1. 包含一些逻辑错误,比如'\n'放在哪里
  2. 无法检查对 fopen() 的调用状态
  3. 使用无意义的变量名
  4. 初始化变量失败
  5. 未能让此函数的调用者知道函数是否成功
  6. 未能正确利用 fgetc() 返回的状态
  7. intchar 之间进行无效比较
  8. 缺少一些需要的括号

警告:我还没有检查其他功能:

以下建议代码: 更正了函数的明显问题:

建议使用类似于:

#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;
}

关于c - 我从 C - Linux 中的 .txt 文件中得到错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49985730/

相关文章:

C:子范围内的反向数组

file - JVM 属性 -Dfile.encoding=UTF8 还是 UTF-8?

c - 使用套接字 API 的基于轮询的客户端服务器

使用talloc时编译错误

linux - 在 docker 容器上持久化/etc/passwd

linux - 为什么 freebsd 上的 libstdc++ 不需要 libc.so 的链接?

c - 使用 C 在文本文件中返回随机行的最佳方法是什么?

c - 警告从不兼容的指针类型传递参数 1 of ‘fopen’

c - 无法解析代码中的结构内容

php - php执行时scp主机 key 验证失败