By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

fstream::get(char&) repeating last character

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
* 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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.