Solved

How to delete a Line or word in file that is opened by File pointer in C

Posted on 1998-12-17
6
197 Views
Last Modified: 2010-04-15
When ever I am opening any existing file by using File* pointers in C. I am facing the problem of inserting some new strings/Lines(other than at the end of file) OR deleting some strings/Lines. To do this I am using explicitly some temporary buffers. Is there any simple way to do this. Or any standard API is available to do this. Please answer me.
0
Comment
Question by:shivaki
6 Comments
 

Author Comment

by:shivaki
ID: 1255254
Edited text of question
0
 
LVL 2

Accepted Solution

by:
wyy_cq earned 0 total points
ID: 1255255
no .
if you want to insert ,please store the data after the position you want to insert .and copy it to the tail back after you insert .


0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1255256
No there is no magic way to do this

You can eiher do as you do, or alternatively you can open files for read/write.  This way you can read part of the file and write to other parts.  Obviously if you are inserting you need to read any stuff you want to keep before overwritting it.

The way round this is to use a file structure with indexes (e.g. ISAM or a database engine, etc.) so you can do inserts without reading the file into memory first.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:shivaki
ID: 1255257
ThanQ but Let me to make it more specific. If Iam inserting a new string or deleting I have to protect the previous contents. this I can achieve by using the buffers. What the actual problem of mine is to include these buffers into the actual file I have to delete the exising file and create a new file with the same name and I can do that. By this there is a potential danger that I have pay the cost of file attributes ,i.e file creation date, acutal owner of that file ... To protect all of these I have to explicitly save the file attributes. I am seeking for a simple solution for this with in 'C' environment.
0
 

Author Comment

by:shivaki
ID: 1255258
This is fine for insertion since the overwritten buffer had more length than existing one. What about if I want to delete some of the file contents?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255259
you can read the while file and copy to a temporary.  When you get to lines/words that you don't want - then don't copy.  When you get to where you want new lines/words - then write them out.

When finished rename files or re-open the old input file for output and the output file for input and copy everything back again.

Alternatively, read the whole file into structures/arrays in memory, modify it there and write it all back out again.


0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now