• C

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
Serena2345Asked:
Who is Participating?
 
CetusMODCommented:
PAQed, with points refunded (50)

CetusMOD
Community Support Moderator
0
 
brettmjohnsonCommented:
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

0
 
GaryFxCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Serena2345Author Commented:
still wont slow down
0
 
dhyaneshCommented:
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
0
 
jinumjoyCommented:
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
       
   
0
 
GaryFxCommented:
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
0
 
Serena2345Author Commented:
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
0
 
GaryFxCommented:
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
0
 
Serena2345Author Commented:
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);
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.