WRT counter show me that line from a txt file

Hello Experts,

   I am beginner to C language.. 

I know I am making a simple mistake but I dont know what the problem here

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

[code]

int main(void)
{
FILE* fh;
char s[50];
int i;
int count =10;
double a,b;
printf("COUNT= %d", count);
fh=fopen("abc1.txt","r");
if (fh ==NULL){
printf ("Error opening file: abc1.txt");
}

    for\(i=0; i&lt; count; i\+\+\)\{
                fgets\(s,49,fh\);
            if\(feof\(fh\)\)\{
                    rewind\(fh\);                    //fclose\(fh\);previously doing like this
                    i--;                                   //fh=fopen\("abc1.txt","r"\);previously doing like this
            \}
    \}
            sscanf\(s,"%lf %lf\\n",&a,&b\);
            printf\("VALUES :%f %f\\n", a, b \);

    fclose\(fh\);

}
[\code]
suppose I ve txt file as below
abc1.txt

[code]
10.32 11.98
24.76 34.87
43 55
6 7
8 9
10 11
12 13
[\code]
I want If the counter value is 10 then it should display me the line 43 and 55 , but here it displays
24.76000 34.87000

I got the error I modified the code in bold .. Thanks..........

fh=fopen("abc1.txt","r");
if (fh ==NULL){
printf ("Error opening file: abc1.txt");
}

Is the code compiling alright? From the code you have posted it should dump because of how the input file is being specified specifically the code segment shown above. So how are you specifying the input filename to the compiled executable?

Here's the proper way...

fh = fopen(*++argv, "r");
if (!fh) {
   printf ("Error opening file: %s\n", *argv);
}

Thanks for the reply . I ve no problem with that .
I ve abc1.txt in the current directory , Iam not giving from command line argument..

Now I am doing like below for reading a file over and over upto the counter value.. But when I am using feof() func it gives me the following error.

read.c:13: warning: dereferencing `void *' pointer
read.c:13: request for member `_flag' in something not a structure or union

Please help me get my code correct..

#include <stdio.h>
 
void* fileopen();
void read_line(void*);
 
void read_line(void *fh){
              char s[50];
              int i,j;
              int counter = 3000;
                     for(i = 0; i< counter; i++){
                             fgets(s,49,fh);
                             printf("%s", s);
                             //if(feof(fh)){
                               //      rewind(fh);
                           // }
                     }
                     fclose(fh);
}
void* fileopen(){
          void *file = fopen("abc1.txt", "r");
            return file;
}
int main(void) {
 
           void *fh;
              fh = fileopen();
              read_line(fh);
              return 0;
}
#include <stdio.h>
 
FILE *fileopen();
void read_line(FILE *);
 
void read_line(FILE *fh)
{
     char s[50];
     int i;
     int counter = 3000;

     for (i = 0; i < counter; i++) {
          fgets(s, 49, fh);
          printf("%s", s);
          if (feof(fh))
               rewind(fh);
     }
     fclose(fh);
}

FILE *fileopen()
{
     return fopen("abc1.txt", "r");
}

int main(void)
{
     FILE *fh;
     fh = fileopen();
     read_line(fh);
     return 0;
}

Thank you for your nice reply..

yeah Now it works ,, Can you please Tell me why its printing me the last line of the file twice , In my case if I take
abc1.txt

12 23
23 34
23 34 

Then it prints the last line twice instead of once ..

Actually I want to implement one function like below

It 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 .. If the file pointer fh is already opened in another function fileopen();

FILE* fileopen(){
          void *file = fopen("abc1.txt", "r");
            return file;
}

void read_line(FILE *fh, double *input_re, double *input_im){
        double a, b;
        char s[50];
        if ( fgets(s,49,fh) != NULL){
          printf("%s ",s);
          sscanf(s,"%lf %lf\n",&a,&b);
          *input_re = a;
          *input_im = b;
        }
        else{
                rewind(fh);
        }
}

Pls help ..
Regards,
User_prady

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 ..