• 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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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
Serena2345Author Commented:
still wont slow down
Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

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
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
       
   
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
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
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
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);
}
CetusMODCommented:
PAQed, with points refunded (50)

CetusMOD
Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.