Creating a read/write file

This is a discussion on Creating a read/write file within the C++ Programming forums, part of the General Programming Boards category; I want to create a read/write file.Whenever I open it(even if it already exists) I want it to truncate itself. ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    Creating a read/write file

    I want to create a read/write file.Whenever I open it(even if it already exists) I want it to truncate itself.

    The only way I found how to do it was via a streambuf from the book 'Thinking in C++':

    Code:
    #include "../require.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main() {
        ifstream in("Iofile.cpp");
        assure(in, "Iofile.cpp");
        ofstream out("Iofile.out");
        assure(out, "Iofile.out");
        out << in.rdbuf(); // Copy file
        in.close();
        out.close();
        
        // Open for reading and writing:
        ifstream in2("Iofile.out", ios::in | ios::out);
        assure(in2, "Iofile.out");
        ostream out2(in2.rdbuf());
        cout << in2.rdbuf(); // Print whole file
        out2 << "Where does this end up?";
        out2.seekp(0, ios::beg);
        out2 << "And what about this?";
        in2.seekg(0, ios::beg);
        cout << in2.rdbuf();
    } ///:~
    I don't want to use a streambuf so I tried the following:
    Code:
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main() {
    
        fstream out("Texttest.txt",ios::out|ios::in|ios::trunc);
        ifstream in("Text.txt");
        out<<in.rdbuf();
        out.seekp(0,ios::beg);
        string s;
        out>>s;
        cout<<s;
        cin.get();
    }
    When I run it prints on the screen ZKM7461(see below the file Text.txt).This happen for every execution,as it should be.

    But when i run it with the file 'test21.txt' replacing the file 'Text.txt'
    it prints 'Λ212324' whenever it wants.Some times it prints it,some others it doesn't print anything.
    What is happaning?
    Are there any other suggestions in how I could have a read/write file the way I want it to be?

    Text.txt:

    ZKM7461 FORD Ka
    YRM3245 TOYOTA Corolla
    ET9672 PEUGEOT 204
    ZZO7563 PEUGEOT 206

    test21.txt:

    Λ212324 ΠΕΤΡΟΣ ΑΝΑΓΝΟΥ Μυρσίνης 3 15765 1
    Α124456 ΣΠΥΡΟΣ ΠΕΤΡΟΥ Δοιράνης 8 34489 0
    Β456765 ΗΛΙΑΣ ΑΝΑΣΤΑΣΙΟΥ Ελπίδος 14 15568 1
    Ζ455467 ΑΝΔΡΕΑΣ ΚΑΛΟΣ Αθηνάς 35 16472 1
    Χ789342 ΙΩΑΝΝΗΣ ΤΣΑΝΑΚΑΣ Κορυτσάς 68 32467 0
    Λ343434 ΑΝΤΩΝΗΣ ΣΤΑΜΟΥ Περικλέους 2 32256 1
    Τ565678 ΚΩΣΤΑΣ ΠΑΠΠΑΣ Παλαμά 87 44566 1

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Are you sure that "test21.txt" exists? I'm not having a problem running it and I don't see a problem with the way you've coded it (aside from the fact that you, of course, aren't checking if the ifstream file exists)...

    I'm a little confused about what you're doing.. Are you saying that the garbage at the end of your post is the contents of the "test21.txt" file?

    Code:
      out.seekp(0, ios::beg);
      string s;
      out>>s;
    Shouldn't you be using seekg() instead of seekp() here since your next operation is a read?
    Last edited by LuckY; 04-29-2003 at 12:18 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    "But when i run it with the file 'test21.txt' replacing the file 'Text.txt'
    it prints 'Λ212324' whenever it wants.Some times it prints it,some others it doesn't print anything.
    What is happaning?"


    Of course it exists.If it wouldn't it would never print 'Λ212324'.
    But it prints it whenever it wants.


    "Are you saying that the garbage at the end of your post is the contents of the "test21.txt" file"

    Yes that's what I'm saying.

    I tested it with seekg.Nothing changes.Works with the first file, doesn't work with the second.
    Last edited by k_w_s_t_a_s; 04-29-2003 at 12:25 PM.

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Originally posted by k_w_s_t_a_s
    Of course it exists.If it wouldn't it would never print 'Λ212324'.
    But it prints it whenever it wants.
    Please, pardon me, your highness. Perhaps if you explained yourself with a little better literacy, I would be better able to understand what you mean instead of trying to read your mind.
    I thought you were saying that you were reading junk instead of the text you were expecting...

    It is a mystery to me why it won't work for you. There really doesn't appear to be anything programatically wrong. To create a read/write object, all you need to do is what you're doing... After that, you're on your own as far as I'm concerned because, as I said, it works just fine for me.. Maybe you should post that second file instead of pasting it's contents? Just a thought.
    Last edited by LuckY; 04-29-2003 at 12:30 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    Sorry if my words were inappropriate.As i saw you got little bit ........ed off.I just can't figure out why it works with the one file and why it doesn't with the other.
    You ran the program and it worked perfectly for both files?

    The only way to make a read/write file is with fstream?I read that you can use something like that:

    ofstream(myfile,ios::in)

    But how am i going to read from the file then?
    I apologise for once more for my language.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Changed line commented.
    Code:
    #include "../require.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main() {
        ifstream in("Iofile.cpp");
        assure(in, "Iofile.cpp");
        ofstream out("Iofile.out");
        assure(out, "Iofile.out");
        out << in.rdbuf(); // Copy file
        in.close();
        out.close();
        
        // Open for reading and writing:
        //ifstream in2("Iofile.out", ios::in | ios::out);
        ifstream in2("Iofile.out", ios::in | ios::out | ios::trunc);
        assure(in2, "Iofile.out");
        ostream out2(in2.rdbuf());
        cout << in2.rdbuf(); // Print whole file
        out2 << "Where does this end up?";
        out2.seekp(0, ios::beg);
        out2 << "And what about this?";
        in2.seekg(0, ios::beg);
        cout << in2.rdbuf();
    } ///:~

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Originally posted by k_w_s_t_a_s
    You ran the program and it worked perfectly for both files?

    The only way to make a read/write file is with fstream?I read that you can use something like that:

    ofstream(myfile,ios::in)

    But how am i going to read from the file then?
    ofstream is derived from basic_ostream which is intended for an object that will write output only.
    ifstream is derived from basic_istream which is intended for input only.
    fstream is derived from both basic_ostream and basic_istream and intended for both input and output.

    Creating an object of class ofstream with flag "in" is a contradiction in terms.

    And, yes, each file worked fine every time I ran it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

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