1753562 Members
5954 Online
108796 Solutions
New Discussion юеВ

C++ help

 
RobertClark
Occasional Contributor

C++ help

Hello All,
Below is a file with the following entry.
and i want to delete following entry from file
Order of records is the file remain same.

if i give input Owner and passwd2
it should delete from this file.
Owner:Group:user:passwd
Owner:Group:user:passwd1
Owner:Group:user:passwd2
Owner:Group:user:passwd3
Owner1:Group:user:passwd
Owner2:Group:user:passwd1
Owner3:Group:user:passwd2
Owner4:Group:user:passwd3

final output
Owner:Group:user:passwd
Owner:Group:user:passwd1
Owner:Group:user:passwd3
Owner1:Group:user:passwd
Owner2:Group:user:passwd1
Owner3:Group:user:passwd2
Owner4:Group:user:passwd3

Can somone suggest me by using map ? or any
other data structure.Well i am against
using tempfile.

regards
robert
3 REPLIES 3
Peter Godron
Honored Contributor

Re: C++ help

Robert,
I'd read the file, strtok the entries into a malloc, after checking for the first and last fields against the parameters.
At the end of the file, rewind and output the array.

Also seems you have not updated your earlier thread:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1074999
Dennis Handly
Acclaimed Contributor

Re: C++ help

Peter suggests using strtok but you need to make a copy.

Actually there is no need to malloc, you can just filter out each record as you read and then write a new file. Unless this was excluded by Robert saying "against using tempfile".
Peter Nikitka
Honored Contributor

Re: C++ help

Hi,

this solution with awk

awk -v o=Owner pw=passwd2 -F: 'NF==4 && !($1==o && $4==pw)' this_file

'transform' to C. I don't think C++ specific stuff will bring any advantage.
awk -> read file line by line
-v o=Owner -> char *o, *pw;
-F: -> field = strtok(line,":");
while (strtok(NULL,":")

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"