Hello Friends,
I got stuck with fgets () & rewind() function .. Please need help..
Actually I am doing a like,
The function should read lines from a txt file until the function is called..
If the data from the txt file ends then it goes to the top and then again when the function is called it should display me the top row .. I am doing like below , But it's not doing what I am expecting
Please need help..
#include <stdio.h>
FILE* fileopen();
void read_line(void*);
void read_line(void *fh){
char s[50];
int i,n;
if( fgets(s,49,fh) != NULL) {
n = 0;
while(isspace(s[n])){
n++;
if(s[n] == '/' && s[n++] == '/'){
fgets(s,49,fh);
}
}
printf("%s", s);
}
else{
rewind(fh);
}
}
FILE* fileopen(){
void *file = fopen("abc1.txt", "r");
return file;
}
int main(void) {
void *fh;
int i;
fh = fileopen();
for(i = 0;i < 12; i++){
read_line(fh);
}
return 0;
}
abc1.txt
6 7
8 9
12 13
output
6 7
8 9
12 13
6 7
8 9
12 13
6 7
8 9
12 13
It should give me 12 rows as the function is called 12 times..
but it is returning me 9 rows..
How to solve it I am wondering from last couple of days ..Pls need help badly..
Regards,
prady