File I/O in C++

This is a discussion on File I/O in C++ within the C++ Programming forums, part of the General Programming Boards category; Hi I am witing a program to change the the text in a file from "This is an apple" to ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    4

    Smile File I/O in C++

    Hi

    I am witing a program to change the the text in a file from "This is an apple" to This is a sample". I need to open the file in append mode too for adding text afterward.
    The code for program is:-

    #include <fstream>
    #include<iostream>
    using namespace std;

    int main (int argc, char *argv)
    {
    long pos;
    fstream outfile;
    outfile.open ("test.txt",iosut|ios::ate|ios::app);
    if (outfile.fail())
    {
    cout << "Error opening file, errno: " << errno ;
    }
    pos=outfile.tellp();
    cout<<"\n pos is " << pos;
    outfile.seekp (pos-7);
    cout<<"\n pos is " << outfile.tellp();
    outfile.write (" sam",4);

    outfile.close();
    return 0;
    }

    The problem is instead of changing the text it adds the word sam in the file. Please help.

    Thanks.

  2. #2
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    you should not add "ios::app" in this code;
    use this :
    Code:
    outfile.open ("test.txt",ios::out|ios::ate);
    ios::app means add content after the end of the file.

    blow me ... ...

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    4
    Thank you for replying.

    I tried this way too. if i open the file like
    outfile.open ("test.txt",ios:ut|ios::ate);
    or
    outfile.open ("test.txt",ios::ate);

    it overwrites the contents of existing file and displays just the sam in the file.

    Any other suggestions are welcome.

    Thansks.

  4. #4
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    i have tried this code, it works fine.
    make sure the content of text.txt is "this is an apple" only.
    Code:
    #include <fstream>
    #include<iostream>
    using namespace std;
    
    int main (int argc, char *argv)
    {
    long pos;
    fstream outfile;
    outfile.open ("test.txt",ios::out|ios::ate);
    if (outfile.fail())
    { 
    cout << "Error opening file, errno: " << endl ; 
    }
    pos=outfile.tellp();
    cout<<"\n pos is " << pos;
    outfile.seekp (pos-7);
    cout<<"\n pos is " << outfile.tellp();
    outfile.write (" sam",4);
    
    outfile.close();
    return 0;
    }

    blow me ... ...

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    4
    Hi

    I tried it again. This is not working for me. I checked the text.txt file. It contains "This is an apple" only.
    What could be the problem?

    Thanks.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    std::ofstream outfile("test.txt", std::ios::out | std::ios::in); //ios::in - don't overwrite
    outfile.seekp(9);
    outfile << " sam";
    Though I'll have to doublecheck, I believe ios::app specifies that all characters written will be appended to the end of the file (perhaps it sets the position 0 to be at the end of the file). ios::ate specifies that the starting position is at the end of the file, but it's kind of unclear how it operates; from what I remember from my compiler's docs, it said that ate causes the first write operation to write at the end of the file, but after that all write operations write from the beginning of the file; other online references just say that it seeks to EOF. Consequently I just try to avoid it altogether But the ios:: out flag is implicitly included in ofstream's constructor, which deletes everything anyway. To prevent truncation, you have to specify the ios::in flag (or ios::app, but that won't work in our case).

    Hope this helps
    Last edited by Hunter2; 11-26-2004 at 10:54 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    believe ios::app specifies that all characters written will be appended to the end of the file (perhaps it sets the position 0 to be at the end of the file)
    ios::app causes the write position to be set to the end of file previous to each write operation. In other words, it effectively issues a
    stream.seek(0, ios::end);
    before each write.

    ios::ate specifies that the starting position is at the end of the file, but it's kind of unclear how it operates;
    ios:ate issues a
    stream.seek(0, ios::end);
    directly after opening the file and has no effect whatsoever afterwards, meaning that writes go whereever you currently are.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Right, my mistake.
    http://msdn.microsoft.com/library/de...a.ofstream.asp

    Also, simran, if you want to seek from the end of the file you shouldn't use ios::ate and seek to (position - bytes), you should just do

    Code:
    std::ofstream outfile("test.txt", std::ios::out | std::ios::in); //ios::in - don't overwrite
    outfile.seekp(-7, std::ios::end);
    outfile << " sam";
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    4
    Hi

    Code:
    outfile.open ("test.txt",ios::in | ios::out |ios::ate );
    works. It does not overwrite the existing contents plus it replaces the text from "an apple" to "a sample"

    Thansk a lot.

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. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 09:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

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