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;
cout << "c= " << c << endl;
file_vtk << c << " " ;
}

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

I'm sure that is a good way to do this, but I dont have it !!
any suggestions?
regards.
Yous
 
J

Jacek Dziedzic

Mesri 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";
ofstream file_vtk(argv[1],ios::eek:ut | ios::binary | ios::app);
ifstream file_data(argv[2],ios::in | ios::binary);

As a side note -- if argc<3, your program is fried :)
while(!file_data)
{
file_data >> c;
cout << "c= " << c << endl;
file_vtk << c << " " ;
}

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

I'm sure that is a good way to do this, but I dont have it !!
any suggestions?
regards.
Yous

It is not clear what you want to do with the text file.
Do you want it to be copied verbatim past the end of the
binary file? Or do you rather want to convert the text
data from the text file and to binary and write this binary
representation past the end of the binary file?

Please clarify.

- J.
 
Y

Youssef Mesri

Jacek said:
Mesri 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";
ofstream file_vtk(argv[1],ios::eek:ut | ios::binary | ios::app);
ifstream file_data(argv[2],ios::in | ios::binary);


As a side note -- if argc<3, your program is fried :)
while(!file_data)
{
file_data >> c;
cout << "c= " << c << endl;
file_vtk << c << " " ;
}

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

I'm sure that is a good way to do this, but I dont have it !!
any suggestions?
regards.
Yous


It is not clear what you want to do with the text file.
Do you want it to be copied verbatim past the end of the
binary file? Or do you rather want to convert the text
data from the text file and to binary and write this binary
representation past the end of the binary file?
the first tentative was based on the idea to convert and copy the text
file to the end of the binary one in order to concatinate two binary files:
first file : a binary file that I open with write permission
second file: a text file to convert to binary one and copy it at the end
of the first file. but doesn't work.

The second tentative was performed with two binary files:
copy the second at the end of the first file, this is the code:
int main(int argc, char **argv)
{
double c=0;
int n=1;

char *S=new char [80];
S="density\0";
ofstream file_vtk(argv[1],ios::eek:ut | ios::binary | ios::app);
ifstream file_data(argv[2],ios::in | ios::binary);
file_vtk.write((char*)&n,sizeof(int));
file_vtk.write(S,80*sizeof(char));
file_vtk.write((char*)&n,sizeof(int));


while(file_data)
{
file_data.read((char*)&c,sizeof(double));
file_vtk.write((char*)&c,sizeof(double));
}


file_data.close();
file_vtk.close();
delete [] S;
return 0;
}
 

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,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top