473,394 Members | 1,640 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,394 software developers and data experts.

fstream::get(char&) repeating last character

Hi,
I'm using fstream.get to read in a character from a file, then print it
on the screen. I have a file called test.log that contains "Hello,
World!", but when I try and print the contents out on the screen I get
"Hello, World!!". The following is the code (please ignore all
wxWidgets classes):

void SystemBackupFrm::compress()
{
using namespace std;
const wxString dir = "C:\\Backups\\";

wxFFileOutputStream out(dir + "files.zip");
wxZipOutputStream zip(out);
wxTextOutputStream data(zip);
fstream file1("C:\\test.log", ios::in );
zip.PutNextEntry("C:\\test.log");
char t;
while(file1.good())
{
file1.get(t);
cout << t ;
data << t;
}
file1.close();

}

Cheers,
Kieran

Jul 23 '05 #1
3 4163
* ki****@cyrocom.co.uk:
I'm using fstream.get to read in a character from a file, then print it
on the screen. I have a file called test.log that contains "Hello,
World!", but when I try and print the contents out on the screen I get
"Hello, World!!". The following is the code (please ignore all
wxWidgets classes):

[extranous code removed] void SystemBackupFrm::compress()
{
using namespace std;
fstream file1("C:\\test.log", ios::in );
Strong hint: make that

"c:/test.log"

Windows accepts both forward and backward slash at the API level.
You only need those awkward backward slashes for the user interface,
and then only if you elect to uphold the illusion of backslashisism.
With forward slash you can use a single convention throughout the
C++ code, especially in #include directives.
char t;

while(file1.good())
{
file1.get(t);
cout << t ;
}
file1.close();
}


When 'get' fails you still output the the content of 't', which
hasn't been changed.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Thanks,
I stupidly thought it was something to do with fetching the character
itself. Also thanks for the advice on forward and back slashes,
Cheers,
Kieran

Jul 23 '05 #3
In article <11*********************@g47g2000cwa.googlegroups. com>,
<ki****@cyrocom.co.uk> wrote:

while(file1.good())
{
file1.get(t);
cout << t ;
data << t;
}


good() returns "false" only *after* you have *tried* and *failed* to read
past the end of the file. A correct way to write your input loop is as
follows:

while (file1.get(t))
{
cout << t;
data << t;
}

In a boolean context, get() (like all other standard C++ input operations)
evaluates as "true" if the input succeded, and "false" if it failed.

--
Jon Bell <jt****@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 23 '05 #4

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

Similar topics

1
by: Christopher Reeve | last post by:
Hi, I wonder if anyone could help me. I am moving accross from C to C++ and am writing a program that reads and writes data into a file. I am aware of the old C ways of doing it but am trying to...
5
by: Jacek Dziedzic | last post by:
Hi! Consider the following program #include <fstream> #include <iostream> using namespace std; int main() { ifstream in("test.txt");
7
by: Computer Whizz | last post by:
Hi, I was just wondering if someone would like to comment on these two issues. I had a 15 minute wander around some sites and was curious about loading files (plain ASCII I think will do for a...
9
by: sherifffruitfly | last post by:
Hi, I've a got a little (exercise) program that reads data from a file and puts it into struct members. I run into trouble when one of the data pieces is comprised of several words (eg "john...
6
by: ssetz | last post by:
Hello, For work, I need to write a password filter. The problem is that my C+ + experience is only some practice in school, 10 years ago. I now develop in C# which is completely different to me....
9
by: jraul | last post by:
1) Am I correct that C++ does not have a defined character set? In particular, a platform might not use the ASCII character set? 2) C++ supports wchar_t types. But again, this has no defined...
2
by: beepa | last post by:
As you will be able to see I am fairly new at this. Here is the part I'm having problems figuring out: The file I'm inputing from is formated like this: firstName lastName postion name (one or...
7
by: ramana | last post by:
I'm wondering if someone could point me to the flaw in the following code that uses the while(!FP.eof()) condition to read the input data. This condition is reading the last data point of the file...
3
by: TamaThps | last post by:
I have an array of string values, and I need to access each character of the string individually. For instance I want my char variable to equal the first character in the string, and then compare it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.