Thread: Read each line in a text file when I press a key (inside while loop)? C

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    13

    Read each line in a text file when I press a key (inside while loop)? C

    Hello everyone!

    I know how to use fgets inside a while loop to read an entire text file, but how can I press a key inside that loop, so it will read another line, one at a time?
    I tried with a simple printf("Press any key\n") getchar() but nothing happens, I run the programm and it just doesn't do anything, not even show the first line. I'm assuming this may be a stupid question but I can't find anywhere how to do this

    Here is what I've tried:

    Code:
    /*  gcc readline.c -Wall -o read */
    
    
    #include <stdio.h>
    //#include <stdlib.h>
    
    
    
    
    
    
    
    
    int main (int argc, char *argv[]) {
    
    
        char url[]="dbus.log";
        FILE *arq;
        char info[1000];
        
        arq = fopen(url, "r");
        
            while( (fgets(info, sizeof(info), arq))!=NULL ){        
                
                printf("%s", info);
                printf("Press Any Key to Continue\n");  
                getchar();   
                
            }
            
        fclose(arq);
        
         return 0;
    }
    Any clues on how to proceede?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should check to insure that the file actually opens.

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    13

    Thumbs up

    Hey Jim!

    I checked and it was reading the entire file without problem (the file is on the same directory), but i forgot something very important inside the while loop..... just forgot to put a fflush(stdout); before I use getchar()... . Here is the code working 100%:

    Code:
    /*  gcc readline.c -Wall -o read */
    
    
    #include <stdio.h>
    //#include <stdlib.h>
    
    
    int main (int argc, char *argv[]) {
    
    
        char url[]="dbus.log";
        FILE *arq;
        char info[1000];
    
    
        arq = fopen(url, "r");
    
    
        while (fgets(info, sizeof(info), arq) != NULL) {        
            printf("%s", info);
            fflush(stdout);
            printf("Press Any Key to Continue\n");  
            getchar();   
        }
    
    
        fclose(arq);
        return 0;
    }
    Thanks for your answer Jim!

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by kdan View Post
    just forgot to put a fflush(stdout); before I use getchar()
    That's unlikely to have been the problem since the terminal is normally line-buffered so that outputting a newline will automatically flush it. And it's not "press ANY key" but "press the ENTER key".

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Aug 2016
    Posts
    13
    @itsme86 Yeah it is, and I'm sorry for that, I just needed to make it work as soon as possible, and it won't happen again

  7. #7
    Registered User
    Join Date
    Aug 2016
    Posts
    13
    @algorism, I'm actually running it in Msys2 and not in a Linux OS, not sure if it interfere, but I do need to use the fflush otherwise it won't work, and yes indeed is not any key but enter key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read a text file line by line?
    By Sam Conran in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2016, 09:22 AM
  2. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  3. Is there any way to read a line from a text file?
    By megareix in forum C Programming
    Replies: 13
    Last Post: 01-09-2009, 01:13 PM
  4. Read each line of text file.
    By Mithoric in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2003, 11:53 AM
  5. how do I read the last line of a text file?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-12-2002, 05:34 PM

Tags for this Thread