'How can I read the next line from a text file every time I enter the loop?
I have written this program and I want every time I enter the while loop to read the next line from my text file and not the before lines. So far, this code with fscanf inserts in a list all the elements before the one I want. My text files have 4 elements per line and I do not want to read it all as a string also. For example, if the elements from line 1 have entered in the list, the next time in the loop I want to start from line 2 and not to read again the line 1 etc.
for(Time=0.0;Time<200;Time=Time+12.0) {
for(i=5;i<8;i++){
char to_open[32];
snprintf(to_open,32, "fptg_%d.txt", i);
printf("i=%d\n", i);
if ((fr = fopen(to_open, "r")) == NULL)
{
break;
}else{
fscanf(fr, "%d %d %d %lf",&rollnumber, &src, &dest, &gentime);
while(gentime<Time){
if (insertedpackets[i]<10){
printf ("-------------TIMESLOT-------------\n");
printf("time: %.1f\n",Time);
fscanf(fr, "%d %d %d %lf\n",&rollnumber, &src, &dest, &gentime);
insert(rollnumber,src,dest,gentime);
insertedpackets[i]++;
printf("insertedpackets[%d]:%d\n\n", i, insertedpackets[i]);
printf ("----------list till now-----------\n");
display();
printf ("----------------------------------\n\n\n\n");
}
else if (insertedpackets[i]>=10)
{
break;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
