-
What is wrong here?????
I have run through this code many times, and i cant figure out what's wrong with it! why doesn't it work?? i have isolated it to just the following lines:
Code:
fstream DataFile("data.txt", ios::in | ios::binary);
if(!DataFile)
{
cout << "No file found!!!\n";
return 0;
}
else
{
char* data;
data = new char;
char tmp;
while(DataFile.get(tmp)) strcat(data, &tmp);
cout << data << endl;
delete data;
return 0;
}
my thoughts are maybe something to do with the strcat? also how do i know i am not overrunning something??
-
I think it might have something to do with
Code:
char* data;
data = new char;
You should do this:
Code:
char* data = new char;
(I think)
-
...
but that shouldn't matter? anyways i guess what would be a better idea than just asking 'whats wrong??' would be to kind of explain the code.
main objective:
to replace 5 lines of variable length within a file.
the file is somewhat similar to an ini file, containing any number of definitions in the following format:
<name>
<line1>
...
<line5>
what i need to do is find the name of the current definition as loaded into the memory, then replace the 5 lines that follow it with the program-modified version, and close the file. alternately, if the name is not found, i want to append the name along with the five lines at the end of the file.
now, to my understanding, a file in it's primitive state is simply a string. so, i cant just replace say 10 characters with 15, or i will be overwriting the next line of the file. so i decided to try loading the whole file into memory as a string, and then see what i can do from there. does this seem like a good approach?
--------
added
-------
maybe an array of strings would be better, each string for one line of the file? then when i want to save it back just slap it all back in there in a row?
-
char* data;
data = new char;
char tmp;
while(DataFile.get(tmp)) strcat(data, &tmp);
You only alocates memory for one char but you try to squeeze in a whole file?
-
Yes, reading the entire file into memory, making changes as needed in file, and writing back to file is part of basic file handling.
data should be a string, not a single char
since you don't know how many sets of instructions there will be, a list or a vector may be a better container to hold the file contents. While more straighforward with STL strings and lists, this task is also doable with user defined lists and C style strings.
string buffer;
vector<string> fileContents;
ifstream fin("filename.txt");
while (!fin.eof())
{
//use geline for string class to read first line into buffer
//place buffer into vector
if(buffer == targetString)
//change next five lines of input before sending to fileContents
else
//send next five lines of input to fileContents unchanged
}
now write all of fileContents back to filename.
-
Right after i posted, i began wondering about that whole 'new char' thingie.. realised that would mean i only gots memory for one char. course, making an array would be tough cause i don't know a) how big the file is or even b) how many extra characters i want to put in after modifying it, so anyway I tried using a string and it works fine. I think I am really beggining to almost nearly kinda sorta understand pointers.
anyways thanks for the help, it works pretty good now. talk to you later!