Click here to Skip to main content
15,881,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i read from files in c without repeating the first character in reading ?

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char course_name[20];
    int number_of_student;
    double courses_average_grade;
    int i;
    FILE *in;

    in=fopen("course_info.txt","r");

    for(i=0;i<20;i++) {
        fscanf(in,"%s %d %lf",&course_name[i],&number_of_student,&courses_average_grade);
        printf("%s \t %d \t %.2f \n",course_name,number_of_student,courses_average_grade);
    }

    return 0;
}
Posted
Updated 21-May-17 1:47am
v3
Comments
[no name] 21-May-17 7:12am    
"without repeating the first character in reading ", that depends on what on earth you are talking about.
OriginalGriff 21-May-17 7:14am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work from. And without your input file we can't have any idea what you are getting as output! So edit your question, show us a few lines of sample input data, and add sample output - explaining exactly what you didn't expect to get!
Use the "Improve question" widget to edit your question and provide better information.
OriginalGriff 21-May-17 7:16am    
Oh, and BTW: pick a bracketing style and STICK TO IT.
Changing from K&R to "hide the brackets and don't indent the code" doesn't make even trivial code samples like that easy to read.

I think your fscanf() statement should be without [i]:
fscanf(in,"%s %d %lf",&course_name,&number_of_student,&courses_average_grade);
 
Share this answer
 
course_name and &course_name[i] are the same only if i is zero!!!
So write in fscanf also course_name only...
 
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