Solved

reading from a file and writing contents back to it

Posted on 2011-09-04
8
297 Views
Last Modified: 2012-05-12
Hi Experts,

I want to wait for a file to be written with some data and remove the contents from that file and write back to it.  How can I do it?

Thanks
0
Comment
Question by:ambuli
8 Comments
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36482193
You can open file with r+ or w+ to read, write and modify content.
http://www.go4expert.com/forums/showthread.php?t=2977
and fseek() func
http://www.cplusplus.com/reference/clibrary/cstdio/fseek/
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 36482200
1. If you know the unique pattern of the content, then you can do
grep   <PATTERN>   file
   to check the output is the content that you want to remove.
  If the output is exactly those content you want to remove, then
sed '/PATTERN/d'   file > new_file
mv   new_file   file


2. If you know the unique pattern in the first line of the content to the end of file, then you can do
sed '/PATTERN/,$d'   file   > new_file
mv  new_file   file


3. If you know the line number of the content you want to remove, say line 21 ~ 25, then you can do
sed   '21,25d'   file  > new_file
mv  new_file   file
0
 

Author Comment

by:ambuli
ID: 36482234
Thanks all. Sorry for not being clear. Actually I am trying to code what
the following commands do

#exec 3<> /ambuli/myfilel
#echo "msg:some-message" >&3; cat <&3

My code has to wait until something is written to the file and then remove the contents and write back to it.
Thank you.
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 36482246
> My code has to wait until something is written to the file and then remove the contents and write back to it.
Then don't write to it. Take out
echo "msg:some-message" >&3
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Accepted Solution

by:
xeltek earned 200 total points
ID: 36482259
I am confused about the "remove the contents and write back to it."
1. you need to monitor the file is written.
[you ere under linux ,you can use inotify:
http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/
]
2. you want to read the content
[
when you get notified that file content is changed, you can open and read it.
]
3. you will write back what you read?
[
seek to the position where you read, and write back?
]
0
 

Author Comment

by:ambuli
ID: 36482312
I know it sounds silly, but I need to remove the data and write it back.  This is for a test.
0
 
LVL 38

Assisted Solution

by:wesly_chen
wesly_chen earned 100 total points
ID: 36482358
#exec 3<> /ambuli/myfilel
# cp  /ambuli/myfilel   /ambuli/myfilel_backup
#echo "msg:some-message" >&3; cat <&3
# cp  /ambuli/myfilel_backup   /ambuli/myfilel    (or replace "cp" with  "/bin/mv -f " )
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 200 total points
ID: 36484238
if it is a text file it is not possible to simply remove data. on the best you could open the file for both reading and writing; move to the file position where the part is which needs to be removed, remind that position and read the rest of the file into dynamic memory. finally set the file position and write the rest from memory to file. close the file. it now has all old contents beside of the removed part. to insert the removed part again, open the file for reading and writing again. set the file position agqin and write the removed part prior to the rest.

Sara
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

20 Experts available now in Live!

Get 1:1 Help Now