473,549 Members | 2,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2812
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...@vilte rsten.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 objektorientier ter Datenverarbeitu ng
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
3145
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 declare lets say a 1000 of those classes and put them in a vector. Can I write that vector directly to a binary file?
50
4896
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 to do it? Handling the buffer is not the problem -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
0
1454
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 "---------------------------390C0F3E0099" without the quotes), and also some more text like this and file name (e.g. "Content-Disposition: form-data; name="upload_file"; filename="C:\testing\myfile.dat"...
9
6738
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 20284, 20924, 1140, 37, 500 and 20127.By working I get the correct answer by taking the decimal valueand using that as an index to an array that will map to thecorrect EBCDIC value in hex. By larger...
7
6044
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an...
30
4546
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
5247
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;
11
3564
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: '-------------------------- Public Shared Function Read_bin(ByVal ruta As String) Dim cadena As String = "" Dim dato As Array If File.Exists(ruta) = True Then
13
3667
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 and i want to update values in it. The file consists of structures of type---- struct record { int acountnum; char name; float value;
0
7526
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...
0
7965
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...
1
7483
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...
0
7817
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5375
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...
0
5092
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...
1
1949
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
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
771
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.