using streambuf when working with files

This is a discussion on using streambuf when working with files within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm writing a simple program that performs a simple text substituion. Program needs to replace text in a file. ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    using streambuf when working with files

    Hi, I'm writing a simple program that performs a simple text substituion.
    Program needs to replace text in a file.
    I manage to do something like this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <streambuf>
    
    using namespace std;
    
    int main()
    {
    	string s;
    	string::size_type ind;
    
    	ifstream in ("in.txt", ios_base::in);
    	ofstream out("out.txt", ios_base::out);
    
    	while (getline(in ,s))
    	{	
    		ind = s.find("second");
    		if (ind == string::npos)
    		{
    			out<< s<<endl;
    			continue;
    		}
    		else
    		{
    			s.replace(ind,sizeof("second")-1,"rep");
    			out<<s<<endl;
    		}
    	}
    
    }
    I test it with a simle file in.txt:
    This is first sentence.
    This is second sentence.
    This is third sentence.

    and result is:

    This is first sentence.
    This is rep sentence.
    This is third sentence.

    As you can see my program create another file named out.txt which contains replacement.
    I want changes to be made in original file.
    Sure, I can add something like this:
    Code:
        out.close();
    	in.close();
    	out.open("in.txt",ios_base::out);
    	in.open("out.txt", ios_base::in);
    	out<<in.rdbuf();
    but this would be very inefficient way to handle this plus there is a new (unwanted) file out.txt.

    My question is how to perform this task with only one file in a more efficient way.

    My idea is to:
    1. open file (ifstream)
    2. store contents in a buffer
    3. close file
    4. examine buffer (possible with the same while loop)
    5. made changes
    6. open file (ofstream)
    7. overwrite it with new contnts.

    HI don't know much about strembufs and if you can post some code to understand how this would be done.

    My attempt was something like this:
    Code:
    ...
    ifstream in ("in.txt", ios_base::in);
    streambuf* pbuf = in.rdbuf();
    ...
    but question is how to use getline in that case?

    I hope you can help me. The best help would be to post some code, because it's the best way to learn new things (at least, for me)
    I realize this can be done in another way, for instance by using vector to act like buffer
    Code:
    string s;
    	string::size_type ind;
    	ifstream in ("in.txt", ios_base::in);//open file
    	vector<string> v;//sentences will be stored in vector (buffer)
    
    	while (getline(in ,s))
    	{	
    		ind = s.find("second");
    		if (ind == string::npos)
    		{
    			v.push_back(s);
    			continue;
    		}
    		else
    		{
    			s.replace(ind,sizeof("second")-1,"rep");
    			v.push_back(s);
    		}
    	}
    
    	in.close();//close file
    
    	vector<string>::const_iterator it;//vector iterator
    	ofstream out ("in.txt",ios_base::out);
    	for (it = v.begin(); it != v.end(); ++it)
    		out<<*it<<endl;//write to file
    	
    	out.close();//close file
    But, I'd like to see solution using streambuf, if I'm not asking too much
    Thanks
    Last edited by Micko; 03-19-2005 at 07:12 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > I want changes to be made in original file.
    Except you just do delete old file, rename new file.
    Both of these are likely to be standard services offered by your OS.

    > My idea is to:
    > ... 7. overwrite it with new contnts.
    Except inopportune power failures and program bugs will result in you losing both the old and the new file.
    It is very unwise to go round deleting the old file until you're sure the new file is in place. Indeed, simply keeping the old one around as file.bak (like your average editor does) is probably not a bad idea.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote Originally Posted by Salem
    > I want changes to be made in original file.
    Except you just do delete old file, rename new file.
    Both of these are likely to be standard services offered by your OS.
    Thnaks, Salem
    So this solution of mine is a good one?
    P.S. Should I delete and rename file using remove() and rename() functions?
    Last edited by Micko; 03-19-2005 at 10:33 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    A better solution is the temporary file. One reason is because file size may vary.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  2. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  3. Working with DLL files...
    By Devil Panther in forum Windows Programming
    Replies: 8
    Last Post: 11-14-2004, 11:42 PM
  4. working with resource files
    By the Wookie in forum Windows Programming
    Replies: 4
    Last Post: 02-01-2003, 09:26 AM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 01:51 PM

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