Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
void readInfo()
{
    struct grades *grades=malloc(3*sizeof(grades));
    char *line,*str;int i=0;
    FILE *rf=fopen("Grades.txt","r");
    if(rf!=NULL)
    {
        puts("inside if1");
        while(i<3&&fgets(line,60,rf)!=NULL)
        {
            puts("inside while");
            fscanf(rf,"%d %s %c",&((*(grades+i)).ID),str,&((*(grades+i)).grade));i++;
            strcpy(str,((*(grades+i)).course));
            printInfo(grades+i);
        }
    }
}


What I have tried:

Suddenly fgets stoped working while i haven't done any change that will affect the function
here is my code .please let me know why it is not working !?
Posted
Updated 13-Mar-21 6:02am
v3
Comments
Patrice T 6-Dec-16 15:42pm    
If the code didn't changed, may be it is the Grades.txt file.

Quote:
Fgets doesn't work - it seems to return a null
If the code didn't changed, may be it is the Grades.txt file.

To make sure, use the debugger.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
You make a call to fgets which reads the next line from the file. You then call fscanf; are you sure you want to read two lines each time?
Also all those complex dereferencing expressions are difficult to interpret.
 
Share this answer
 
To continue with what Richard wrote, you should not call fscanf if you also call fgets. You should use sscanf on the buffer you pass to fgets. Speaking of which, in the code, line and str are declared as character pointers but are not assigned to anything. That is going to result in a crash.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900