473,471 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

istream acting weird on windows

92 New Member
I have some code that does two passes in a file. Each pass opens and closes the file itself, so no problem there. The first pass reads the first and last valid lines, whereas the second pass reads the entire file.

The problem is in the first pass. I have the following loop:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.     file.exceptions (ios_base::badbit);
  3.     file.getline (buffer, MAX_BUFFER_SIZE - 1);
  4.     while (!file.eof () && !m_validation_func (buffer, num_fields))
  5.         file.getline (buffer, MAX_BUFFER_SIZE - 1);
  6.  
  7.     // Some code to read the data from 'buffer;
  8.     if (file.eof ())
  9.         return (false);
  10.  
  11.     // Seek the last valid line in the file
  12.     file.seekg (-1, ios::end);
  13.     char c = file.peek ();
  14. }
  15. catch (...) {
  16. if (file.fail ())
  17.     printf ("FAIL!\n");
  18. if (file.bad ())
  19.     printf ("BAD!\n");
  20. if (file.eof ())
  21.     printf ("EOF!\n);
  22. }
  23.  
There is more code after this, but it fails here. peek throws an exception. In my catch block I check for bad () and fail () and eof () they are false. I know its the peek since I have some debug printfs in there. To make it even odder, if I compile with debug flags it works properly.

Any ideas whats going on? Also the algorithm is a bit shoddy since it reads character by character. Anyone have a good algorithm for using getline to read a file in reverse order?
Jul 17 '07 #1
2 1457
weaknessforcats
9,208 Recognized Expert Moderator Expert
You might try reading the file into an array of strings and then reading the array from the last element to the first.

Also, your try block covers too much code. Reduce the number of lines you want to try.

Also, bad(), eof() and fail() are not exception conditions. No exception is thrown on these.

STL throws exception objects. That means you can catch the object and call the what() method to see what the problem is:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.      //something
  4. }
  5. catch (exception& ex)
  6. {
  7.     cout << ex.what() << endl;
  8.     return;   //bail out
  9. }
  10.  
Jul 17 '07 #2
Benny the Guard
92 New Member
By calling file.exceptions (ios_base::badbit); it can throw exceptions. In fact it should only throw when badbit is set. The reason I have this code is because I do not want to read the entire file in. I want to spot check the first and last line to see if its worth reading the rest in. By reading it all in I would kill the memory (5GB file in some cases) and if I read line by line it takes awhile.

Anyhow, my update is I finally got it to work. We use the Intel C++ Compiler v 9.0 which works with MS .NET 2003 (and 2005). Well there are the compile flags "/Qip /Ob2" that are optimizations to inline functionality. It tries to be smart and figure out code it can inline for you. Problem is apparently it messes up ifstream in some cases, in our version. But if we upgrade it works. I love compiler bugs. But its running like a charm now. Thanks for the help.
Jul 18 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jason K | last post by:
Let me preface this by saying this obviously isn't a C++ *language* issue per se; rather probably an issue relating to quality of implementation, unless I'm just misusing iostream... I wrote a...
4
by: Aaron Gray | last post by:
istream does not seem to be behaving logically. ..eof() or .peek() == EOF is being issued on a double CR. So basically the app I am working on bombs out on a blank line of input. Is this...
3
by: Andreas Hildebrandt | last post by:
Hi all, we are trying to port a fairly large c++ library to windows using Visual Studio.net 2003, and are running into some unexpected problems with the stream handling. Since our development is...
5
by: David | last post by:
My asp.net app is working perfectly in WinXP Pro. However when I moved it to IIS running on win2003 a lot of functionality just don't work. I don't even get any error just no response or queries...
13
by: Gianni Mariani | last post by:
What I would like to do is read bytes from a stream, any number and any time. I would like it to wait until there are any bytes to read. I want the exact same functionality as cstdio's "fread"...
3
by: KWienhold | last post by:
I'm currently writing an application (using Visual Studio 2003 SP1 and C#) that stores files and additional information in a single compound file using IStorage/IStream. Since files in a compound...
1
by: St33med | last post by:
Not like it has a virus (in which I doubt because I have McAfee), but, when I shutdown, it tells me a program cannot write to a specific sector, mostly Windows programs. Plus, whenever I get back on...
1
by: Merrigan | last post by:
Hi All, I have written a little script to upload some files to an ftp folder. The problem is as follows : I wrote the script on my windows laptop, and I want to run it from mylinux server....
21
by: Peter Larsen [] | last post by:
Hi, I have a problem using System.Runtime.InteropServices.ComTypes.IStream. Sample using the Read() method: if (IStreamObject != null) { IntPtr readBytes = IntPtr.Zero;...
2
by: dohboy | last post by:
a kinda newbie here. I've done a simple little program that reads a text file and counts the number of lines and words. I had a heck of a time getting it to count properly when I finally discovered...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
1
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...
0
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...
0
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,...
1
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...
0
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...
0
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...
0
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 ...

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.