CFile Insert not Write...
-
hello, i'm having a hard time figuring out how to insert a character/string on my CFile, i have no problem writing the character set at the end or at the beginning of the file, but when i try to add it in between, it overwrites the file. Example: i have "JON SMITH" on my file, when i try to insert "H" at index 2, it should look like this "JOHN SMITH", but what happens is this "JOH SMITH" it overwrites, it does not insert. here is my code
//write to file
CString txtInsert;
m_txtInsert.GetWindowText(txtInsert);
int szSize = txtInsert.GetLength();//create the file if (!cfile\_object.Open(l\_ExePath + "Logs\\\\cfile\_example.txt", CFile::modeWrite)) { AfxMessageBox(\_T("Cannot Open File for Reading!\\nPls. Check Directory of File!")); } else { cfile\_object.Seek( 2, CFile::begin ); cfile\_object.Write(txtInsert, szSize); //close file cfile\_object.Close();
i needed to type "H SMITH" just to get the result "JOHN SMITH" but all i want is the insert the character "H" on the right place.. any help or hints will be much appreciated, thankyou
-
hello, i'm having a hard time figuring out how to insert a character/string on my CFile, i have no problem writing the character set at the end or at the beginning of the file, but when i try to add it in between, it overwrites the file. Example: i have "JON SMITH" on my file, when i try to insert "H" at index 2, it should look like this "JOHN SMITH", but what happens is this "JOH SMITH" it overwrites, it does not insert. here is my code
//write to file
CString txtInsert;
m_txtInsert.GetWindowText(txtInsert);
int szSize = txtInsert.GetLength();//create the file if (!cfile\_object.Open(l\_ExePath + "Logs\\\\cfile\_example.txt", CFile::modeWrite)) { AfxMessageBox(\_T("Cannot Open File for Reading!\\nPls. Check Directory of File!")); } else { cfile\_object.Seek( 2, CFile::begin ); cfile\_object.Write(txtInsert, szSize); //close file cfile\_object.Close();
i needed to type "H SMITH" just to get the result "JOHN SMITH" but all i want is the insert the character "H" on the right place.. any help or hints will be much appreciated, thankyou
File I/O does not work this way, you can only overwrite the data that is already there. You can also add more information at the end by appending data to the existing file. However, if you want to modify what is already there then you must read it in, modify the contents in memory and write out the new information, either to the original, thus overwriting it, or to a new file.
Just say 'NO' to evaluated arguments for diadic functions! Ash
-
File I/O does not work this way, you can only overwrite the data that is already there. You can also add more information at the end by appending data to the existing file. However, if you want to modify what is already there then you must read it in, modify the contents in memory and write out the new information, either to the original, thus overwriting it, or to a new file.
Just say 'NO' to evaluated arguments for diadic functions! Ash
resolution: READ from whatever starting pointing from your file, save to buffer, then add whatever character you want at the beginning of the buffer. then WRITE the buffer to your file, this way you can insert...
//points to index where to start reading
cfile_object.Seek( insIndex, CFile::begin );//Reads the content of file starting at the SEEK index, in my case its index \[2\].. int flen = cfile\_object.GetLength(); char \*fBuf= new char\[flen\]; UINT file = cfile\_object.Read(fBuf,flen); fBuf\[file\] = '\\0'; //Joins the user input and the file read.. txtInsert = txtInsert + fBuf; //Points to index where to insert, in my case its index \[2\].. cfile\_object.Seek( insIndex, CFile::begin ); //writes to file where index pointer is.. cfile\_object.Write(txtInsert, szSize + file); //close file cfile\_object.Close();
modified on Tuesday, September 28, 2010 2:53 AM
-
resolution: READ from whatever starting pointing from your file, save to buffer, then add whatever character you want at the beginning of the buffer. then WRITE the buffer to your file, this way you can insert...
//points to index where to start reading
cfile_object.Seek( insIndex, CFile::begin );//Reads the content of file starting at the SEEK index, in my case its index \[2\].. int flen = cfile\_object.GetLength(); char \*fBuf= new char\[flen\]; UINT file = cfile\_object.Read(fBuf,flen); fBuf\[file\] = '\\0'; //Joins the user input and the file read.. txtInsert = txtInsert + fBuf; //Points to index where to insert, in my case its index \[2\].. cfile\_object.Seek( insIndex, CFile::begin ); //writes to file where index pointer is.. cfile\_object.Write(txtInsert, szSize + file); //close file cfile\_object.Close();
modified on Tuesday, September 28, 2010 2:53 AM
As I explained in my previous answer there is no 'insert' facility in file I/O, it has to be implemented by reading the data into memory, modifying the memory buffer(s) and rewriting all of the file from the point of insertion. The line
fBuf[file] = '\0';
will cause random memory corruption. I am not sure what the line
txtInsert = txtInsert + fBuf;
is supposed to do, but I doubt that it is what you expect.
Just say 'NO' to evaluated arguments for diadic functions! Ash
-
As I explained in my previous answer there is no 'insert' facility in file I/O, it has to be implemented by reading the data into memory, modifying the memory buffer(s) and rewriting all of the file from the point of insertion. The line
fBuf[file] = '\0';
will cause random memory corruption. I am not sure what the line
txtInsert = txtInsert + fBuf;
is supposed to do, but I doubt that it is what you expect.
Just say 'NO' to evaluated arguments for diadic functions! Ash
Well i have to agree with everything that you have written, i am totally new in visual C++... unlike the .NET that i came from, coding is more easier there, but MFC just gives me what i needed, MFC gives me "CONTROL OVER MY CODE" i am very thankful about your comments, i got a good hint how to control my Project... thanks a lot for posting,. the bigger problem that i face now is how to SORT the record on the CFile., i needed real time sorting for my main project.. every-time a user input is made, the record should be automatically sorted A-Z.. how to sort:
BANANA
APPLE
ORANGE
MANGO
PINEAPPLEand i got NO idea how to do that, any help would be very much appreciated
-
Well i have to agree with everything that you have written, i am totally new in visual C++... unlike the .NET that i came from, coding is more easier there, but MFC just gives me what i needed, MFC gives me "CONTROL OVER MY CODE" i am very thankful about your comments, i got a good hint how to control my Project... thanks a lot for posting,. the bigger problem that i face now is how to SORT the record on the CFile., i needed real time sorting for my main project.. every-time a user input is made, the record should be automatically sorted A-Z.. how to sort:
BANANA
APPLE
ORANGE
MANGO
PINEAPPLEand i got NO idea how to do that, any help would be very much appreciated
loid grey manuel wrote:
i got NO idea how to do that
Well, this is the place we all had to start from, but it does require you to spend some time learning. If you are serious about writing C++ programs then you should learn some of the features that come with it, such as the Standard C++ Library[^]. There are also lots of useful classes in MFC if you are using those libraries; Here[^] is an article that should help you with your sorting problem. In either (or both) case(s) you should get familiar with the MSDN[^] site to help you find answers to many of the problems you will meet on the way.
Just say 'NO' to evaluated arguments for diadic functions! Ash