473,698 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

infile.get(x)

las
I'm having a wee problem with the get method, here is my code :
ifstream infile;
char x;
infile.open("te mp.txt");
if( !infile.good() )
{
cout << "Error opening file" << endl;
system("PAUSE") ;
exit(1);
}

while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}

The file temp.txt contains one word - "beer". However when I run the above
code I get BEERR (notice the two R's at the end). What gives ?

Thanks for any help.
Jul 19 '05 #1
7 12085
las wrote in news:I1******** ***********@new s4.e.nsc.no:
I'm having a wee problem with the get method, here is my code :
ifstream infile;
char x;
infile.open("te mp.txt");
if( !infile.good() )
{
cout << "Error opening file" << endl;
system("PAUSE") ;
exit(1);
}

while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}

The file temp.txt contains one word - "beer". However when I run the
above code I get BEERR (notice the two R's at the end). What gives ?


you mean: B E E R R

eof() doesn't get set until you try to read a char beyond the
EOF. Unfortunatly eof() is an after the event error not a before the
event warning.

try:

while ( infile.get( x ) )
{
cout << toupper( x ) << "\t";
}

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #2
upon executing your code i got
B E E R
in fact i think thats a sweet idea.....

#include<iostre am>
#include<fstrea m>
using namespace std;

int main()
{
ifstream infile;
char x;
infile.open("te mp.txt");
if( !infile.good() )
{
cout << "Error opening file" << endl;
system("PAUSE") ;
exit(1);
}

while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}

return 0;
}

"las" <la******@onlin e.no> wrote in message
news:I1******** ***********@new s4.e.nsc.no...
I'm having a wee problem with the get method, here is my code :
ifstream infile;
char x;
infile.open("te mp.txt");
if( !infile.good() )
{
cout << "Error opening file" << endl;
system("PAUSE") ;
exit(1);
}

while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}

The file temp.txt contains one word - "beer". However when I run the above
code I get BEERR (notice the two R's at the end). What gives ?

Thanks for any help.

Jul 19 '05 #3


Spectre wrote:

upon executing your code i got
B E E R
But that's only your system :-)
Other systems may behave differently.
in fact i think thats a sweet idea.....

#include<iostre am>
#include<fstrea m>
using namespace std;

int main()
{
ifstream infile;
char x;
infile.open("te mp.txt");
if( !infile.good() )
{
cout << "Error opening file" << endl;
system("PAUSE") ;
exit(1);
}

while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}


eof gets true only *after* you have *tried* and *failed*
to read from the stream.

the program enters the while loop and processes 'b' 'e' and 'e'.
The next time the loop is entered the program reads the next
character: 'r'. The character is converted to upper case and
sent to cout. eof is tested again and returns false! Remember:
you need to try and fail to read from the stream.
Thus the loop is entered again and an attempt to read from
infile is made. But this time the attempt fails, since there
is nothing more in the stream that could be read. Yet your
loop body continues as if nothing has happend and converts
and outputs x. Only after that, eof will return true.
End effect: You looped one to many through the loop and
try to process a character, whose read operation from infile
has failed.

Whenever you see a loop

while( somestream.eof( ) )
{
read from somestream
process
}

then almost always, this is wrong. Most programmer think
this way:

as long as I have not reached the end of the file
{
read frm the file
process what has been read
}

This may work in other programming lanquages, but not in C++.
In C++ one, correct, way to think is:

as long as I can read from the file
{
process what has been read
}

why did the read fail? Was it because eof?
If yes, then everything is OK. The stream was
read completely.
or in code:

while( infile.get( x ) )
{
x = toupper( x );
cout << x << '\t';
}

if( !infile.eof() )
{
cout << "Read operation failed for unknown reasons\n";
...
}

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #4
"las" <la******@onlin e.no> wrote:
while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}


You shall check whether the read from the file was successful: the EOF flag
is guaranteed to be set only *after* you attempted to read past the end of
file. It does not make sense to set it prior to the extraction because it
is unknown how many characters you will expect.

BTW, note that 'toupper()' takes only values representable as 'unsigned
char' but the type 'char' may be signed. You probably want to do something
like this:

while (infile.get(x))
std::cout << std::toupper(st atic_cast<unsig ned char>(x)) << '\t';
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>
Jul 19 '05 #5
tom_usenet wrote:
...can only take the values EOF (-1)...


Unless I'm mistaken, EOF is only guaranteed to be a negative int value,
so it may or may not be -1. At least I am quite sure this is the case in
C, and I can't find anything in the C++ standard that changes it.

-Kevin

Jul 19 '05 #6
In article <3f2817e6@shkne ws01>,
Kevin Goodsell <us************ *********@never box.com> wrote:
tom_usenet wrote:
...can only take the values EOF (-1)...


Unless I'm mistaken, EOF is only guaranteed to be a negative int value,
so it may or may not be -1. At least I am quite sure this is the case in
C, and I can't find anything in the C++ standard that changes it.


That's right (for both C and C++).
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 19 '05 #7
las wrote:
I'm having a wee problem with the get method, here is my code :
ifstream infile;
char x;
infile.open("te mp.txt");
if( !infile.good() )
{
cout << "Error opening file" << endl;
system("PAUSE") ;
exit(1);
}

while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}

The file temp.txt contains one word - "beer". However when I run the above
code I get BEERR (notice the two R's at the end). What gives ?

Thanks for any help.


A few additional notes:

Please post complete code when you ask a question. We should be able to
copy, paste, and compile if we need to.

Errors should usually be written to std::cerr, not std::cout.

system("PAUSE") is not portable, and is probably only useful for
debugging anyway. There are probably other ways you could accomplish
what you want, that don't require adding an extra function call before
every exit() call (actually, using exit() isn't necessarily a good idea
anyway - throwing an exception that is caught in main() is probably better).

exit(1) is not portable. There are 3 return values for your program that
have defined meanings according to the standard. They are 0,
EXIT_SUCCESS, and EXIT_FAILURE. The first two have the same meaning, and
may or may not have the same value.

-Kevin

Jul 19 '05 #8

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

Similar topics

14
9743
by: Bruce A. Julseth | last post by:
When I execute this SQL statement in my PHP code, I get an error "File '.\Address.txt' not found (Errcode: 2)" $File = addslashes(".\Address.txt"); $SQL = "Load Data InFile \"" . $File . "\" into table addresses"; $result = mysql_query($SQL) or die(mysql_error()); The file is located in the same directory as my .PHP file. How do I generate a relative address for this file so that it can be found?
2
6339
by: Pieter Van Waeyenberge | last post by:
Hello i *had* it working ... i have everything in place as all documentation and fora stated.. but yet i AGAIN get the error: "The used command is not allowed with this MySQL version" in /home/foo/public_html/goo/import.load.items.php on line.. LOAD DATA LOW_PRIORITY LOCAL INFILE '/home/foo/public_html/goo/import/file.txt' REPLACE INTO TABLE discipline
0
6684
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to be able to place the PHP file on the server, and I guess you probably can't do that either. Talk about catch 22... The only other way I can think of is to install MySQL on a machine you control, then import the data there using the method I...
1
4068
by: Jami Bradley | last post by:
HI all, For the past several months we have been using LOAD DATA LOCAL INFILE to bulk load tables within Perl modules. Recently, someone thought it would be a good idea to upgrade the Solaris machines - I'm thinking it wasn't that good an idea :-( We are now running MySQL 4.0.18 and the DBI version is 1.35. Now, our scripts are failing with the dreaded "The used command is not allowed with this MySQL version" message. Here's what I...
0
1648
by: Steffen | last post by:
Hello, I´m using mysql 3.23.58 and I want to enable the local-infile option. The manual on dev.mysql.com says the following to that: ..... If you use LOAD DATA LOCAL in Perl scripts or other programs that read the group from option files, you can add the local-infile=1 option to that group. However, to keep this from causing problems for programs that do not understand local-infile, specify it using the
1
16072
by: Ray in HK | last post by:
What are the differences between LOAD DATA INFILE and LOAD DATA LOCAL INFILE ? I found some web hosting company do not allow using LOAD DATA INFILE but allow LOAD DATA LOCAL INFILE. The reason is for the sake of security. What does that mean ?
2
4294
by: Jason3231 | last post by:
well i've found and pieced together a script (mainly found; i'm very new to perl) to scan a group of cisco switches that i have and print information out in a text file. everything seems to work pretty well except when a particular cmd doesn't apply to one of the switches listed in my infile. the script seems to get stuck in these instances and closes without going to the next valid switch. maybe i need some sort of ELSE type of statement or...
15
4865
by: jjh | last post by:
So with infile I have this so far: #define MAXBOOKS 9 ifstream infile("library.txt"); struct Book { title } while(true) {
15
2996
by: waltbrad | last post by:
Hello. I'm studying the book "C++ Primer Plus" by Stephan Prata. In chapter 6 he gives an exercise that reads from a file. The list is thus: 4 Sam Stone 2000 Freida Flass 100500 Tammy Tubbs
0
8674
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
9157
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
9028
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7728
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...
0
5860
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
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...
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2001
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.