Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > fstream question

Reply
Thread Tools

fstream question

 
 
Youssef Mesri
Guest
Posts: n/a
 
      12-22-2005
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: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
 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-22-2005
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: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.

 
Reply With Quote
 
 
 
 
Youssef Mesri
Guest
Posts: n/a
 
      12-22-2005
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: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
>
>
>> {
>> 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.
>

 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-22-2005

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Question about Extended ASCII character set, and fstream C++ 1 10-21-2004 05:40 PM
Question about Extended ASCII character set, and fstream C++ 1 10-21-2004 09:30 AM
what is different between <fstream.h> and <fstream>MS VC++ Armando C++ 6 01-29-2004 09:01 AM
fstream lock question Rob Kreger C++ 1 12-11-2003 03:03 AM
a naive question about the exception in fstream Newsgroup - Ann C++ 1 10-29-2003 04:40 AM



Advertisments