Link to home
Start Free TrialLog in
Avatar of Serena2345
Serena2345Flag for United States of America

asked on

Open and read file

#include <stdio.h>

int main()
{
    FILE *f;
    char s[1000];

    f=fopen("fgetsc","r");
    while (fgets(s,1000,f)!=NULL)
        printf("%s",s);
        sleep(10);
       
    fclose(f);
   
    return 0;
}

This is supposed to read from a file and output results to the screen. However the program runs so fast I can't see if I've done it right and suggestions to slow it down or fix? I'm going back to scholl in Jan and trying to relearn some things I forgot
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

You can pipe the output through a pager, like 'more':
If the above program is called 'myreader' execute it as

myreader | more

That will pause on each screen full of text so you can verify the output.
You could also redirect the output to another file and compare the two:

myreader > fgetsc.out
diff fgetsc fgetsc.out

Avatar of GaryFx
GaryFx

While I agree with Brett's solution, let me also point out what I believe is a bug in your code:

while (fgets(s,1000,f)!=NULL)
       printf("%s",s);
       sleep(10);
       
The indentation suggests that you think the sleep is inside the loop.  However, this being C and not Python, that's not true.  You need to add the braces around the body of the loop.

This is why I recommend always using braces, and at a minimum, always using braces if a loop or if statement goes past one line.  Many people will write stuff like
    while(SomeBooleanCalculation(x))
         SomeFunction(x)

which is technically correct, but highly error prone during maintenance.

Gary
Avatar of Serena2345

ASKER

still wont slow down
hi

try put a counter variable. After say 25 or 30 lines you can put a getch() to pause the output and then set counter back to 0

Dhyanesh
Hi,

your code seems to be good but the sleep should come inside the while loop, then things will slow down. At present the sleep(10) falls after the loop hence it makes no effect.

try this out in your code.

while (fgets(s,1000,f)!=NULL)
{
        printf("%s",s);
        sleep(1);
}

I bet you dont want to sleep(10), thats 10 seconds for every line. If sleep(1) also seems to be more try using usleep(1000) or usleep(2000).

hope this helps!

-Jinu
       
   
Depending upon the input, you may be bit by buffering.  You could always try doing a flush right after the printf.  

But why bother?  I was just pointing out another error in the code.  The use of more is the easy way.

Gary
okay I think i messed up and did not explain myself very well.
the file to be read is in this fashion
John
E
Smith
The output to the screen should be on one line like John E Smith
I keep getting
John
E
Smith
This is what I have (now that i can see it )
#include <stdio.h>
#define max 20

int main(void)
{
      char name[max];
      FILE *fp;
      fp=fopen("fgetsc.txt","r");
      while (fgets(name,max,fp)!= NULL)
      puts(name);
            
      sleep(10);
      return(0);
}
      Thanks for any help
puts always appends a newline.  You can use fwrite instead, and then manually write the newline.

And please, put in the braces around your while loops.  It will save you debugging in the future.

Gary
I think I've got it
#include <stdio.h>
#define max 20

int main(void)
{
     char name[max];
     FILE *fp;
     fp=fopen("fgetsc.txt","r");
     while (fscanf(fp,"%s",name)!= EOF)
     printf("%s"" ",name);
         
     sleep(10);
     return(0);
}
ASKER CERTIFIED SOLUTION
Avatar of CetusMOD
CetusMOD
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial