Solved

Open and read file

Posted on 2003-11-08
12
685 Views
Last Modified: 2010-05-18
#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
0
Comment
Question by:Serena2345
12 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9708888
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
 
LVL 6

Expert Comment

by:GaryFx
ID: 9708968
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
 

Author Comment

by:Serena2345
ID: 9709107
still wont slow down
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9709264
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
 

Expert Comment

by:jinumjoy
ID: 9709296
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 6

Expert Comment

by:GaryFx
ID: 9709629
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
 

Author Comment

by:Serena2345
ID: 9709650
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
 
LVL 6

Expert Comment

by:GaryFx
ID: 9709661
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
 

Author Comment

by:Serena2345
ID: 9709765
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
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 10553301
PAQed, with points refunded (50)

CetusMOD
Community Support Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now