Home › Forums › C Programming › Help – file reading
- This topic has 7 replies, 3 voices, and was last updated 15 years, 9 months ago by msaqib.
- AuthorPosts
- March 21, 2009 at 2:07 pm #2182Ernesto0751Participant
hi,
can someone give me the idea on how to read the following file (student’s mark text file) using array and then print it out, how would be the design of the code?
No Matrix quiz1(5%) quiz1(5%) quiz1(5%) Proj(25%) MidT
1 BK20008 4 3 5 18 18
2 BK20002 5 5 4 10 8
3 BK20003 3 4 5 18 10
4 BK20006 4 3 4 20 14
5 BK20005 5 4 3 13 17
6 BK20004 2 5 2 4 16
7 BK20007 3 4 4 21 14
8 BK20001 4 3 5 11 15
9 BK20009 3 2 4 10 10
10 BK20010 4 3 3 23 18i’m not asking for code because this is my project. After reading the file, the program have to calculate the grade and sort them, but first i have to know how to read this file
any kindly help i will be very appreciate!
thanks in advance - March 21, 2009 at 10:38 pm #3529JonathaThurstonParticipant
Are you using C or C++? If its C the following can help.
123<br />fscanf(fileName, "%d [] %d %d %d %d %d", variableName, arrayName, variableName ...);<br /> - March 22, 2009 at 12:23 pm #3530Ernesto0751Participant1234567891011121314151617181920212223242526272829303132333435363738394041424344int main ()<br />{<br />char matrix[20][200];<br />int i, j, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20];<br />FILE *infile;<br /><br />infile = fopen("studentsmark.txt","r");<br /><br />i=0;<br /><br />while( fscanf(infile,"%d %s %d %d %d %d %d",&no<i>, matrix</i><i>, &quiz_1</i><i>, &quiz_2</i><i>, &quiz_3</i><i>, &project</i><i>, &midT</i><i>) !=EOF) i++;<br /><br /><br /><br />for(j=0;j<i;j++)<br />{<br />printf("%d %s %d %d %d %d %dn",no[j], matrix[j], quiz_1[j], quiz_2[j], quiz_3[j], project[j], midT[j]);<br />}<br /><br />fclose(infile);<br />}#include <stdio.h><br /><br /><br />int main ()<br />{<br />char matrix[20][200];<br />int i, j, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20];<br />FILE *infile;<br /><br />infile = fopen("studentsmark.txt","r");<br /><br />i=0;<br /><br />while( fscanf(infile,"%d %s %d %d %d %d %d",&no</i><i>, matrix</i><i>, &quiz_1</i><i>, &quiz_2</i><i>, &quiz_3</i><i>, &project</i><i>, &midT</i><i>) !=EOF) i++;<br /><br /><br /><br />for(j=0;j<i;j++)<br />{<br />printf("%d %s %d %d %d %d %dn",no[j], matrix[j], quiz_1[j], quiz_2[j], quiz_3[j], project[j], midT[j]);<br />}<br /><br />fclose(infile);<br />}</i>
but i am unable to read tis line
No Matrix quiz1(5%) quiz1(5%) quiz1(5%) Proj(25%) MidT(20%)what should i add to make the program read it and ignore it so it can read on the real data i need, i really have no idea…
- March 22, 2009 at 1:00 pm #3531Ernesto0751Participant1234567891011121314151617181920212223242526272829303132333435363738394041424344int main ()<br />{<br />char matrix[20][200];<br />int i, j, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20];<br />FILE *infile;<br /><br />infile = fopen("studentsmark.txt","r");<br /><br />i=0;<br /><br />while( fscanf(infile,"%d %s %d %d %d %d %d",&no<i>, matrix</i><i>, &quiz_1</i><i>, &quiz_2</i><i>, &quiz_3</i><i>, &project</i><i>, &midT</i><i>) !=EOF) i++;<br /><br /><br /><br />for(j=0;j<i;j++)<br />{<br />printf("%d %s %d %d %d %d %dn",no[j], matrix[j], quiz_1[j], quiz_2[j], quiz_3[j], project[j], midT[j]);<br />}<br /><br />fclose(infile);<br />}#include <stdio.h><br /><br /><br />int main ()<br />{<br />char matrix[20][200];<br />int i, j, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20];<br />FILE *infile;<br /><br />infile = fopen("studentsmark.txt","r");<br /><br />i=0;<br /><br />while( fscanf(infile,"%d %s %d %d %d %d %d",&no</i><i>, matrix</i><i>, &quiz_1</i><i>, &quiz_2</i><i>, &quiz_3</i><i>, &project</i><i>, &midT</i><i>) !=EOF) i++;<br /><br /><br /><br />for(j=0;j<i;j++)<br />{<br />printf("%d %s %d %d %d %d %dn",no[j], matrix[j], quiz_1[j], quiz_2[j], quiz_3[j], project[j], midT[j]);<br />}<br /><br />fclose(infile);<br />}</i>
but i am unable to read tis line
No Matrix quiz1(5%) quiz1(5%) quiz1(5%) Proj(25%) MidT(20%)what should i add to make the program read it and ignore it so it can read on the real data i need, i really have no idea…
- March 22, 2009 at 1:12 pm #3532msaqibParticipant
Use fgets() function to read the file line by line and then split the string and store all the values in variables.
123456789101112131415<br />int x = 0;<br />while (fgets(msg,25,fp)!=NULL)<br />{<br />x++;<br />if(x==0){<br />//we do not need to read first line as it contians headings<br />//this is the first line which you don't need to read and you can hardcode it in progrm<br />}<br />else{<br />//write your rest of the program here to read the data and manipulate<br />}<br /><br />}<br /> - March 22, 2009 at 2:37 pm #3533Ernesto0751Participant
thanks! i hav been thinking the whole day for this. I will try working on it !
- March 23, 2009 at 10:19 am #3534Ernesto0751Participant
now my program able to read the file by adding some code given by you, but i couldnt understand how it actually run..
1234567891011121314151617181920212223242526272829303132333435363738#include <stdio.h><br /><br /><br />void main (void)<br />{<br />char matrix[20][200], line[100][100];<br />int i, j, x, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20], final[20];<br />FILE *infile;<br /><br />infile = fopen("studentsmark.txt","r");<br /><br />x=0;<br />while (fgets(line,100,infile)!=NULL)<br />{<br /><br />x++;<br />if(x==1)<br />{<br /><br />}<br /><br />else{<br />i=0;<br />while( fscanf(infile,"%d %s %d %d %d %d %d %d",&no<i>,matrix</i><i>, &quiz_1</i><i>, &quiz_2</i><i>, &quiz_3</i><i>, &project</i><i>, &midT</i><i>, &final</i><i>) !=EOF)<br /><br /><br /><br />printf("%d %s %d %d %d %d %d %dn",no</i><i>, matrix</i><i>, quiz_1</i><i>, quiz_2</i><i>, quiz_3</i><i>, project</i><i>, midT</i><i>, final</i><i>);<br />i++;<br /><br /><br /><br /><br />}<br /><br />}<br />fclose(infile);<br />}</i>this is my actual input file
123456789101112123401234567890123456789012345678901234567890123456789012345678901234567890123456789<br />No Matrix quiz1(5%) quiz1(5%) quiz1(5%) Proj(25%) MidT(20%) Final(40%)<br />1 BK20008 4 3 5 18 18 36<br />2 BK20002 5 5 4 10 8 30<br />3 BK20003 3 4 5 18 10 35<br />4 BK20006 4 3 4 20 14 36<br />5 BK20005 5 4 3 13 17 28<br />6 BK20004 2 5 2 4 16 10<br />7 BK20007 3 4 4 21 14 32<br />8 BK20001 4 3 5 11 15 31<br />9 BK20009 3 2 4 10 10 20<br />10 BK20010 4 3 3 23 18 22there is a line of number in the input file.can you explain to me? thanks!
- March 26, 2009 at 7:10 pm #3535msaqibParticipant
The code
1while (fgets(line,100,infile)!=NULL)reads the file and function fgets() reads the file line by line. So when the program reads first line, it skips the first line of records which is
No Matrix quiz1(5%) quiz1(5%) quiz1(5%) Proj(25%) MidT(20%) Final(40%)
and is done by this code:1234if(x==1)<br />{<br /><br />}And you read all the marks and data in else part of the program.
123456else{<br />i=0;<br />while( fscanf(infile,"%d %s %d %d %d %d %d %d",&no<i>,matrix</i><i>, &quiz_1</i><i>, &quiz_2</i><i>, &quiz_3</i><i>, &project</i><i>, &midT</i><i>, &final</i><i>) !=EOF)<br />printf("%d %s %d %d %d %d %d %dn",no</i><i>, matrix</i><i>, quiz_1</i><i>, quiz_2</i><i>, quiz_3</i><i>, project</i><i>, midT</i><i>, final</i><i>);<br />i++;<br />}</i>
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.