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

infile.get(x)

las
I'm having a wee problem with the get method, here is my code :
ifstream infile;
char x;
infile.open("temp.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 12042
las wrote in news:I1*******************@news4.e.nsc.no:
I'm having a wee problem with the get method, here is my code :
ifstream infile;
char x;
infile.open("temp.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<iostream>
#include<fstream>
using namespace std;

int main()
{
ifstream infile;
char x;
infile.open("temp.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******@online.no> wrote in message
news:I1*******************@news4.e.nsc.no...
I'm having a wee problem with the get method, here is my code :
ifstream infile;
char x;
infile.open("temp.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<iostream>
#include<fstream>
using namespace std;

int main()
{
ifstream infile;
char x;
infile.open("temp.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******@online.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(static_cast<unsigned char>(x)) << '\t';
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
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@shknews01>,
Kevin Goodsell <us*********************@neverbox.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("temp.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
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 . "\"...
2
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...
0
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...
1
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...
0
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...
1
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...
2
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...
15
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
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.