473,795 Members | 2,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fstream::get(ch ar&) 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\\" ;

wxFFileOutputSt ream out(dir + "files.zip" );
wxZipOutputStre am zip(out);
wxTextOutputStr eam data(zip);
fstream file1("C:\\test .log", ios::in );
zip.PutNextEntr y("C:\\test.log ");
char t;
while(file1.goo d())
{
file1.get(t);
cout << t ;
data << t;
}
file1.close();

}

Cheers,
Kieran

Jul 23 '05 #1
3 4194
* 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.goo d())
{
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************ *********@g47g2 000cwa.googlegr oups.com>,
<ki****@cyrocom .co.uk> wrote:

while(file1.goo d())
{
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
4009
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 use the C++ functions which don't seem to be working propperly. I need to open the file for reading and writing because I want to search for a possition in a file called settings.dat and then write a number after a certain chatacter. However when...
5
6036
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
2875
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 beginner right now). So I looked into the fstream library file. I saved 5 lines to the file: testing, testing this is
9
2430
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 doe", with a space in it). For console input, cin.getline(var, howMuchIWant) or cin.get() has done the trick for me in the past. It doesn't seem to work for me nearly so well with a file stream. I wouldn't have thought cpp regarded
6
4068
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. But, the password filter needs to be built, so I'm doing my best. First of all, I am creating an xml string that contains both username and password, and then I want to write the ascii values for all characters to a textfile. By using the...
9
2927
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 character set? For instance, it might not be a unicode character set?
2
455
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 two words) firstName lastName first base (for example) firstName lastName position name (one or two words)
7
4011
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 twice. #include <iostream> #include <fstream> using namespace std; int main(int argc, char **argv) { double x;
3
2582
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 to character values using if statements. Later in the program I will need to not only access the first character but all of them individually in each string. How can I set that up? Here is my code for trying to have the charcter variable equal...
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.