fstream question

Discussion in 'C++' started by Youssef Mesri, Dec 22, 2005.

  1. 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
     
    Youssef Mesri, Dec 22, 2005
    #1
    1. Advertising

  2. 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::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.
     
    Neelesh Bodas, Dec 22, 2005
    #2
    1. Advertising

  3. 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::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
    >
    >
    >> {
    >> 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.
    >
     
    Youssef Mesri, Dec 22, 2005
    #3
  4. 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.
     
    Neelesh Bodas, Dec 22, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    1
    Views:
    2,105
    Davlet Panech
    Oct 29, 2003
  2. Rob Kreger

    fstream lock question

    Rob Kreger, Dec 11, 2003, in forum: C++
    Replies:
    1
    Views:
    4,756
    Jack Klein
    Dec 11, 2003
  3. Armando
    Replies:
    6
    Views:
    749
    Armando
    Jan 29, 2004
  4. Guest
    Replies:
    1
    Views:
    806
    Catalin Pitis
    Oct 21, 2004
  5. Guest
    Replies:
    1
    Views:
    477
    Ron Natalie
    Oct 21, 2004
Loading...

Share This Page