473,408 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Weird thing with fstream

When I used fstream to handle a file, I met with a very weird thing:

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
fstream outfile;

outfile.open("test.txt", ios_base::in | ios_base::out);
if (!outfile)
{
cout << "open file fail!\n";
return -1;
}

//display original text
char ch;
while (outfile.get(ch))
{
cout << ch;
}
cout << '\n';
outfile.clear(); //clear eofbit

//seek to end of the text
outfile.seekp(0, ios_base::end);
//get input from the user and append it to outfile
while (cin.get(ch))
{
outfile.put(ch);
}

outfile.close();
return 0;
}

the content of test.txt is:

This is a text
only for test.

But after I ran the program, it turned out to be:

ThThis is a text
only for test.
abcdefg

The "abcdefg" was the input. But, where did the additional "Th" come
from?

What's wrong with my program?

Compiler: gcc3.2.3
System: Windows2000
Compile option: -ansi -Wall
Jul 19 '05 #1
1 2026
gukn9700 wrote:
When I used fstream to handle a file, I met with a very weird thing:

[code snipped]

the content of test.txt is:

This is a text
only for test.

But after I ran the program, it turned out to be:

ThThis is a text
only for test.
abcdefg

The "abcdefg" was the input. But, where did the additional "Th" come
from?

What's wrong with my program?

Compiler: gcc3.2.3
System: Windows2000
Compile option: -ansi -Wall


Hmmm... That's odd. Your program looks OK to me, and it works as
expected when compiled with G++ 3.3 on a Red Hat 9 Linux box.

Just out of curiosity, what happens if you specify the 'binary' mode
flag when the program opens the text file, i.e.,

outfile.open ("test.txt", ios_base::out | ios_base::binary);

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Maximus | last post by:
Hi, I'm not verry good at c++ but I was asking myself this question... Could it be possible to store a variable into a file and load it back into the same variable? What I would like to do is save...
5
by: Jim Strathmeyer | last post by:
So I'm having some weird problems with file output. If I try to boil this problem down to a small, simple program to just show what problems I'm having, I can't get the same problematic behavior. I...
1
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
8
by: nickyeng | last post by:
I have written 3 files, i dont know whether i do it correctly or wrongly but somehow it compiled well and can run. My simple aim is to display the terrain.txt file into the terrain array, and then...
5
by: eagerlearner | last post by:
When I open .html file i got a few weird characters printed out which preceding the content inside the .html file. But when I have the same file content with .txt extension, it has not problem....
2
by: carlos123 | last post by:
im pretty new to java. this might sound like a weird question but! how do i add this code class FileWrite { public static void main(String args) { try{ // Create file ...
2
by: brixton | last post by:
Hello, I've got the following code: wxString path = filepath; wxString newpath = filepath; fstream f(path.Append("/tests/tests.bin"), ios::in | ios::binary); fstream...
2
by: TamaThps | last post by:
Hi, I'm using visual studio 2008 and normally when I get an error it shows what line it is on and which file etc. The error I'm getting I don't know how to solve or even what the problem is. This...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.