Solved

reading from a file and writing contents back to it

Posted on 2011-09-04
8
302 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Set-Top-Box hardware and Open Source! 8 264
gdb doesn't stop on breakpoint 2 95
c++, dynamic object by json 1 53
How to Correctly derive class from CWinThread in MFC 4 67
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

830 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