472,328 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Reading from a text file that also is binary

Regarding the following code i have a problem.

void read () {
fstream file;
ios::open_mode opMode = ios::in;
file.open ("some.txt", opMode);
char *ch = new char[1];
vector <charv;
while (!file.eof ()) {
do {
file.read (ch, 1);
v.push_back (*ch);
}
while ((int)ch[0] != 10 || file.eof ());
}
file.close ();
}

For some reason, one that i don't comprehend, i get through
the inner while-loop three times (as suppsed to) but then, i
get stuck and the computer does not see the end of the file.

Contents of the file are three lines of normal text and then
a double saved binary.

Any thoughts? My guess is that it's perhaps due to that i
didn't open the file binary but does it really matter to
recognizing "the end"? How can i "switch" the openess
of the file?

Yes, the file _IS_ supposed to have a leading few lines of
normal text and then a bunch of binary data. Sad but true...
--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
Jun 11 '07 #1
3 2695
The Cool Giraffe wrote:
Regarding the following code i have a problem.

void read () {
fstream file;
ios::open_mode opMode = ios::in;
file.open ("some.txt", opMode);
If you expect *ANY* binary data in the file you must open it in binary
mode. This means that you will get platform specific line endings which
means you need to parse them.

x '\n' x - is a unix newline
x '\r' '\n' x - is a windows newline
x '\r' x - I think (not sure) is a MAC newline.

Basically eat up at most one \n and \r and then send back a newline.
char *ch = new char[1];
char ch[1] not good enough ? Or just plain char ch ?
vector <charv;
while (!file.eof ()) {
do {
file.read (ch, 1);
v.push_back (*ch);
}
while ((int)ch[0] != 10 || file.eof ());
Should that not be ! file.eof() ?
}
file.close ();
}
Jun 11 '07 #2
Gianni Mariani wrote/skrev/kaita/popisal/schreibt :
The Cool Giraffe wrote:
>Regarding the following code i have a problem.

void read () {
fstream file;
ios::open_mode opMode = ios::in;
file.open ("some.txt", opMode);

If you expect *ANY* binary data in the file you must open it in binary
mode. This means that you will get platform specific line endings
which means you need to parse them.

x '\n' x - is a unix newline
x '\r' '\n' x - is a windows newline
x '\r' x - I think (not sure) is a MAC newline.
I tried with '\n' on the WinXP i'm on and it worked too. Should i
thank the nice employers of mr. Gates for this feature or should
i duck preparing for further issues if i keep using '\n'?
Basically eat up at most one \n and \r and then send back a newline.
> char *ch = new char[1];

char ch[1] not good enough ? Or just plain char ch ?
> vector <charv;
while (!file.eof ()) {
do {
file.read (ch, 1);
v.push_back (*ch);
}
while ((int)ch[0] != 10 || file.eof ());

Should that not be ! file.eof() ?
Actually, not quite. It took my a while but i finally realized that
not only the negation was needed (as you suggested) but also
conjunction, instead of disjunction.

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
Jun 11 '07 #3
On Jun 11, 11:09 am, "The Cool Giraffe" <giraf...@viltersten.com>
wrote:
Regarding the following code i have a problem.
I'm not sure what you're trying to do, but this is certainly
wrong. (In general, anytime you use istream::eof() in the
control of a loop, it's wrong.)
void read () {
fstream file;
ios::open_mode opMode = ios::in;
file.open ("some.txt", opMode);
char *ch = new char[1];
vector <charv;
while (!file.eof ()) {
do {
file.read (ch, 1);
v.push_back (*ch);
Note that if you are at EOF, and the read fails, then you will
push back the previously read character.
}
while ((int)ch[0] != 10 || file.eof ());
You want to loop if you've seen EOF? (And what on earth is 10.)

What I suspect you want is something like:

char ch ;
while ( file.get( ch ) && ch != '\n' ) {
v.push_back( ch ) ;
}

This will read all of the characters up to the first '\n',
inserting them into the vector, and then leave the file
positionned after the '\n'.
}
file.close ();
}
For some reason, one that i don't comprehend, i get through
the inner while-loop three times (as suppsed to) but then, i
get stuck and the computer does not see the end of the file.
Once you reach end of file, the nested loop is infinite.
Contents of the file are three lines of normal text and then
a double saved binary.
Which means that you must open the file in binary, and that you
don't expect EOF when reading the three lines. I'd do something
like:

std::vector< std::string lines ;
std::string thisLine ;
char ch ;
while ( lines.size() != 3 && file.get( ch ) ) {
if ( ch != 0x0D ) {
if ( ch == 0x0A ) {
lines.push_back( thisLine ) ;
thisLine.clear() ;
} else {
thisLine += ch ;
}
}
}

This will handle the end of line conventions under both Unix and
Windows correctly (but not necessarily under other systems),
reading three lines, and leaving the file positionned after the
end of line of the last line.
Any thoughts? My guess is that it's perhaps due to that i
didn't open the file binary but does it really matter to
recognizing "the end"?
It can. Opening it in text would greatly simplify the above,
but would mean that you cannot possibly read the double
correctly.
How can i "switch" the openess
of the file?
You mean the mode: you can't, once the file is open.
Yes, the file _IS_ supposed to have a leading few lines of
normal text and then a bunch of binary data. Sad but true...
I've run into a similar convention before. In such cases,
you're stuck with reading it in binary, and recognizing the line
endings yourself.

--
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

Jun 11 '07 #4

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

Similar topics

4
by: nightflyer | last post by:
Hi all, [code snippet appended at the end.) my question: A class has a few string variables with not know length at design time. Now I...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way...
0
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like...
9
by: jeff M via .NET 247 | last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages...
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....
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
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;...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: ...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.