Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

reading from a file and writing contents back to it

Posted on 2011-09-04
8
Medium Priority
?
316 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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
 
LVL 2

Accepted Solution

by:
xeltek earned 800 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 400 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 35

Assisted Solution

by:sarabande
sarabande earned 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

877 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