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

Reading a file using istearm

Hi,

I'm trying to read a file using the istearm class (I cant use ifstream
since the input might be a file or it might be stdin).
istream *input;
// Add checks for file name here, else use input = &cin;

while (!input->eof())
{
char readbuffer[32];
input->getline(readbuffer,32);
// Do something here
}

My problem here is this :

If the line contains less than 32 characters, this works fine and
reads the whole line

However if the line contains more than 32 characters,
i. readbuffer[] contains the first 31 characters of the stream ----
Good
ii. gcount() returns 31 ----Good
iii. tellg() returns -1 ----Not good

As a result of the tellg() function returning -1, subsequent
readline() calls are reading from position -1 from the file and as
such the readline() fails.

My Question :

How can I work around this ?

One possible solution I could think of is maintain a count of the
number of characters read and manually do a seekg() to that position
before every read. However this doesn't sound like a very elegant
solution and not very efficient at all.

Also, is it possible to read an entire line of a file into a string
(or maybe a stringstream?) ? Irresepctive of the the line in the input
stream.

Thx !

- Tushar

(Disclaimer : I know a buffer size of 32 is small and likely to be
inefficient due to the numerous file accesses, but I chose it for
debugging so I can see whats going wrong)
Dec 13 '07 #1
4 1882
On Dec 13, 11:14 am, tushar.sax...@gmail.com wrote:
Hi,

I'm trying to read a file using the istearm class (I cant use ifstream
since the input might be a file or it might be stdin).

istream *input;
// Add checks for file name here, else use input = &cin;

while (!input->eof())
{
char readbuffer[32];
input->getline(readbuffer,32);
// Do something here

}
std::string buf("");
while(getline(input,buf))
{
//do proessing, if any
}

this getline is provided by string. do not forget to include <string>.
hope this works.
>
My problem here is this :

If the line contains less than 32 characters, this works fine and
reads the whole line

However if the line contains more than 32 characters,
i. readbuffer[] contains the first 31 characters of the stream ----Good

ii. gcount() returns 31 ----Good
iii. tellg() returns -1 ----Not good

As a result of the tellg() function returning -1, subsequent
readline() calls are reading from position -1 from the file and as
such the readline() fails.

My Question :

How can I work around this ?

One possible solution I could think of is maintain a count of the
number of characters read and manually do a seekg() to that position
before every read. However this doesn't sound like a very elegant
solution and not very efficient at all.

Also, is it possible to read an entire line of a file into a string
(or maybe a stringstream?) ? Irresepctive of the the line in the input
stream.

Thx !

- Tushar

(Disclaimer : I know a buffer size of 32 is small and likely to be
inefficient due to the numerous file accesses, but I chose it for
debugging so I can see whats going wrong)
Thanks,
Balaji.

Dec 13 '07 #2
Works like a charm. Thx !

On Dec 12, 10:26 pm, kasthurirangan.bal...@gmail.com wrote:
On Dec 13, 11:14 am, tushar.sax...@gmail.com wrote:
Hi,
I'm trying to read a file using the istearm class (I cant use ifstream
since the input might be a file or it might be stdin).
istream *input;
// Add checks for file name here, else use input = &cin;
while (!input->eof())
{
char readbuffer[32];
input->getline(readbuffer,32);
// Do something here
}

std::string buf("");
while(getline(input,buf))
{
//do proessing, if any

}

this getline is provided by string. do not forget to include <string>.
hope this works.


My problem here is this :
If the line contains less than 32 characters, this works fine and
reads the whole line
However if the line contains more than 32 characters,
i. readbuffer[] contains the first 31 characters of the stream ----Good
ii. gcount() returns 31 ----Good
iii. tellg() returns -1 ----Not good
As a result of the tellg() function returning -1, subsequent
readline() calls are reading from position -1 from the file and as
such the readline() fails.
My Question :
How can I work around this ?
One possible solution I could think of is maintain a count of the
number of characters read and manually do a seekg() to that position
before every read. However this doesn't sound like a very elegant
solution and not very efficient at all.
Also, is it possible to read an entire line of a file into a string
(or maybe a stringstream?) ? Irresepctive of the the line in the input
stream.
Thx !
- Tushar
(Disclaimer : I know a buffer size of 32 is small and likely to be
inefficient due to the numerous file accesses, but I chose it for
debugging so I can see whats going wrong)

Thanks,
Balaji.
Dec 13 '07 #3
In message
<23**********************************@b40g2000prf. googlegroups.com>,
ka*******************@gmail.com writes
>On Dec 13, 11:14 am, tushar.sax...@gmail.com wrote:
>Hi,

I'm trying to read a file using the istearm class (I cant use ifstream
since the input might be a file or it might be stdin).

istream *input;
// Add checks for file name here, else use input = &cin;

while (!input->eof())
It's also worth pointing out that this is almost never the right thing
to do. eof() doesn't become true until *after* you have tried (and
failed) to read beyond the end of the file.
>{
char readbuffer[32];
input->getline(readbuffer,32);
// Do something here

}
This is the correct way: note that the test for failure takes place
*after* the call of getline().
>std::string buf("");
while(getline(input,buf))
{
//do proessing, if any
}

this getline is provided by string. do not forget to include <string>.
hope this works.
[snip]

--
Richard Herring
Dec 13 '07 #4
On Dec 13, 11:17 am, Richard Herring <ju**@[127.0.0.1]wrote:
In message
<23801a42-f402-4b4d-a659-a44b36dfd...@b40g2000prf.googlegroups.com>,
kasthurirangan.bal...@gmail.com writes
On Dec 13, 11:14 am, tushar.sax...@gmail.com wrote:
I'm trying to read a file using the istearm class (I cant
use ifstream since the input might be a file or it might be
stdin).
istream *input;
// Add checks for file name here, else use input = &cin;
while (!input->eof())
It's also worth pointing out that this is almost never the
right thing to do. eof() doesn't become true until *after* you
have tried (and failed) to read beyond the end of the file.
Actually, whether it becomes true or not depends. You can't
count on it one way or the other.
{
char readbuffer[32];
input->getline(readbuffer,32);
// Do something here
}
This is the correct way: note that the test for failure takes
place *after* the call of getline().
And it doesn't involve calling eof().
std::string buf("");
while(getline(input,buf))
{
//do proessing, if any
}
this getline is provided by string. do not forget to include
<string>. hope this works.
Note too that in his original code: getline() sets failbit if it
cannot read a line, because the buffer is too small. (This
shouldn't be a problem with the version using std::string,
although in extreme cases, you may get a bad_alloc exception
from it.) And error status is sticky; the error remains (and
all further operations are no-ops) until it is explicitly
cleared.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 13 '07 #5

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

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
1
by: Michael Palmer | last post by:
I'm reading xml from SQL Server 2K with VB.net using an XSD schema file and SQLXML 3.0. I have the below code working fine, but I'd like to change the code from reading the schema file from a...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...
0
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...
0
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,...
0
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...

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.