C 存储和消除垃圾值

C 存储和消除垃圾值,c,arrays,store,C,Arrays,Store,每次我尝试运行代码时,它都会打印出文件的内容,但最后它会打印出一个垃圾值,我不知道如何清除它。我应该将文件的内容存储到一个数组中,但是我对如何实现这一点有点困惑 #include <stdio.h> #include <stdlib.h> #include <string.h> char filePrinter(char*arr) int main (int argc, char**argv) { char fileArray[150];

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

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

由于
EOF
发生在
fgetc
中,因此计时比通过
feof
查看循环的开头更糟糕

取代

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


feof
如果对读取操作的最后一次调用达到EOF,则返回true。您希望在调用
fgetc
后对其进行测试。或者,更好的是,只需检查
fgetc
是否返回了特殊值
EOF


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

我认为代码应该是:

#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;
}
#包括
#包括
#包括
作废文件打印机(char*arr);
int main(int argc,字符**argv)
{
char文件数组[150];
memset(fileArray,0,sizeof(fileArray));
如果(argc!=2)
{
printf(“无效输入。请输入程序名称,后跟输入文件名\n”);
}
文件打印机(文件阵列);
返回0;
}
无效文件打印机(char*arr)
{
int c=0,j=0;
FILE*FILE=NULL;
file=fopen(“assests/room.txt”、“r”);
if(file==NULL)
{
printf(“无法打开文件\n”);
出口(-1);
}
其他的
{
而(1)
{
c=fgetc(文件);
如果(c!=EOF)
{
arr[j++]=c;
}
其他的
{
打破
}
}
}
fclose(文件);
返回;
}

非常感谢您!它起作用了!但是,如何将文件内容存储到数组中。“我对此有点困惑。”user3380932添加了示例。它让我一直在给我分段fault@user3380932在我尝试过的地方没有问题。一定是另外有什么问题。它存储数组,但同时打印垃圾值end@user3380932添加“memset(fileArray,0,sizeof(fileArray))
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;
}
#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;
}