Saving string to file problem

This is a discussion on Saving string to file problem within the C++ Programming forums, part of the General Programming Boards category; I'm trying to save a strings to file, each of length 12, and where each character in the string can ...

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    Saving string to file problem

    I'm trying to save a strings to file, each of length 12, and where each character in the string can be of any type char, even the nonprintable ones. I then want to be able to load these 12 characters later on in the program and place them into a new string of length 12. Is there a way to do this?

    If I save the string to file normally and then turn around and load them into a new string, it doesn't always work unless all 12 characters are printable ones. Driving me nuts!

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Are you using std::string or arraybased char strings???

    Also how would the file contents be structured???

    Code:
    xxxxxxxxxxxx
    xxxxxxxxxxxx
    xxxxxxxxxxxx
    xxxxxxxxxxxx
    Like that???

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Errrr.... I'm using strings from the string library. And the data is stored consecutively in the same line. What I have it doing right now which isn't working (I've tried other ways which didn't work either) is a for loop that reads in one character at a time like so:
    Code:
    for (int x=0; x<12; ++x)
    {
        infile>>myString[x];
    }
    This works if everything is a printable character, but if its an unprintable it screws up.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    38
    Hmmm .... convert to bianary save as that .....then convert to ASCII while loading into a string?

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Thanks, I decided to go a different route and I'm not saving chars anymore but instead 64-bit ints to binary. Works fine now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in writing string to File
    By Bargi in forum C Programming
    Replies: 4
    Last Post: 02-18-2009, 12:05 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. File saving problem in DevC++ pls help ???
    By intruder in forum C Programming
    Replies: 3
    Last Post: 12-17-2002, 01:17 AM

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