12 Replies - 1015 Views - Last Post: 25 July 2008 - 10:12 AM Rate Topic: -----

#1 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Deleting Info from a txt file

Posted 19 July 2008 - 07:16 AM

I've learned how to write info to a txt file, but now I'd like to learn how to remove that info from the txt file

(I'm too lazy to go to the root of my hard drive and open the file and delete it)

What's even worse is I have launchy where I hit alt + space and I could type in the location of the file and open that way, but nah I'd rather do straight from the program.

My question is basically this, I'd like for the program to list all the records (which I already know how to do)
and then I'd like to be able to select a record based on the row, and delete it or modify it.

I need this preferably in C. Thanks

Is This A Good Question/Topic? 1

Replies To: Deleting Info from a txt file

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Deleting Info from a txt file

Posted 19 July 2008 - 07:24 AM

HAI
CAN HAS STDIO?
GIMMEH TEHCODEZ
VISIBLE "Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well."
KTHXBYE


[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Deleting Info from a txt file

Posted 20 July 2008 - 06:00 PM

I'm sorry I want you to know that I'm not ignoring this post It's just that this was a little side project that I wanted to learn and I couldn't seem to find anything useful under google search using manipulating text files in C that seemed to describe what I wanted to do.

When I get more time, I'll see if I can play with my code a little and figure something close. Until then, if someone knows of a great resource that I could look at and point me in the right direction I'd be happy to take a look at it as long as it for C language.

Thanks.
Was This Post Helpful? 1
  • +
  • -

#4 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,119
  • Joined: 01-August 07

Re: Deleting Info from a txt file

Posted 20 July 2008 - 09:30 PM

You want to delete something in the file or delete the file?

Just search in the C++ snippets.
Was This Post Helpful? 0
  • +
  • -

#5 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Deleting Info from a txt file

Posted 21 July 2008 - 05:19 AM

Something in the file...
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,165
  • Joined: 14-September 07

Re: Deleting Info from a txt file

Posted 21 July 2008 - 09:58 AM

This snippet will guide the way:

File workings

cplusplus.com also has a great section on FILE input/output
Was This Post Helpful? 0
  • +
  • -

#7 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Deleting Info from a txt file

Posted 21 July 2008 - 10:03 PM

you could take the contents of the file, read it to an array, remove what needs to be removed, then write the array back to the file after you wipe the file contents.
Was This Post Helpful? 0
  • +
  • -

#8 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Deleting Info from a txt file

Posted 24 July 2008 - 04:21 PM

View PostDelta_Echo, on 22 Jul, 2008 - 01:03 AM, said:

you could take the contents of the file, read it to an array, remove what needs to be removed, then write the array back to the file after you wipe the file contents.


That's perfect, because this is something I'll need to use in my next project. Can you point to a reference on how to do that? I've looked on cprogramming.com but I'm not really sure what to do here...
Was This Post Helpful? 0
  • +
  • -

#9 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Deleting Info from a txt file

Posted 24 July 2008 - 10:17 PM

Tutorials: (on C++ File I/O (Input/Output)
http://www.cplusplus...rial/files.html
http://www.cprogramm...l/lesson10.html
Was This Post Helpful? 0
  • +
  • -

#10 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Deleting Info from a txt file

Posted 24 July 2008 - 11:37 PM

Oh, if your just starting C+, i suggest you buy "C++ Without Fear". Its a great book.
Was This Post Helpful? 0
  • +
  • -

#11 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Deleting Info from a txt file

Posted 25 July 2008 - 05:31 AM

Again this is all great info, but I'm not onto learning C++ yet. I'm still learning C. If anyone knows of any resources that are written for C I'd appreciate it. Thanks.
Was This Post Helpful? 0
  • +
  • -

#12 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Deleting Info from a txt file

Posted 25 July 2008 - 09:06 AM

OK. IFF you want to read info into an array then rewrite it, you need to open it in truncation mode if you are using fstream with c++.


ofstream fout;
fout.open("file.txt", fstream::trunc);
//writing here overwrites the file
fout.close();

Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Deleting Info from a txt file

Posted 25 July 2008 - 10:12 AM

No matter what solution, "Deleting info from a text file" will essentially involve reading in a file, discarding the bit you don't like, and overwriting that file.

While using an array make this very straight forward, it doesn't scale well: it's limited to memory. For this sort of thing I'd open an instream and an outstream. Read from the only and only send to the out the stuff out want to keep. The only caveat to the approach is that you initially need to write to a temporary file, then replace the current file with the temporary file.

This seems to have enough to get you going: http://irc.essex.ac...._and_output.asp
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1