   Save/read string array and NULL
  Hello everyone.I have huge problem...I run my program and I add something to my string array.I have array like this:Quote:index 0 = NULLindex 1 = "text1"index 2 ="text2"Then, I save this array to the file and close the program.After that, I run my program again but... array look like this:Quote:index 0 = "text1"index 1 = "text2"........So everything has been moved!NULL wasn't save.My save/read function:void Save(){ofstream Strm; Strm.open("tex_dane.txt"); if (!Strm.is_open()) return; else    for(int i=0;i<2000;i++){ Strm <<tex_dane<<endl;   }}void Read(){ ifstream Strm; string S; Strm.open("tex_dane.txt"); int i=0; if (!Strm.is_open()) return; else {   do{if((Strm >> S).good()){tex_dane=S;++i;}}   while (!Strm.eof()&&!Strm.bad());}}Is it possible, that these functions are changing my array?   
  I'm assuming that when you run your program, you first call Read(), and then call Save().Your culprit is probably if((Strm >> S).good()){not doing what you think it is.  If you open your file, does it look like this?"text1text2"If so, 'Strm >> S' is going to skip all white space, including your NULL entry for index 0.  Using your debugger, look at the first entry the Read() method puts into 'tex_dane', it won't be NULL.Also, look into using a std::vector instead of a raw array for 'tex_dane'.   ;Quote:Original post by MantearI'm assuming that when you run your program, you first call Read(), and then call Save().Your culprit is probably if((Strm >> S).good()){not doing what you think it is.  If you open your file, does it look like this?"text1text2"If so, 'Strm >> S' is going to skip all white space, including your NULL entry for index 0.  Using your debugger, look at the first entry the Read() method puts into 'tex_dane', it won't be NULL.Also, look into using a std::vector instead of a raw array for 'tex_dane'.So, It's impossible to read NULL with this method?I changed code in Read function:int i=1; //not 0and it works fine now becouse I don't have more NULL...but I know it's not the best..   ;  You have a few problems here.1) In C++, we have a real string class, called std::string. You should be using it. It will avoid lots of memory management problems, eliminate worry about null pointers and give lots of extra functionality.Similarly, hard-coded arrays are brittle (it's easy to cause bugs if you want to change anything, and you end up having to make fixes in a lot of places), limiting and potentially very wasteful (what if you don't use all that space?). To avoid these problems, use a standard library container such as std::vector.2) With the C-style strings (pointers to char), a null pointer is not the same thing as an empty string.3) Input into a string (whether C-style or std::string) with operator>> reads one word of the input, not one line.4) You should use the read operation itself to control the loop. This is the way to get the logic right. See more here.5) If you separate your input with newlines, you'll have a logic problem reading them in if you want a string that actually contains a newline. There are, of course, ways around this.   ;Quote:You have a few problems here.1) In C++, we have a real string class, called std::string. You should be using it. It will avoid lots of memory management problems, eliminate worry about null pointers and give lots of extra functionality.Similarly, hard-coded arrays are brittle (it's easy to cause bugs if you want to change anything, and you end up having to make fixes in a lot of places), limiting and potentially very wasteful (what if you don't use all that space?). To avoid these problems, use a standard library container such as std::vector.Yea, I know. My array isn't char. It's a std::string tex_data[2000].I've never heard about something like std::vector. I would read something about it.Quote:5) If you separate your input with newlines, you'll have a logic problem reading them in if you want a string that actually contains a newline. There are, of course, ways around this.My array contains data abaout names of texture. It's not a big string. It's just a one or two(separated by " ") words.Anyway. I still don't understand anything and what should I do now ;D   