473,857 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple ifstream question

OK this is really simple but I can't think what is the best
and most elegant solution.

Say I am getting an int from an ifstream as follows:

int val;
inFile.get(rein terpret_cast<ch ar*>(&val), sizeof(val));

How do I know if this succeeded? It only returns false if
the eof is at the end of the int.
Jul 22 '05 #1
11 2518

"flips" <fl***@flops.co mm> wrote in message
news:bp******** **@news.freedom 2surf.net...
OK this is really simple but I can't think what is the best
and most elegant solution.

Say I am getting an int from an ifstream as follows:

int val;
inFile.get(rein terpret_cast<ch ar*>(&val), sizeof(val));

How do I know if this succeeded? It only returns false if
the eof is at the end of the int.


if(!inFile && !inFile.eof())
/* error */

-Mike
Jul 22 '05 #2
Are you mad?

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:eA******** **********@news read1.news.pas. earthlink.net.. .

"flips" <fl***@flops.co mm> wrote in message
news:bp******** **@news.freedom 2surf.net...
OK this is really simple but I can't think what is the best
and most elegant solution.

Say I am getting an int from an ifstream as follows:

int val;
inFile.get(rein terpret_cast<ch ar*>(&val), sizeof(val));

How do I know if this succeeded? It only returns false if
the eof is at the end of the int.


if(!inFile && !inFile.eof())
/* error */

-Mike

Jul 22 '05 #3

"flips" <fl***@flops.co mm> wrote in message
news:bp******** **@news.freedom 2surf.net...
Are you mad?


Not at all. If what I wrote doesn't help,
perhaps you should express your problem more
clearly.

BTW Please don't top post.

-Mike
Jul 22 '05 #4
"Mike Wahler" <mk******@mkwah ler.net> wrote:

if(!inFile && !inFile.eof())
/* error */


You clearly must have meant:

if(!inFile || inFile.eof())
/* error */

What you wrote isn't meaningful...
(If "!inFile" is true then "!inFile.eo f()" willl yield a null pointer
exception!)

/Andreas
Jul 22 '05 #5
"Andreas Kirkeskov Carlsen" <ak*@daimi.au.d k> wrote in message
news:bp******** **@news.net.uni-c.dk...
"Mike Wahler" <mk******@mkwah ler.net> wrote:

if(!inFile && !inFile.eof())
/* error */


You clearly must have meant:

if(!inFile || inFile.eof())
/* error */

What you wrote isn't meaningful...
(If "!inFile" is true then "!inFile.eo f()" willl yield a null pointer
exception!)


!?!?!

Null pointer exception? inFile isn't even a pointer.
Jul 22 '05 #6
if(!inFile && !inFile.eof())
/* error */

why do you type (!inFile && !inFile.eof())
I do not understand the purpose of (!inFile)
I thought that (!inFile.eof()) is enough
Jul 22 '05 #7
"TaiwanNoWh ere" <ta***********@ netscape.net> wrote in message
news:16******** *************** ***@posting.goo gle.com...
if(!inFile && !inFile.eof())
/* error */

why do you type (!inFile && !inFile.eof())
I do not understand the purpose of (!inFile)
I thought that (!inFile.eof()) is enough


Quite.

Anyway, the point of my original post was that, if the file is
say 3 bytes long, and I read 4 (to read an int), no error occurs.

inFile is true AND inFile.eof() is false.

Is this the same on other people's compilers? Perhaps I need
to update the verion of STL I'm using.
Jul 22 '05 #8

"flips" <fl***@flops.co mm> wrote in message
news:bp******** **@news.freedom 2surf.net...
"TaiwanNoWh ere" <ta***********@ netscape.net> wrote in message
news:16******** *************** ***@posting.goo gle.com...
if(!inFile && !inFile.eof())
/* error */

why do you type (!inFile && !inFile.eof())
I do not understand the purpose of (!inFile)
I thought that (!inFile.eof()) is enough


Quite.

Anyway, the point of my original post was that, if the file is
say 3 bytes long, and I read 4 (to read an int), no error occurs.

inFile is true AND inFile.eof() is false.

Is this the same on other people's compilers? Perhaps I need
to update the verion of STL I'm using.


Have you tried inFile.good()?

Tom
Jul 22 '05 #9
flips escribió:
Anyway, the point of my original post was that, if the file is
say 3 bytes long, and I read 4 (to read an int), no error occurs.

inFile is true AND inFile.eof() is false.


inFile.gcount () tell you the number of char readed.

Regards.
Jul 22 '05 #10

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

Similar topics

5
4990
by: David Sobey | last post by:
Hi Sorry bout this basic prob. Got a file called file.obj. tryna read the first line from it as a string and print it to the screen. getting errors: #include "stdafx.h" #include <stdio.h> #include <fstream.h> #include <iostream.h> #include <string>
7
3246
by: Anton Ishmurzin | last post by:
Greetings All, I think everybodyknows the answer already. But i am quite a newbie in c++. I've got the following line in my code: ifstream ini_file_in("filename.dat", ios::in); But, the gcc (GCC) 3.2 spits out some stuff about
6
3401
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber" 10,20,30
4
3743
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream in("in.txt"); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";}
1
6259
by: inkapyrite | last post by:
Hi all. I'm using ifstream to read from a named pipe but i've encountered an annoying problem. For some reason, the program blocks on reading an ifstream's internal buffer that's only half-filled. Only when the buffer becomes full does it resume execution. Here's my test code for reading from a pipe: //(compiled with g++ -std=c++98) //--------------------------------------------- #include <iostream>
3
1480
by: JustSomeGuy | last post by:
I have written a C methods to support a medical imaging file format. I wish to port this class to C++. I think it should inherit from ifstream. That is I want to define a class called d3p10_stream. class d3p10_stream : public ifstream { private:
2
1773
by: Tomás | last post by:
Up until lately I've been writing all mad kinds of code for accomplishing things, but lately I've decided to lean more toward the whole readability, etc. side of things. I have a command line application, which I intend to be used like so: unicon.exe -8to32 source.txt target.txt There are three command line arguments. The first is something like:
2
14119
by: Karl | last post by:
Hey everyone! I've got a quick question on whether std::ifstream is buffered or not. The reason is that I have a homework assignment that requires me to benchmark copying files using different buffer sizes. I ended up doing this using std::istream::readsome() and std::ostream::write(): // now try writing to the destination file std::ifstream sourceStream(sourceFilename.c_str(), std::ios::binary);
7
3237
by: Boltar | last post by:
Hi I'm using ifstream (which I hardly ever use) to read an ascii text file (containing numbers delimited by newlines) in a loop using something like: ifstream infile("filename") int value; infile >value;
0
9923
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
11083
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
10711
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
10808
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,...
1
7940
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
5774
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
5971
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4592
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
2
4191
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.