Thread: File reading and writing!

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

    File reading and writing!

    is there a way to read in a file one char at a time then when a certain letter is met you can overwrite what comes next or insert text after it??

    as far as i know the only way to write to a file is either to truncate it or append neither is what i want

    really my problem lies in writing to the file because i can do everything up till that point

    thx

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes.

    What you want to do is create a filestream to open your file. Then write a loop that reads in a single character from the stream, then checks to see if it's the character you want. If it isn't, it will concatinate it onto a string variable then read in another character. If it is the character you want, it will exit the loop, then you just concatinate what you want to add onto your string. Then you write that to a new file.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main() {
        
        char readIn;
        string acceptedTxt;
        
        ifstream inFile ("hello.txt");
        
        while (acceptedTxt != "Hello,") {
           readIn = inFile.get();
           
           acceptedTxt = acceptedTxt + readIn;
           }
           
        acceptedTxt = acceptedTxt + " Fred!";
    
        
        inFile.close();
        
        ofstream outFile ("hello.txt");
        
        outFile << acceptedTxt;
        
        return 0;
    }
    What this program will do, if put into the same folder as my attached text, will change the text in the file "Hello, Sarah!" into "Hello, Fred!".

    It reads in up to the part you want, then changes the rest.

    I hope that's what you were looking for.
    Last edited by SlyMaelstrom; 10-14-2005 at 11:42 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    if you are attempting to insert characters into the file, you will need to use two files -- input file and temporary output file. Read the characters from the input file then write what you want to the output file. After the input file is completly read, close both files, delete the input file and finally rename the temporary output file to the same name as the original input file.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If the text you want to replace is the same size as the text you want to replace it with, you can do it with only one file by using fstream and seekg or seekp or something like that. I don't remember the details, but if you search for fstream and seek you might find it.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    40
    basicaly what im trying to do is if i have this in a file

    Code:
    Hold me now, oh hold me now
    Till this hour has gone around
    And I gone, on the rising tide
    For to face Van Diemen's land.
    the user inputs a word to replace lets say hold
    then a word to replace it with(same length) lets say AAAA

    then the program finds and replaces all occurances of hold giving

    Code:
    Hold me now, oh AAAA me now
    Till this hour has gone around
    And I gone, on the rising tide
    For to face Van Diemen's land.
    i think i might be able to figure it out with what has been said thx alot

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    40
    can fstream file("*.*") be used for text or just binary...

    if it can be used for text i cant figure out how.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Assuming you put a filename in there instead of wildcards, yes. In fact, it is used for text by default. If you want binary you have to specify binary. Show your code and describe the issues you are having.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    40
    first i tried this

    Code:
    int main()
    {
        fstream file("hello.txt");
        file<<"hello"<<endl;
        file.close();
    }
    and it didnt even make a file.

    then i tried

    Code:
    fstream file;
    file.open("hello.txt");
    and that did nothing

    so then i tried

    Code:
    int main()
    {
        fstream file;
        file.open("hello.txt");
        file.write("hey", 3);
        file.close();
    }
    and it still didnt even make a file

    then i tried
    Code:
    int main()
    {
        fstream file;
        file.open("hello.txt", ios::out);
        file<<"hey"<<endl;
        file.close();
    }
    and it worked !! but when i changed it to
    file.open("hello.txt", ios:ut | ios::in );
    it doesnt work...

    so i guess my new question is how do i make a fstream that reads and writes

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If the file does not exist it will fail to open for reading. So check for fail() and if it is true then just use ios_base::out to open the file. This is a quick example that does work:
    Code:
    #include <fstream>
    using namespace std;
    
    int main()
    {
        fstream file("Fout.txt", ios_base::out | ios_base::in);
        if (file.fail())
        {
            file.clear();
            file.open("Fout.txt", ios_base::out);
        }
    
        if (file.good())
            file << "hey" << endl;
    
        file.close();
    }

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    40
    thanks that helps alot... and seekg seekp pointers remain independant right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmetation fault (reading and writing file in c)
    By tasosa in forum C Programming
    Replies: 5
    Last Post: 04-13-2009, 06:04 AM
  2. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM