file i/o 
Author Message
 file i/o

i was wondering if someone could help me with a problem i am having with
file i/o

i want to remove a single line from a file.. so it can be replaced with
new data..
basically...

i am opening a file...
getting the data line by line
for each line... i want the old data, within the file, to be
deleted/erased
so i can input the new data that has been created, into that line..
and then goto the next line... and repeat....
also, i doubt the new data will be the same size as the old data...

i was wondering if anyone could help me... because i am stumped...

thanks in advanced

matt
--



Wed, 05 Mar 2003 12:22:50 GMT  
 file i/o


Quote:
>i am opening a file...
>getting the data line by line
>for each line... i want the old data, within the file, to be
>deleted/erased so i can input the new data that has been created, into
>that line.. and then goto the next line... and repeat.... also, i doubt
>the new data will be the same size as the old data...

Easy, but you cannot, in general, do it using a single file. You have to
read one file in, write to another and then delete the old one before
renaming the new one. Look at the standard library and you will find all
you need.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Sat, 08 Mar 2003 11:17:28 GMT  
 file i/o

comp.lang.c.moderated:

Quote:
> i was wondering if someone could help me with a problem i am having with
> file i/o

> i want to remove a single line from a file.. so it can be replaced with
> new data..
> basically...

> i am opening a file...
> getting the data line by line
> for each line... i want the old data, within the file, to be
> deleted/erased
> so i can input the new data that has been created, into that line..
> and then goto the next line... and repeat....
> also, i doubt the new data will be the same size as the old data...

> i was wondering if anyone could help me... because i am stumped...

> thanks in advanced

> matt

The reason that you are stumped is that you can't do what you want the
way you want to do it, unless the new data is exactly the same size as
the old data.

What you really need to do is work with two files, the original that
you read and a new output file that you create.  You read from the
original file, make whatever additions, deletions, or changes that you
want as you go, and write the results to the new file.

You can create the new file with a temporary name, and when you have
finished and closed both files successfully you can use the standard
function remove() to delete the original file and rename() to change
the name of the new file to that of the original.  Alternatively you
can rename the original file to keep it is a back up.

Jack Klein
--
Home: http://jackklein.home.att.net
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.faqs.org/faqs/C-faq/learn/
--



Sat, 08 Mar 2003 11:19:09 GMT  
 file i/o

Quote:
>i was wondering if someone could help me with a problem i am having with
>file i/o

>i want to remove a single line from a file.. so it can be replaced with
>new data..
>basically...

>i am opening a file...
>getting the data line by line
>for each line... i want the old data, within the file, to be
>deleted/erased
>so i can input the new data that has been created, into that line..
>and then goto the next line... and repeat....
>also, i doubt the new data will be the same size as the old data...

>i was wondering if anyone could help me... because i am stumped...

>thanks in advanced

>matt

If the new data is EXACTLY the same length as the old data, then you
can position (fseek) to the start of the old data and write the new
data.  If not, you must copy the original file to a new file, omit the
old data, add the new data, and continue until you have copied the
entire file.

<<Remove the del for email>>
--



Sat, 08 Mar 2003 11:19:13 GMT  
 file i/o

Quote:

> i was wondering if someone could help me with a problem i am having with
> file i/o

> i want to remove a single line from a file.. so it can be replaced with
> new data..
> basically...

> i am opening a file...
> getting the data line by line
> for each line... i want the old data, within the file, to be
> deleted/erased
> so i can input the new data that has been created, into that line..
> and then goto the next line... and repeat....
> also, i doubt the new data will be the same size as the old data...

> i was wondering if anyone could help me... because i am stumped...

> thanks in advanced

> matt
> --


Unless you are going to get into some sort of sophisticated
indexing, and moving about the file in a random fashion, probably
the easiest way to do what you want is to open the input file,
and a new output file, input records from the input file and
output them to the new file.  The contents of each output
record will either be an original record you inputted, or one
of your new data records (to replace it):

  open input file;
  open output file;

  while more input records {
    if record is still OK {
      write record to output file;
    else if need to use new data
      write new record to output file;
  }

  close input file;
  close output file;

Yours,

Geoff Houck
systems hk

http://www.teleport.com/~hksys
--



Sat, 08 Mar 2003 11:19:17 GMT  
 file i/o

Quote:

> i am opening a file...
> getting the data line by line
> for each line... i want the old data, within the file, to be
> deleted/erased
> so i can input the new data that has been created, into that line..
> and then goto the next line... and repeat....
> also, i doubt the new data will be the same size as the old data...

Read the comp.lang.c FAQ:
<http://www.eskimo.com/~scs/C-faq/q19.14.html>.

Richard
--



Sat, 08 Mar 2003 11:20:12 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. putting end of file character in binary file (cutting the file short)

2. FILE(0), FILE(1), FILE(2) .....

3. HOW TO Filter Files Without File Extention in Open File Dialog Box

4. File extension filtering for the file dialog presented for file opening

5. Play WAV File From Resource File

6. Newbie: separate big .cs file into small .cs files

7. File Sharing Bug(?) in File.OpenText

8. How to write rtf file of a word file using VC++

9. How to download results file from form post (In IE it generates a save file dialog)

10. write a file to a xml file

11. fatal error C1083: Cannot open include file: 'excpt.h': No such file or directory

12. File.Move File.Delete

 

 
Powered by phpBB® Forum Software