ofstream and ifstream for searching and writing

This is a discussion on ofstream and ifstream for searching and writing within the C++ Programming forums, part of the General Programming Boards category; I have a question to ask regarding reading/writing simultaneously to random files. I've searched the FAQ and the forums from ...

  1. #1
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Question ofstream and ifstream for searching and writing

    I have a question to ask regarding reading/writing simultaneously to random files. I've searched the FAQ and the forums from DAY 1 and I have not come accross anything like what I'm trying to do. Maybe I'm just weird. I dunno. But I realise I need your help.

    Here's the deal: I'm trying to read and write simultaneously to the same file using ifstream (read) and ofstream variables. The thing is, these two are separate functions. When I search for a record with ifstream, the ofstream variable doesn't know where the ifstream variable is. So I have to do a seekp on the ofstream based on the ifstream's tellg. Get it?

    In short, everytime I search using ifstream, I have to update ofstream on where ifstream is so that I can write to that position. The problem is that it doesn't always work.

    I miss the days of C when I could just use ONE FILE *fptr variable to do both.

    My question is, am I doing something wrong here? Is that the way it's supposed to be done in C++? Your comments please.
    Excuse me, while I water my money tree.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    I could tell you didn't read my post, go back and read it again.

    gg

  3. #3
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Talking

    Thanks Codeplug - I think it's more of my misreading your post. Sorry...

    It's still a lot more work than C though.
    Excuse me, while I water my money tree.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    Yes, it is a pain to test for existance and create if need be. You only have to do it once - perhaps at start up.
    fstream will have all the input and output methods in one package.

    gg

  5. #5
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70
    Umm... Codeplug... I'd hate to break this to ya... but that doesn't seem to work either.

    Can you declare a variable to do BOTH reading (read) and writing? (write)

    That's what I am trying to do. I did try the ios::binary| ios::in| ios:ut and that didn't seem to work. My compiler is telling me that the read functions aren't a member of the ofstream class.

    I'm trying to read and write simultaneously - with one variable if possible.
    Excuse me, while I water my money tree.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    fstream - no i and no o.

    fstream contains the interfaces of both ifstream and ofstream.

    gg

  7. #7
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Thumbs up

    Thanks Codeplug - works beautifully. I just wished you had been a little clearer in the first post regarding the Deitel and Deitel. Essentially you had already answered my question.

    But I think you made the misassumption that I already knew that fstream was actually the base class for both ifstream and ofstream. Hence, the reason why I completely misunderstood your first post. If I did, I wouldn't have posted this one

    Never the less, it works like a charm now. Thanks for your help. It was incredibly useful.
    Excuse me, while I water my money tree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to a file, it doesn't seem to be working for me...
    By porsche911nfs in forum C++ Programming
    Replies: 11
    Last Post: 04-06-2009, 02:57 PM
  2. Ofstream... broke as a result of ifstream fix
    By tms43 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 10:40 AM
  3. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 03:38 PM
  4. why is this writing the last line twice to the file
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2002, 01:37 AM
  5. Replies: 9
    Last Post: 06-06-2002, 07:03 PM

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