Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Open and read file

Posted on 2003-11-08
12
Medium Priority
?
692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

715 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