Thread: reading text from a file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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.

  4. #4
    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);

  5. #5
    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?

  6. #6
    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.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by ktran03 View Post
    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?
    Just by testing for equality with a whitespace character.

    if( variable == ' ' ) DoSomething();
    Last edited by DeadPlanet; 01-30-2009 at 10:04 AM. Reason: Changed wording

  8. #8
    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

  9. #9
    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.

  10. #10
    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.

  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
    And what did you post which was constructive?

    Nothing. Hence - plonk!
    I asked you to think. But I see now that this is not constructive in your case...
    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
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by esbo View Post
    And what did you post which was constructive?
    Some people believe that being condescending is constructive. It's not.

    You took the time to try work it out and to provide the OP with some code, which gave the OP a solid hint that fscanf was what was required.

    You did good, esbo.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  13. #13
    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.

  14. #14
    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?

  15. #15
    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.

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