Writing strings to file

This is a discussion on Writing strings to file within the C++ Programming forums, part of the General Programming Boards category; Alright, I thought I had fully grasped strings but lately a lot of my work has required me to output ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    31

    Writing strings to file

    Alright, I thought I had fully grasped strings but lately a lot of my work has required me to output strings and manipulate strings and I keep running into all sorts of trouble for such basic a subject.

    Alright, so treat me like a child on this one and explain why the heck the following code outputs a whole lot of spaces after the text in the text file.

    This is just an example of what my code resembles-
    Code:
    struct txt{
        char string[256];
    };
    
    struct msc{
        int numTxt;//sixe of array t[]
        txt *t;
    };
    
    void output(msc* m){
        ... open file ...
        fwrite( m->txt, sizeof(txt), msc->numTxt, file );
        ... close file ...  
    }
    That's essentially what I'm doing, I output to the console and it's fine. However I open up the text file and it'd be like (using _ to illustrate a space):
    ___string_________________________________________ __
    ______

    I can't make sense of it, what am I possibly doing wrong? This isn't the first time I've written to a file, but what have I done wrong this time?

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I don't see anyplace where you allocate memory for msc.t. I suggest you should consider dropping the pointer to the type, i.e.,:

    Code:
    struct msc{
        int numTxt;//sixe of array t[]
        txt t;
    };
    Further diagnosis will require more code.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    31
    Alright, maybe I should have just posted the whole glob of code in the first place (usually I would have, it's 4am, I'm hardly thinking straight).
    Code:
    #include <iostream>
    
    struct LOGENTRY{
           char txt[256];
    };
    
    struct LOGSTRUCT{
           int iNumLogs;
           LOGENTRY* entries;
    };
    
    int LogAddEntry( LOGSTRUCT* sLog, char* entry ){
        if( strlen( entry ) > 0 ){
             LOGENTRY* tmp;
             sLog->iNumLogs++;
             tmp = (LOGENTRY*)realloc( sLog->entries, sizeof(LOGENTRY)*sLog->iNumLogs );
             sLog->entries = tmp;
             strncpy( sLog->entries[sLog->iNumLogs-1].txt, entry, 256 );
        }else
             return -1;
        return sLog->iNumLogs;
    }
    
    void LogInit( LOGSTRUCT* sLog ){
        sLog->iNumLogs = 0;
        sLog->entries = (LOGENTRY*)malloc( sizeof(LOGENTRY) );
    }
    
    bool LogSave( LOGSTRUCT* sLog, char* file ){
        FILE* f = fopen( file, "w+" );
        if( f == NULL )
            return false;
        if( fwrite( &sLog->iNumLogs, sizeof( int ), 1, f ) != 1 ){
            fclose( f );
            return false;
        }
        if( fwrite( sLog->entries, sizeof( LOGENTRY ), sLog->iNumLogs, f ) != sLog->iNumLogs ){
            fclose( f );
            return false;
        }
        fclose( f );
        return true;
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Is this C or C++ (try to stick to C++ code on the C++ board)? Do you know how to (can you) use a vector<string> container? It would be much easier.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The problem is that you are writing your msc::t array as an array of txt structs instead of outputting the value of each txt::string individually. The way you are doing it, it outputs all 256 bytes in each string, instead of only outputting up to a terminating null.

    Try writing each entry in the t array one at a time. You will want to also write out the strlen of the string or you won't know how many bytes to read back in.

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    1) You are writing binary data to the file, so you should open the file with "wb+" mode.
    2) You are writing the entire struct object which contains 256 bytes allocated to the char array.

    What are you hoping to accomplish? Do you want to write text data to the file that will be human readible or do you just want the data stored so your app can read/write it?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    31
    Oh bloody hell, I knew it'd be something stupid like that. I think I'd best go to sleep, I've rewritten it and it's working now. Thanks guys.

    [edit:] I just wrote it how I have been writing all my other binary files, but those have had to be read back in and not viewed in notepad or anything so I just wasn't thinking.
    Last edited by Ichmael™; 07-12-2005 at 01:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 05:13 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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