473,657 Members | 2,427 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 2502

"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
4976
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
3230
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
3383
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
3730
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
6248
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
1471
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
1758
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
14047
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
3220
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
8402
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
8315
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
8734
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
8508
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
7341
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
6172
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
5633
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
4164
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
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.