By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

fstream question

P: n/a
I have two files, the first one is an ascii file and the second is a
binary one.
I want to add the ascii file on the end of the binary file in order to
obtain a one binary file:

I have done something like this but doesn't work:

int main(int argc, char **argv)
{
double c;
char density[80]="density\0";
ofstream file_vtk(argv[1],ios::out | ios::binary | ios::app);
ifstream file_data(argv[2],ios::in | ios::binary);
while(!file_data)
{
file_data >> c;
file_vtk << c << " " ;
}

file_data.close();
file_vtk.close();
return 0;
}

I'm sure that there is a good way to do this, but I dont have it !!
any suggestions?
regards.
Yous
Dec 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Youssef Mesri wrote:
I have two files, the first one is an ascii file and the second is a
binary one.
I want to add the ascii file on the end of the binary file in order to
obtain a one binary file:

I have done something like this but doesn't work:

int main(int argc, char **argv)
{
double c;
char density[80]="density\0";
A string literal is an ascii-0 terminated array of approproate number
of characters. So you neednot append an ascii-0 character specifically
(unless you want an extra 0 character there)
ofstream file_vtk(argv[1],ios::out | ios::binary | ios::app);
ifstream file_data(argv[2],ios::in | ios::binary);
while(!file_data)
this means that while file_data is in FAIL state (and you mean exactly
the reverse)
So what you need is

while(file_data)
{
file_data >> c;
why do you want to use double ? Use a char. double will create problems
since the data read will get converted as a double.
file_vtk << c << " " ;
}

file_data.close();
file_vtk.close();
return 0;
}


HTH.

Dec 22 '05 #2

P: n/a
Neelesh Bodas wrote:
Youssef Mesri wrote:
I have two files, the first one is an ascii file and the second is a
binary one.
I want to add the ascii file on the end of the binary file in order to
obtain a one binary file:

I have done something like this but doesn't work:

int main(int argc, char **argv)
{
double c;
char density[80]="density\0";

A string literal is an ascii-0 terminated array of approproate number
of characters. So you neednot append an ascii-0 character specifically
(unless you want an extra 0 character there)

ofstream file_vtk(argv[1],ios::out | ios::binary | ios::app);
ifstream file_data(argv[2],ios::in | ios::binary);
while(!file_data)

this means that while file_data is in FAIL state (and you mean exactly
the reverse)
So what you need is

while(file_data)


soory, it was a typing error
{
file_data >> c;

why do you want to use double ? Use a char. double will create problems
since the data read will get converted as a double.

the data in file_data is double, I want to save it double.
my problem is located in the different types of data in the two files
(ascii and binary). If I open the result file is constructed with two
different type, the first part in binary and the second in the ascii
format. And I would like just a binary type!!
file_vtk << c << " " ;
}

file_data.close();
file_vtk.close();
return 0;
}

HTH.

Dec 22 '05 #3

P: n/a

Youssef Mesri wrote:
my problem is located in the different types of data in the two files
(ascii and binary).
Whether the contents are double or ints or Person details is all
relative to how you view it (and makes sense only at a higher level of
abstraction). For a stream, the whole information is seen as a sequence
of "char"s.
Eg: if you have 10 in your file, you may interprete it as "int",
"double" or "binary" or whatever - the stream will read it as a
sequence of characters at the lowest level. So you should use a char
here (assuming you are using ifstream and ofstream)
If I open the result file is constructed with two
different type, the first part in binary and the second in the ascii
format. And I would like just a binary type!!


What do you mean by "first part" and "second part"? What do you mean by
ascii and binary format? Note that these terms actually make sense with
the low-level file operations which decide how to open the file for
reading and writing etc. Since you have appended the binary file to a
text file, you will surely see first some ascii characters and then
some non-ascii characters.

Of course I am not an expert in C++. May be I missing something.
Corrections are most welcome.

Dec 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.