fstream question

Y

Youssef Mesri

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::eek:ut | 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
 
N

Neelesh Bodas

Youssef said:
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::eek:ut | 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.
 
Y

Youssef Mesri

Neelesh said:
Youssef said:
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::eek:ut | 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
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!!
 
N

Neelesh Bodas

Youssef said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top