Solved

reading from a file and writing contents back to it

Posted on 2011-09-04
8
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 34

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

630 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