File streamin question in|out

This is a discussion on File streamin question in|out within the C++ Programming forums, part of the General Programming Boards category; Code: fstream File(my.txt,ios::in | ios::out); Is it possible to read and to write to a file at the same time ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    57

    File streamin question in|out

    Code:
    fstream File(my.txt,ios::in | ios::out);
    Is it possible to read and to write to a file at the same time useing that line. I have been told it is impossible to do that but i have seen tutorials out there. If it is anything i should concider

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It's possible. Streams uses separate read and write pointers (seekg/p and tellg/p). I have some memory of when doing a read/write operation it will reset the other pointer so take this into consideration, but don't quote me on this one...

    If it is anything i should concider
    If you don't need to read and write at the same time don't open it as both read-n-write.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    My teacher said that he tryed to do it but no luck. oo we use dev c++ if that helps beside that yah i will try thanks.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Hm, it seems that reading data clears the written data to the file. I was so sure this was possible . Anyway, your best bet would be to close then re-open the file using a new mode (read/write).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    If you wish to append data to the end of a file, you should specify the ios::app flag, else the ostream will overwrite the existing contents of a file.

    if you wish to modify a file at the beginning or "somewhere in the middle" then the only (*1) option is to create a new temporary file, in which to create the updated contents, then overwrite the old file with the updated one.


    *1 - Of course, there might be nonstandard extentions which achieve this using some other method, this is the only way to do it with standard C++.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    Thanks Yah thats what i have been told

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21