【问题标题】:Store and eliminating garbage values存储和消除垃圾值
【发布时间】:2014-04-08 09:52:24
【问题描述】:

每次我尝试运行代码时,它都会打印出文件的内容,但是它会在最后打印出一个我不知道如何摆脱的垃圾值。我应该将文件的内容存储到一个数组中,但是我对如何做到这一点有点困惑???

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char filePrinter(char*arr)

int main (int argc, char**argv)
{
    char fileArray[150];

    if(argc !=2)
    {
        printf("Invalid Entry. Please Enter name of program followed by input filename\n");
    }

    filePrinter(fileArray);

    return 0;
}

char filePrinter(char*arr)
{
    int i;
    FILE*file;
    i=0;

    file=fopen("assests/room.txt","r");
    if(file == NULL)
    {
        printf("Could not open file\n");
        exit(-1);
    }
    else
    {
        while(0 ==feof(file))
        {
            i=fgetc(file);
            printf("%c", i);
        }
    }
    fclose(file);
    return i;
}

文件内容:

10x16 ds5 h6,5 g7,8 p3,3
10X16 de4 h5,7 g9,2
10X16 dw6,h2,3 m6,7
10X16 dn3,h2,4 p2,3
10X16 de2 h9,9 m4,5
10X16 dn8 h4,5 g1,1*/

【问题讨论】:

    标签: c arrays store


    【解决方案1】:

    我认为代码应该是:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void filePrinter(char*arr);  
    
    int main (int argc, char**argv)
    {
        char fileArray[150];
        memset(fileArray, 0, sizeof(fileArray));
    
        if(argc !=2)
        {
            printf("Invalid Entry. Please Enter name of program followed by input filename\n");
        }
    
        filePrinter(fileArray);
    
        return 0;
    }
    
    void filePrinter(char *arr)
    {
        int c = 0, j = 0;
        FILE* file = NULL;
    
        file=fopen("assests/room.txt","r");
        if(file == NULL)
        {
            printf("Could not open file\n");
            exit(-1);
        }
        else
        {
            while (1)
            {
                c = fgetc(file);
                if (c != EOF)
                {
                      arr[j++] = c;
                }
                else
                {
                    break;
                }
            }
        }
        fclose(file);
        return;
    }
    

    【讨论】:

    • 它存储数组,但它在最后打印出垃圾值
    • @user3380932 添加“memset(fileArray, 0, sizeof(fileArray));”
    【解决方案2】:

    时间比通过feof 查看循环的开头更糟糕,因为EOF 出现在fgetc 中。

    替换为

    while(EOF!=(i=fgetc(file))){
        printf("%c", i);
    }
    

    int filePrinter(char*arr){
        int i = 0, ch;
        FILE*file;
    
        file=fopen("assests/room.txt","r");
        if(file == NULL) {
            printf("Could not open file\n");
            exit(-1);
        } else {
            while(EOF!=(ch=fgetc(file))) {
                //printf("%c", ch);
                arr[i] = ch; //*arr++ = ch;
                ++i;//i : range check
            }
            arr[i] = '\0';
        }
        fclose(file);
        return i;
    }
    

    【讨论】:

    • 非常感谢!有用 !但是,如何将文件内容存储到数组中。我对此有点困惑。
    • @user3380932 添加了示例。
    • 它让我一直给我分段错误
    • @user3380932 我试过的地方都没有问题。一定是单独有什么问题。
    【解决方案3】:

    feof 如果 last 调用读取操作命中 EOF,则返回 true。您想在fgetc 调用之后对其进行测试。或者,更好的是,只需检查 fgetc 是否返回特殊值 EOF

    (A FILE * 有一个“文件结束标记”,表示某些读取操作是否已到达 EOF。读取操作在到达 EOF 时设置“文件结束标记”。在您点击之前- --意思是试图读过去---文件的结尾,那个“文件结尾标记”是清楚的。)

    【讨论】:

      猜你喜欢
      • 2015-05-24
      • 2021-12-12
      • 1970-01-01
      • 2021-10-07
      • 2017-02-01
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      相关资源
      最近更新 更多