×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

insert line anywhere in .txt file without erasing

insert line anywhere in .txt file without erasing

insert line anywhere in .txt file without erasing

(OP)
This may seem a stupid question but I didn't find an ansewer to it...

I'd like to insert lines of data in a .txt file that already contains data.
I tried with fprintf() , helped by fseek() to place the cursor at the beginning of the file for example, but the problem is that when fprintf() writes data in the file, it replaces the data of the file by the data to be written.
(the file has previously been opened wtih fopen(fid,'r+'))

What I'd like to do is inserting my new data, just like I would do if I write my new data at the beginning of the file, remaining the previous data in the following lines.

I also checked if I made an error of the permission of fopen, but it seems that there's no other way to open than r, r+, a, a+, w, ... but no one does what I want ... :(

Help please.... I need quick help.... please

Rolando

RE: insert line anywhere in .txt file without erasing

As a general rule, you can't do that directly in a text file.  If you copy the front data into another, append your new data and then the back data, it would work.

TTFN

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources