Thread: reading text from a file

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    51

    reading text from a file

    I don't know where to start, someone please point me in the right direction.

    I opened a file in main, sent the file pointer to a function, and am now trying to read in text.


    the text file goes something like


    5
    hello there
    ok bye now


    5 indicating the number of words. Is there a function to read in words, then when encountering a space, go to the next word? I'm guessing that would make my life too easy.

    anyhelp is appreciated thankx.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by ktran03 View Post
    I don't know where to start, someone please point me in the right direction.

    I opened a file in main, sent the file pointer to a function, and am now trying to read in text.
    Great, where's what you've got so far?

    the text file goes something like


    5
    hello there
    ok bye now


    5 indicating the number of words. Is there a function to read in words, then when encountering a space, go to the next word?
    No.

    I'm guessing that would make my life too easy.

    anyhelp is appreciated thankx.
    Looks like homework. Show an attempt first. You might use fgetc which reads characters or fgets which reads lines (provided the buffer is big enough).

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Try this (untested)
    Code:
    char inputbuffer[200]; //make it big enough to not overflow
    
    while(!fscanf(filepointer,"%s",inputbuffer)) printf("%s\n",inputbuffer);
    fclose(filepointer);

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    omfg, after 7 hours of pounding my head....I finally got it to read something in.

    Code:
        char string[5];
        int i;
        
        for(i=0; i<5; i++){
        string[i] = getc(file);
        printf("%c\n\n", string[i]);
        }

    the code is very primitive, but atleast I finally got somewhere.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by cwr View Post
    Great, where's what you've got so far?


    No.



    Looks like homework. Show an attempt first. You might use fgetc which reads characters or fgets which reads lines (provided the buffer is big enough).

    yo why so bitter?

    it is homework. If anyone remembers me from my posts on here, I never ask for code straight. I always ask people to point me in the right direction, so I can learn, and not just get passed code.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by esbo View Post
    Try this (untested)
    Code:
    char inputbuffer[200]; //make it big enough to not overflow
    
    while(!fscanf(filepointer,"%s",inputbuffer)) printf("%s\n",inputbuffer);
    fclose(filepointer);
    thankx,

    I'm gonna try to implement that.

    but what's the difference between fscanf, and getc?

    and when I use getc, how do I know I've encountered a white space, and therefore need to move onto a new word in the array?

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by ktran03 View Post
    Code:
        char string[5];
        int i;
        
        for(i=0; i<5; i++){
        string[i] = getc(file);
        printf("%c\n\n", string[i]);
        }
    Okay, so you've managed to create a loop, but it's not much use because it insists on reading exactly 5 characters from the file. What you want is a loop that continues until you read the end of the file or an error. Look up what getc() returns when such a condition is reached, and adjust your loop accordingly.

    You're not actually clear on what you need the program to do. What do you intend to do with it aside from read the file?

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by cwr View Post
    Okay, so you've managed to create a loop, but it's not much use because it insists on reading exactly 5 characters from the file. What you want is a loop that continues until you read the end of the file or an error. Look up what getc() returns when such a condition is reached, and adjust your loop accordingly.

    You're not actually clear on what you need the program to do. What do you intend to do with it aside from read the file?


    I'm gonna take it one step at a time. The assignment is pretty big, I'm trying not to overwhelm myself, chipping away piece by piece.

    but task 1 is to get the words read into a string of characters.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    I'm gonna go read up on fscanf a for a bit...

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by ktran03 View Post
    I'm gonna go read up on fscanf a for a bit...
    Oops made a bit of an error I should have put

    Code:
    while(fscanf(filepointer,"%s",inputbuffer)!=-1) printf("%s\n",inputbuffer);

    Not
    Code:
    while(!fscanf(filepointer,"%s",inputbuffer)) printf("%s\n",inputbuffer);


    This should work, file is called 'all.txt'

    Code:
    #include <stdio.h>
    
    
    FILE *filepointer;
    char inputbuffer[200]; //make it big enough to not overflow
    
    main(){
    
    	if (	(filepointer=fopen("all.txt","r")) == NULL) {
    			puts("all.txt does not exist");
    	}
           else{
                while(fscanf(filepointer,"%s",inputbuffer)!=-1) printf("%s\n",inputbuffer);
                fclose(filepointer);
           }
           getchar();
    }

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by esbo View Post
    Try this (untested)
    Code:
    char inputbuffer[200]; //make it big enough to not overflow
    
    while(!fscanf(filepointer,"%s",inputbuffer)) printf("%s\n",inputbuffer);
    fclose(filepointer);
    once again you post code with errors.
    try to think a little before posting what will you code do if fscanf returns EOF
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by ktran03 View Post
    thankx,

    I'm gonna try to implement that.

    but what's the difference between fscanf, and getc?

    and when I use getc, how do I know I've encountered a white space, and therefore need to move onto a new word in the array?
    fscanf reads it's input seperated by white space (unless specified otherwise) according to to the specified format, %s means a character string.

    getc reads a character at a time.

  13. #13
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by vart View Post
    once again you post code with errors.
    try to think a little before posting what will you code do if fscanf returns EOF
    I was having problems with my code too, I was hoping someone would be able to fix it.
    Anyway if it had worked he might have just copied it and not learnt anything.
    Think of it as an extended assignment, cwr did say to make an attempt first
    Last edited by esbo; 01-30-2009 at 12:57 AM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    esbo brings up a good point: what is the length of the longest word that you expect? There are ways to accomodate words of arbitrary length, but that might be a little too complex for you at the moment.

    That said, even when you do know the expected maximum length, you should still write code to not overflow your string buffer should invalid input be entered with a word longer than expected.

    Modifying esbo's example:
    Code:
    #include <stdio.h>
    
    int main(void) {
        FILE *file_pointer;
        char input_buffer[200];
    
        if ((file_pointer = fopen("all.txt", "r")) == NULL) {
            puts("all.txt does not exist");
        } else {
            while (fscanf(file_pointer, "%199s", input_buffer) != EOF) {
                printf("%s\n", input_buffer);
            }
            fclose(file_pointer);
        }
    
        return 0;
    }
    The "%199s" ensures that at most 199 characters will be put in the string, with the last reserved for the terminating null character. Note that with this approach the integer value denoting the number of words will also be read as a word. Perhaps you are supposed to read it first, create a dynamic array of strings with that value as the size, and only then proceed to read in the words into that array?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by vart View Post
    once again you post code with errors.
    try to think a little before posting what will you code do if fscanf returns EOF
    And what did you post which was constructive?

    Nothing. Hence - plonk!
    Last edited by esbo; 01-30-2009 at 08:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM