Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, October 3, 2011 9:04 AM
anyone know how to delete a certain character in a text file??
I need to delete the last character of a text file.
keong
All replies (27)
Tuesday, October 4, 2011 5:22 AM ✅Answered
char str[] = sContent;<--tis line..how to convert string to char array?
>char str[] = sContent;
Drop that line.
>outFile.write(str,strlen(str));
Change this to:
outFile.write(sContent.c_str(),sContent.length());
- Wayne
Tuesday, October 4, 2011 6:07 AM ✅Answered
>the aligment of the text file seem gone ady..
See if it improves with this change:
while(file_op.getline(cContent,3000))
{
sContent += cContent;
sContent += '\n'; // <<==
}
- Wayne
Monday, October 3, 2011 10:57 AM
Open the file with CreateFile. Determine the length of the file using GetFileSize (or GetFileSizeEx for very long texts). Then subtract one, call SetFilePointer (or SetFilePointerEx) and truncate the file using SetEndOfFile. Finally call CloseHandle.
Monday, October 3, 2011 12:04 PM
1. Read file with std::ifstream;
2. Use std::getline to write file contents in std::string;
3. Delete last character from the string using method erase():
str1.erase(str1.end()-1);
4. Write the string in output file with std::ofstream.
Monday, October 3, 2011 8:11 PM
>I need to delete the last character of a text file.
Also see:
_chsize
http://msdn.microsoft.com/en-us/library/dk925tyb%28it-it%29.aspx
- Wayne
Tuesday, October 4, 2011 3:37 AM
hi,
Can you Provide some source code?
I really cant get it although I search few hours online.
Thanks in advance.
keong
Tuesday, October 4, 2011 3:40 AM
hi,
Can you Provide some source code?
I really cant get it although I search few hours online.
Thanks in advance.
keong
Better yet, why don't you show us what you have coded so far? You will learn more that way.
Tuesday, October 4, 2011 3:52 AM
something like tis..
CString filepath = "C:\testing.txt";
char cContent[3000];
string sContent = "";
fstream file_op(filepath,ios::in);
while(file_op.getline(cContent,3000))
{
sContent += cContent;
}
file_op.close();
sContent.erase(sContent.end()-1);
char str[] = sContent;
ofstream outFile;
outFile.open(filepath);
outFile.write(str,strlen(str));
outFile.close();
keong
Tuesday, October 4, 2011 3:54 AM
i am trying to use Sergey Chepurin method to code it..keong
Tuesday, October 4, 2011 4:03 AM
while(file_op.getline(cContent,3000))
{
sContent += cContent;
}
What happens if cContent doesn't end in a null?
This section of your code needs to be rewritten.
Tuesday, October 4, 2011 4:04 AM
i am trying to use Sergey Chepurin method to code it.. keong
Never heard of this method. A reference would be useful if you are alluding to something specific.
Tuesday, October 4, 2011 4:07 AM
while(file_op.getline(cContent,3000))
{
sContent += cContent;
}What happens if cContent doesn't end in a null?
This section of your code needs to be rewritten.
any suggestion how to rewrite it??
another problem is...
char str[] = sContent;<--tis line..how to convert string to char array?
keong
Tuesday, October 4, 2011 5:42 AM
char str[] = sContent;<--tis line..how to convert string to char array?
>char str[] = sContent;
Drop that line.
>outFile.write(str,strlen(str));
Change this to:
outFile.write(sContent.c_str(),sContent.length());
- Wayne
thx, i get it..but the aligment of the text file seem gone ady..keong
Tuesday, October 4, 2011 6:11 AM
This is I/O chapter from a good C++ tutorial to consult with:
http://www.cplusplus.com/doc/tutorial/files/
Make it:
ifstream myfile;
myfile.open("c:\\temporary\\readme.txt", ios::binary);
string line;
string readIn;
while (!myfile.eof())
{
std::getline(myfile,line);
readIn+=line;
}
readIn.erase(readIn.end()-1);
Tuesday, October 4, 2011 6:18 AM
>the aligment of the text file seem gone ady..
See if it improves with this change:
while(file_op.getline(cContent,3000))
{
sContent += cContent;
sContent += '\n'; // <<==
}
- Wayne
thx...it is work for me~:Dkeong
Tuesday, October 4, 2011 6:58 AM
>it is work for me
Good. Note that you should make your program more robust
by checking for errors after every I/O operation. That
means check to see if the file opens were successful -
don't just assume that they will always work. Also
check for error or eof when the getline loop exits,
which will happen when the failbit is set for the file.
- Wayne
Tuesday, October 4, 2011 7:01 AM
Sergey wrote:
>while (!myfile.eof())
> {
> std::getline(myfile,line);
> readIn+=line;
> }
No, that's bad code. The eof flag gets set the first
time an attempt is made to read *past* the end of the
data in the file. This code will not work properly
if there is a newline at the end of the last line
in the file, but will if there is no newline there.
If there is a newline after the last line, then the
getline which reads the last line will stop after
it reads that newline and removes it from the input
stream. At that point the eof flag is not yet set.
The next getline will fail and set the failbit
because there is no data for it to retrieve. It will
also set the eof bit/flag at the same time.
The above code also doesn't test for errors when reading
the file. The getline may fail on errors reading from
the file. Proper code should test for failure of the
getline and if the failbit is set *then* check to see
if the eof bit is also set. If it is then the end of
file has been reached successfully.
The easiest way to do these tests is like this:
while(std::getline(myfile,line))
{
// process line
}
if(!myfile.eof())
{
// an error occurred reading the file!
// take appropriate action
}
// else OK eof was reached
>myfile.open("c:\temporary\readme.txt", ios::binary);
This is also problematic with getline and a "text" file
with newline pairs in it: 0x0D 0x0A
The output file will wind up with just 0x0D at the end of
lines. The resultant file will be shorter than the input
by as many bytes as there are newlines in the input.
- Wayne
Tuesday, October 4, 2011 7:03 AM
>it is work for me
Good. Note that you should make your program more robust
by checking for errors after every I/O operation. That
means check to see if the file opens were successful -
don't just assume that they will always work. Also
check for error or eof when the getline loop exits,
which will happen when the failbit is set for the file.
- Wayne
thx for ur advise~~:D
I am still new for C++..a long journey to go...
keong
Tuesday, October 4, 2011 8:45 AM
Sergey wrote:
>while (!myfile.eof())
> {
> std::getline(myfile,line);
> readIn+=line;
> }No, that's bad code. The eof flag gets set the first
time an attempt is made to read *past* the end of the
data in the file. This code will not work properly
if there is a newline at the end of the last line
in the file, but will if there is no newline there.If there is a newline after the last line, then the
getline which reads the last line will stop after
it reads that newline and removes it from the input
stream. At that point the eof flag is not yet set.The next getline will fail and set the failbit
because there is no data for it to retrieve. It will
also set the eof bit/flag at the same time.The above code also doesn't test for errors when reading
the file. The getline may fail on errors reading from
the file. Proper code should test for failure of the
getline and if the failbit is set *then* check to see
if the eof bit is also set. If it is then the end of
file has been reached successfully.The easiest way to do these tests is like this:
while(std::getline(myfile,line))
{
// process line
}if(!myfile.eof())
{
// an error occurred reading the file!
// take appropriate action
}// else OK eof was reached
>myfile.open("c:\temporary\readme.txt", ios::binary);
This is also problematic with getline and a "text" file
with newline pairs in it: 0x0D 0x0A
The output file will wind up with just 0x0D at the end of
lines. The resultant file will be shorter than the input
by as many bytes as there are newlines in the input.
- Wayne
This is a good code, because this code works. This is a sample code which implies that-
1. it has to be checked and tested first;
2. error check has to be added;
3. leaves for a person something to improve by himself (i added the source to read on the topic).
There are many ways this functionality can be implemented and this sample is one of them.
Tuesday, October 4, 2011 8:55 AM
>This is a good cod, because this code works.
Define "works". It certainly *doesn't* work correctly.
>There are many ways this functionality can be implemented
>this sample is one of them.
One of the wrong ways.
- Wayne
Tuesday, October 4, 2011 9:38 AM
>the aligment of the text file seem gone ady..
See if it improves with this change:
while(file_op.getline(cContent,3000))
{
sContent += cContent;
sContent += '\n'; // <<==
}
- Wayne
This is what you propose as a solution? With magic number "3000" and no error handling even possible. That is it.
Tuesday, October 4, 2011 9:46 AM
errr...harlo bro...calm down...
sorry is my fault to ask this question...>.<
keong
Tuesday, October 4, 2011 10:08 AM
errr...harlo bro...calm down...
sorry is my fault to ask this question...>.<
keong
It is what this forum for - to ask a question and to find an answer. It is even better for you because you will have to decide by yourself who is right, and what to choose. I guess, this task you can solve by yourself.
Tuesday, October 4, 2011 10:10 AM
>This is what you propose as a solution?
Wake up and smell the coffee. The code as written was the
OP's attempt to use *your* method. I never offered any code
fragments as you have done - I made suggestions to overcome
specific problems the OP posted. My suggestions worked -
correctly. At no time was my "mandate" to completely debug
or rewrite the OP's code. The 3000 and other details are
not my suggestion, as should be clear to anyone who looked
at the before and after code. My sole suggestion in that
post was to add the newline to the string, in order to
correct "the alignment of the text file".
Is this a diversionary tactic to avoid acknowledging the
problems with the code you posted?
Giving a neophyte programmer code which has bugs, not
alerting the recipient that the bugs exist, and saying:
"Make it: ... (like this)" is not exactly helpful.
Neither is refusing to test your own code and to admit
that it's defective. It's just blind luck and not good
programming that the erroneous assumption that the getline
will always succeed had no effect on the output in this
case. Since getline clears the receiving string - gives it
a length of zero - when it fails, the extra concatenation
attempt effectively does nothing. But put any other code
after the getline and you might not be so lucky.
No such luck saved your other mistake from compromising
the output. Using getline with a "text" file opened in
binary mode drops characters from the file, as a simple
comparison of the file sizes before and after copying
will easily reveal. I don't call that code that "works".
- Wayne
Tuesday, October 4, 2011 10:16 AM
Last reply to you - this code has NO bugs. You introduce these bugs (mostly imaginable) to prove the wrong. With error handling added and minor corrections (if the programmer wants) it can be used.
Tuesday, October 4, 2011 10:25 AM
>this code has NO bugs.
Amazing. Even after I described them in detail for you,
you still don't get it.
>You introduce these bugs
No - *you* introduced the bugs when you posted the code.
>(mostly imaginable)
All are empirically verifiable, for anyone with the skill
and objectivity to do so.
- Wayne
Tuesday, October 4, 2011 3:37 PM
It is what this forum for - to ask a question and to find an answer. It is even better for you because you will have to decide by yourself who is right...
I've decided. Wayne is right. Your code is buggy, Sergey.