Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing to a text file while reading it!!!!!!

Status
Not open for further replies.

malayaj

Programmer
Mar 19, 2001
11
0
0
hi all,
Through a c/c++ program can we read a txt file & at the same time write in to it.
like
a txt is

WE ARE ALL HUMAN. SOCIETY IS OUR HOUSE.

say...our objective is to write # after every is found.

can I read it char by char with using getc() (or any other fucn) and if i find "is" can i write at the same time # by putc() or fprintf() ?
If yes HOW?
if no why?
please send solutions asap.

regards
malayaj
 
hi malayaj,

of course you can read and write at the
same time into a file in C.
But you need to do some things first.
Open the file in mode "w+" to read and write:

FILE * fp;
fp = fopen("file.txt", "w+");
if (NULL != fp) ...

Most important after that is to call the function fseek
between reading and writing.
For a documentation see:
(Oops, just noticed a mistake in this description:
SEEK_CUR is the current pointer, of course,
looks like copy and paste...
DOES ANYBODY NOW A VERY GOOD ONLINE C-REFERENCE?)

If you walk through the file with fgetc you could call
fseek like this:

fseek (fp, 0, SEEK_CUR);

The pointer doesn't move then and you can write another charater at the same position)
The uncomfortable thing about it is:
You're going to overwrite what's already there.
So if you find the characters ' ', 'i', 's' and ' '
in combination (my C definition of an "is")
and you want to put " is#" instead, the second blank
will be overwritten.

So the most easy way would be to
store the whole file in an array of char
(line by line using fgets)
after that you can handle your strings.
Now you don't need "w+" - "r" is enough.
After having it stored, close the file.

Algorithm:

while not last index of the array{
while buffer is not " is "{ //use strcmp, of course
go on..
}

replace " is " by " is# "
index of array++;
}

Of course it's a little more complicated,
but as it looks like some kind of homework,
so I know you'll be able to do it - be creative. ;-)

After all:
Re-open the file in mode "w"
and (f-)put the whole array in it. (or fwrite it)


Regards,
Buraje
 
For the example you give, the easiest way is to open the original file for read access (see fopen), create a temporary file (see tmpfile or tmpnam), read the original file byte by byte (see getc,fgetc), write the data with changes to the temporary file (see putc,fputc), close the original file (see fclose), close the temporary file, rename the temporary file giving it the original file's name (see rename), overwriting the original file.

Unless the size of the file isn't going to change, you can't read and write to the same file and produce the desired results. IOW, you can't "insert" data into a file.

You don't really need to store the stuff in an array of char, you can just use a single variable and call getc() repeatedly until you hit the end of file. Use a state variable to determine if you should write a '#' or not to the temporary file.

Buraje:

A good online C reference:


Russ
bobbitts@hotmail.com
 
thank you all,
hi Bujare,
I think my problem is solved,
actually i have to write at the end if each line.
So overwriting is not very problematic.
thank you
regards
malayaj

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top