Solved

reading from a file and writing contents back to it

Posted on 2011-09-04
8
299 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

863 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

22 Experts available now in Live!

Get 1:1 Help Now