Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > fstream::get(char&) repeating last character

Reply
Thread Tools

fstream::get(char&) repeating last character

 
 
kieran@cyrocom.co.uk
Guest
Posts: n/a
 
      05-29-2005
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-29-2005
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> 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?
 
Reply With Quote
 
 
 
 
kieran@cyrocom.co.uk
Guest
Posts: n/a
 
      05-29-2005
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

 
Reply With Quote
 
Jon Bell
Guest
Posts: n/a
 
      05-29-2005
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> 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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace the last (and only last) character in a string? Johny Python 8 05-03-2007 09:24 PM
remove the last character or the newline character? Daniel Mark Python 6 09-28-2006 02:40 PM
character encoding +missing character sequence raavi Java 2 03-02-2006 05:01 AM
getting the character code of a character in a string Velvet ASP .Net 9 01-19-2006 09:27 PM
Javascript: filter repeating character... Joseph Javascript 6 11-07-2004 05:48 AM



Advertisments