Thread: reading text from a file

  1. #16
    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

  2. #17
    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

  3. #18
    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.

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by laserlight View Post
    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?


    yes, that's correct. The number of words is given in the txt file.


    I've got it dynamically allocate the strings array, but am having trouble copying the temp variable 'buffer' into the array of strings.

    when I add these two lines
    Code:
            strcpy(array[i], buffer);
            printf("%s\n", array[i]);
    into the while loop reading in the text, I get a segmentation fault.

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by ktran03 View Post
    when I add these two lines
    Code:
            strcpy(array[i], buffer);
            printf("%s\n", array[i]);
    into the while loop reading in the text, I get a segmentation fault.
    So have you allocated enough space for array[i] before copying over it?
    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

  6. #21
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by vart View Post
    So have you allocated enough space for array[i] before copying over it?
    this is part of it

    Code:
            array[i] = malloc(sizeof (char) * 20);

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    oh crap, I think i know what's wrong


    brb

    edit, nm...that can't be it.

    I was thinking the first number in the file was being held in buffer, then being strcpy'd.

    but since I did a printf statement of the buffer, it skips the number and prints the first word, so

    the buffer is holding the first word, not the number.
    Last edited by ktran03; 01-31-2009 at 02:51 PM.

  8. #23
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Code:
    array[0] = "hellohjhjghjgjhgjhghjgjhgj";
        array[1] = "world";
        printf("%s\n%s\n", array[0], array[1]);

    i just tested that after the malloc, so it must be big enough

  9. #24
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by ktran03 View Post
    Code:
    array[0] = "hellohjhjghjgjhgjhghjgjhgj";
        array[1] = "world";
        printf("%s\n%s\n", array[0], array[1]);

    i just tested that after the malloc, so it must be big enough


    got it,

    removed these test values and it works. WICKED!!! making progress is wicked.

  10. #25
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    quick question


    how do I know if the file I'm opening is empty?

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ktran03 View Post
    quick question


    how do I know if the file I'm opening is empty?
    Why does it matter - if you try to read the file, you will know when you have reached the end. If that is ZERO or 1000 bytes into the file, what difference does it make?

    You can measure the length of the file using fseek() and ftell(). But for most things, it doesn't really matter much.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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