Storing data from a file into a single char string

This is a discussion on Storing data from a file into a single char string within the C Programming forums, part of the General Programming Boards category; Hi I am writing a game that uses several text files for its data, and I was wondering how to ...

  1. #1
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35

    Question Storing data from a file into a single char string

    Hi I am writing a game that uses several text files for its data, and I was wondering how to store the entire data within the text file in a single character string of the char *.

    I figured something like this should do it:
    Code:
    FILE *file
    char ch;
    char *data;
    
    while ((ch = fgetc(file)) != EOF)
    {
        /* Function to append ch to the end of data */
    }
    But obviously there must be a better way. If not then could some one tell me how to do it this way?
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Code:
    FILE* pFile;
    pFile = fopen("filename.txt","rb");
    
    fseek (pFile, 0, SEEK_END);
    long size=ftell (pFile);
    char* data=new char[size];
    fread(data,1,size,pFile);
    fclose(pFile);
    
    // work with data all you want
    ....
    
    delete [] data;

  3. #3
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    The above code seems to have a problem with
    Code:
    new char
    . It seems to me that "new" is a C++ keyword and is not available in C. Not only does it not work but it also apears as if your trying to read binary data from a plain text file.
    Last edited by tuxinator; 05-01-2006 at 08:54 PM.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Replace it with this:
    Code:
    char* data = malloc(sizeof(char) * size);
    The sizeof(char) is somewhat redundant, but I figure there is no harm in it.
    Last edited by SlyMaelstrom; 05-01-2006 at 09:01 PM. Reason: Whoops. Semicolon!
    Sent from my iPadŽ

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Oh, oops, that's me not paying attention. Sorry about that.

    Also, it doesn't matter that it's a text file. It's still binary.

  6. #6
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    The next problem:

    When I output the code I have it set to print the "data" variable. So the output should be something like this:
    Code:
    /* This is the contents of the text file that I am trying to load */
    
    (levelset 1
      (name: Classic)
      (location: "classic/classic.tbls")
      (levels: 11))
    (levelset 2
      (name: Confusion)
      (location: "confusion/confusion.tbls")
      (levels: 0))
    (levelset 3
      (name: Insanity)
      (location: "insanity/insanity.tbls")
      (levels: 1))
    
    /* EOF */
    However I seem to be getting a segmentation fault.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    You'll need to make the buffer size sizeof(char) * size +1 and add a null character at the end:
    Code:
    data[sizeof(char) * size] = '\0';

  8. #8
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    Still segmentation faults. Please check to make sure that I have to code correct as I may not.

    Code:
    FILE* pFile;
    pFile = fopen("filename.txt","r");
    fseek (pFile, 0, SEEK_END);
    long size=ftell (pFile);
    char* data = malloc(sizeof(char) * size+1);
    data[sizeof(char) * size] = '\0';
    fread(data,1,size,pFile);
    fclose(pFile);
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  9. #9
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Though it shouldn't matter, it makes more sense to put the data[sizeof(char) * size] = '\0'; statement after you've read the data in. Other than that, you need to make it "rb" not "r".

    Ah damn, fix it like so:
    Code:
    FILE* pFile;
    pFile = fopen("filename.txt","rb");
    
    fseek (pFile, 0, SEEK_END);
    long size=ftell (pFile);
    fseek (pFile, 0, SEEK_SET);
    
    char* data = malloc(sizeof(char) * size+1);
    fread(data,1,size,pFile);
    data[sizeof(char) * size] = '\0';
    fclose(pFile);

  10. #10
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    Still get the segmentation fault. I changed the code as follows:
    Code:
    FILE* pFile;
    pFile = fopen("filename.txt","rb");
    fseek (pFile, 0, SEEK_END);
    long size=ftell (pFile);
    fseek (pFile, 0, SEEK_SET);
    char* data = malloc(sizeof(char) * size+1);
    fread(data,1,size,pFile);
    data[sizeof(char) * size] = '\0';
    fclose(pFile);
    It's happening at the "fseek" function. I found this out by commenting out each indavidual line with this "//" and removing them one by one and found out that it starts the segmentation fault at the fseek.
    Last edited by tuxinator; 05-01-2006 at 09:28 PM.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  11. #11
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Check the edit.

  12. #12
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    There *is* a file called filename.txt somewhere in your directory, right?

  13. #13
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    I have comfirmed that it is do to the "fseek" function as I ran it with gdb and got this:
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread 16384 (LWP 1303)]
    0xb7dc022d in fseek () from /lib/i686/libc.so.6
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  14. #14
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    The text file is what fooled me. Since I merely copied and pasted the code you guys gave me I kept forgetting to change the "filename.txt" to the location of the file. It works now.

    Thanks a bunch. This is was the only major problem with my code that I was facing.

    P.S. If you would like to see the development status of the game itself go the the following URL: tuxblocks.sourceforge.net

    Please note that I am still at the very beggining of the coding process and haven't even set up the screen yet.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  15. #15
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Glad to help, and sorry about my laziness. Long day today at work.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21