473,387 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

new to fstream

Hello, I'm not sure how to do this,

first, just to be sure, getline() reads and copy to the buffer up to the
delim paramater, and then sets the get pointer after the delimiter right ?

Now, let's say I have both get and put pointer at the start of a given
string in a file.

And I want to replace this string with another one, how do I do ?
All the help I can find on iostream on msdn and google is very confusing...
Thanks
Jul 22 '05 #1
5 1626
Flzw wrote:

Hello, I'm not sure how to do this,

first, just to be sure, getline() reads and copy to the buffer up to the
delim paramater, and then sets the get pointer after the delimiter right ?

Now, let's say I have both get and put pointer at the start of a given
string in a file.

And I want to replace this string with another one, how do I do ?


you open the input file
you create and open an intermediate file

you read text from the input and write it to the
intermediate file until you reach the point where the
substitution has to take place.

You then read the original text from the input file *but*
you write the replacement text instead to the intermediate file

you then continue to read the input file and write it to
the intermediate file until the end of input is reached.

At this point you have 2 files:
the original file
the intermediate file, which is an exact copy of the original file
with the exception of the replacement.

You delete the original file
and rename the intermediate file to the same name the original file had.

Job done.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
you open the input file
you create and open an intermediate file


There's no way to do it with only one file open for both reading and wrting
? because I will have to do several (maybe many) replacements one by one, so
copying the whole file x times, deleting then renaming looks like a lot of
overhead...
Jul 22 '05 #3
Flzw wrote:
you open the input file
you create and open an intermediate file

There's no way to do it with only one file open for both reading and wrting
? because I will have to do several (maybe many) replacements one by one, so
copying the whole file x times, deleting then renaming looks like a lot of
overhead...


You can overwrite the text, if and only if, the size of the old text is
the same as the new text.

The C++ language has no facilities for inserting text into the middle
of a file. This has to do with the nature of sequential files. Imagine
a long tape with files one after another with no room between them.
In order to expand one file, all the other ones have to be moved further
down the tape. Big process. So, a new file is created at the end of
the tape containing the new data. The old file is marked as deleted.

If you have a lot of insertions, you may want to create a linked list
of the corrections. The original data is written to the new file until
the first item in the list. Then the list item is written to the new
file. More data from the original file is copied over until the next
item in the list is reached, then it is written to the file. Repeat
until the original file hits EOF.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4

"Flzw" <fl****@wanadoo.fr> wrote in message
news:ci**********@news-reader1.wanadoo.fr...
you open the input file
you create and open an intermediate file
There's no way to do it with only one file open for both reading and

wrting ? because I will have to do several (maybe many) replacements one by one, so copying the whole file x times, deleting then renaming looks like a lot of
overhead...


I've never heard of an file I/O system that supports insertion into the
middle of a file.

You should probably read the whole file into memory. If its too big for
that, then I think you've got some work to do.

john
Jul 22 '05 #5
You should probably read the whole file into memory. If its too big for
that, then I think you've got some work to do.


Yes, that's what I do, don't know why it didn't come to mind earler.

However, io streams classes look extremely complex compared to what they
actually do....
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
3
by: David Blasdell | last post by:
This appears very strange to me, I'm having a problem with a fstream object inside a class which is not being called directly from main (another class calls the class that contains the fstream...
3
by: ratzeel | last post by:
The following snippet code throws an error while compiling on SUN os.. Any idea how to resolve this... #include <iostream.h> #include <fstream.h> #include <math.h> #include <algorithm>...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
1
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data...
5
by: ehui928 | last post by:
The following program is used to open a file in mode both read and append, but it has a problem that the file can't be opened. #include <iostream> #include <fstream> #include <cstdlib> ...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
5
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
7
by: Soneji | last post by:
*sigh* Ok, I have two questions. First, my setup: Win-doze XP-SP2; & Dev-C++ 4.9.9.2; I recently started trying to use 'fstream' to work my input/output, and I noticed that using the...
6
by: Gaijinco | last post by:
Should this do something? #include <fstream> #include <string> int main() { std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out); std::string s="";
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.