Thread: open a file

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    32

    Unhappy write a file

    hi,
    I have this code:
    Code:
    fstream in ("in.data",ios_base::in);
    fstream out ("out.data", ios_base::out|ios_base::in);
    in.seekg (0, ios::end);
    size =in.tellg();
    block = new char [size];
    in.seekg (0, ios::beg);
    
    in.read (block, size);		
    out.write (block, size);
    The problem is: I have open "out.data" with in and out mode (because then I have to read it too). But the write doesn't work! the file isn't create. I notice that if I open out in mode append and in, it all OK! But in the rest of program, I have to read file and write strings at the begining (and not append strings)
    What's happen?
    thanks
    Last edited by mickey0; 07-13-2007 at 05:43 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not sure what the exact problem would be. Why not use a separate variable for reading and writing out.data? Use an ofstream to write it out. Then close the file when you are done and use an ifstream to read it in later.

    Also, note that you are using the binary i/o functions read() and write(), but you didn't specify binary in the flags. If you want to read/write as text, use operator>> and operator<<. Otherwise if you are using binary specify ios::binary.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    32
    hi, I can't read my file in only binary_mode; maybe because is it a text file?
    this below work:
    Code:
    out << memblock;
    but problem is that at the end of file "out" I can see some strange characters (kkkìììììì) that aren't in the input file. How can I read in one hit input file without find in the output those character?
    thanks

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you read the file like this
    Code:
    in.read (block, size);
    You are not creating a 0 terminated string.
    but
    Code:
    out << block;
    expects memblock to be a 0 terminated string.
    a solution could be
    Code:
    in.read (block, size);		
    block[size] = 0;
    but then you would have to allocate one more char for block.
    Kurt

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    32
    This below doesn't work.
    Inside memblock there's my file and then the same strange characters (I guess new allocate a space of memory a bit more large then the size of file. Maybe allocate eg 4 byte when to put the file I need 3,5 byte). So with memblock[size] = 0; I can avoid only some '&#236;&#236;&#236;&#236;&#236;&#236;&#236;&#236;& #236;&#236;&#236;&#236;&#236;&#236;&#236;&#236;&#2 36;&#236;&#236;&#236;' but not all! is it so? any suggest?
    And then out file doesn't created. why?
    thanks
    Code:
    fstream in ("in.data", ios_base::in);
    fstream out ("out.data", ios_base::out|ios_base::in);
    //retrieve size
    memblock = new char [size+1];
    in.seekg (0, ios::beg);
    in.read (memblock, size);	
    memblock[size] = 0;
    cout << " mem bloxk " << memblock;
    out << memblock;
    delete [] memblock;

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your way of getting the size of the file ( seek and tell ) is not very reliable when you're on a windos box.
    The problem is that when you open a file in text-mode all the line end markers (CRLF) are converted to single '\n' characters. Possibly that is your problem.
    Post the complete code.
    Kurt

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    32
    Code:
    fstream in ("in.data", ios_base::in);
    fstream out ("out.data", ios_base::out|ios_base::in);
    ifile.seekg (0, ios::end);	
    int size;
    size = ifile.tellg();
    memblock = new char [size+1];
    in.seekg (0, ios::beg);
    in.read (memblock, size);	
    memblock[size] = 0;
    cout << " mem bloxk " << memblock;
    out << memblock;
    delete [] memblock;
    hi this is complete

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main() {
        fstream in ("data.in", ios_base::in);
        fstream out ("out.data", ios_base::out|ios_base::in);
        in.seekg (0, ios::end);	
        int size;
        size = in.tellg();
        char * memblock = new char [size+1];
        in.seekg (0, ios::beg);
        in.read (memblock, size);	
        memblock[size] = 0;
        cout << " mem bloxk " << memblock;
        out << memblock;
        delete [] memblock;
    }
    This works fine for me. But I'm on a linux box and there is no difference between text and binary files.

    I wonder why you open out for in and output. Ths will prevent out to be created if it doesn't exist.

    I would try opening the streams in binary mode
    like this

    Code:
     fstream in ("data.in", ios_base::in | ios::binary);
    Kurt
    Last edited by ZuK; 07-14-2007 at 07:52 AM.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    32
    Hi, this below now works! The cout prints the memblock right (without stange char) but out file is write wrong: the text is write in the file on a line! (I repeat: instead cout prints it correctly). Why that?
    Code:
        fstream in ("data.in", ios_base::in | ios::binary);
        fstream out ("out.data", ios_base::out|ios_base::in);
        in.seekg (0, ios::end);	
        int size;
        size = in.tellg();
        char * memblock = new char [size+1];
        in.seekg (0, ios::beg);
        in.read (memblock, size);	
        memblock[size] = 0;
        cout << " mem bloxk " << memblock;
        out << memblock;
        delete [] memblock;
    EDIT: it's wordpad (windows) that show me the text on a line. Instead notepad (windows) show me right. How to solve?

    I open out with in and out mode because I need to read from it and write it; after read and write, many times..
    Last edited by mickey0; 07-14-2007 at 08:28 AM.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by mickey0 View Post
    Hi, this below now works! The cout prints the memblock right (without stange char) but out file is write wrong: the text is write in the file on a line! (I repeat: instead cout prints it correctly). Why that?
    Actually the right way to write the outputfile would be to open the output file in binary mode as well and use
    Code:
    out.write (memblock, size);
    Kurt

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    32

    Solved

    hi, with this below works all!
    Code:
    fstream in ("inputfile.data", ios::binary | ios_base::in);
    fstream out ("out.data", ios::binary | ios_base::out);
    note: if I remove ios::binary arise problems I just said posts before.
    If I do:
    Code:
    fstream in ("inputfile.data", ios::binary);
    ...the input file isn't open!!!
    Someone could explain me the reason of this strange behviors?
    thanks

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by mickey0 View Post
    If I do:
    Code:
    fstream in ("inputfile.data", ios::binary);
    ...the input file isn't open!!!
    Someone could explain me the reason of this strange behviors?
    thanks
    You're using a plain fstream -> you have to tell if it's for in or output.
    use ifstream.
    Kurt

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why are you using binary at all? IMO your attempt to use operator<< was the way to go, you just have to use operator>> to read in the data.

    So get rid of binary, read and write, and just use >> and <<.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So get rid of binary, read and write, and just use >> and <<.
    But that doesn't guarantee an exact copy. May-be he wants to preserve all the whitespace?
    getline() could do that, though.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM