file - How to read line in C -
i want reach line in file(*.txt)
my file includes 3 type. firstly number of lines-1, secondly word , number, thirdly query in last line.
for example;
5
dog 3
school 2
apple 2
car 1
cat 4
spoon food heart game stone pen
file *fp; char arr[10][5]; char times[10]; int numberoflines; char querytemp[1000]; fp = fopen("deneme.txt","r"); fscanf(fp,"%d",&numberoflines); for(int i=0;i<10;i++) { fscanf(fp,"%s %d",arr[i],times[i]); } fscanf(fp,"%s",querysubmissionstemp);
where wrong? how can that?
you inputting numberoflines
, inputting 10 lines regardless of entry.
fscanf(fp,"%d",&numberoflines); for(int i=0;i<numberoflines;i++)
also, don't see declaration querysubmissionstemp
.
fscanf(fp,"%s",querytemp);
incorporating bluepixy's comment:
char arr[10][5];
char[5]
small. need include space \0
null terminator @ end of string.
char times[10];
shoud int times[10];
fscanf(fp,"%s %d",arr[i],times[i]);
should fscanf(fp,"%4s %d",arr[i], ×[i]);
(4 small size, pointed out)
fscanf(fp,"%s",querytemp);
this reads 1 word. check out fgets
.
Comments
Post a Comment