0

我是 C 的新手。我的文件被读取的方式几乎没有问题。使用 valgrind,我有“使用大小为 8 的未初始化值”和 while 循环中的条件跳转。请帮助我了解我哪里出错了。谢谢你。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHAR 30
#define LINESIZE 128
int main()
{
    char fileName[30];
    char courseName[30];
    printf("Please enter the file name: ");
    scanf("%s", fileName);
    //printf("%s is the fileName", fileName);

    FILE *fp;
    if((fp=fopen(fileName,"r"))==NULL)
    {
        printf("File does not exsists\n");
        exit(1);
    }
    int lines;
    char ch;
    while((ch=fgetc(fp))!=EOF)
    {
        if(ch=='\n')
            lines++;
    }
    //fclose(fp);
    int i=0;
    char courses[lines][30];


  rewind(fp);
    while (fgets( courses[i], MAXCHAR, fp) != NULL)
    {
        //printf("At line %d we have %s", i, courses[i]);
        i++;
    }
    fclose(fp);
}

我得到的错误

==1057== Use of uninitialised value of size 8
==1057==    at 0x108A81: main (course_graph.c:33)
==1057== 
==1057== Use of uninitialised value of size 8
==1057==    at 0x4EC43B0: rewind (rewind.c:32)
==1057==    by 0x522D8BF: ???
==1057==    by 0x4E5DB96: (below main) (libc-start.c:310)
==1057== 
==1057== Use of uninitialised value of size 8
==1057==    at 0x4EC441F: rewind (rewind.c:35)
==1057==    by 0x1FFEFFFF5F: ???
==1057==    by 0x4E5DB96: (below main) (libc-start.c:310)
4

0 回答 0