LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-25-2010, 11:29 PM   #1
dmuffet
LQ Newbie
 
Registered: Jan 2010
Posts: 12

Rep: Reputation: 0
Question C++ store,modify,and save data in text file.


Hello guys, im new here!

I wanna learn how to store data in text file and be able to modify it and save as well using C++. Its been 3days, I couldn't find help. I hope Im in the right place. I think a simple code will help,not too complicated i mean.thanx in advance,any help will be highly appreciated.

Note: im using Turbo C++ 3.0
 
Old 01-25-2010, 11:57 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You might try to Google for "C++ text I/O"

You might also look in your manual and/or on-line documentation for "fstream".

Just a suggestion .. PSM
 
Old 01-26-2010, 12:14 AM   #3
dmuffet
LQ Newbie
 
Registered: Jan 2010
Posts: 12

Original Poster
Rep: Reputation: 0
thank you paulsm4.
 
Old 01-26-2010, 12:21 AM   #4
dmuffet
LQ Newbie
 
Registered: Jan 2010
Posts: 12

Original Poster
Rep: Reputation: 0
I hope i make my self clear in my post. I wanna use .txt as a database with rows and column.
 
Old 01-26-2010, 12:32 AM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Suggestion:

1. Look up C++ text I/O

2. Write a C++ program that:
a) opens an fstream (e.g. "test.txt")
b) writes the line "a b c"
c) writes a second line, "d e f"
d) closes the file

3. Verify that the file contains:
Quote:
a b c
d e f
4. Now write a *second* program to *read* these two lines.

5. Modify the program to "parse" a, b, and c from the first line; then d, e and f from the second, and finally detect that there is no third line.

6. Sit back and enjoy a nice warm drink. You've earned it.

You're well on your way to completing this homework assignment.

'Hope that helps .. PSM
 
Old 01-26-2010, 12:32 AM   #6
dmuffet
LQ Newbie
 
Registered: Jan 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Thank you for the immediate respond, this mean a lot to new members like me, which really in bad times. I got your point paulsm4, no need to reply in this thread.Than you again.
 
Old 01-26-2010, 12:37 AM   #7
dmuffet
LQ Newbie
 
Registered: Jan 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Its not a homework, i am not studying anymore, im making my self busy learning programming stuff, at the end of this week im hoping to learn using mysql as a database with c++. then next week im planning to learn java.i hope you can help me more by that time.thanks a lot,.
 
Old 01-26-2010, 10:15 AM   #8
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

For whatever it's worth, I think Java is more commonly used for "real world" projects with mySQL. PHP or Perl are two other candidates.

I'm *not* a big fan of C++ (although I *am* of C). I agree with Alan Kay:

Quote:
"I invented the term object-oriented, and I can tell you that C++ wasn't what I had in mind"
http://c2.com/cgi/wiki$

Personally, I think Java is a *much* better language for learning, and arguably also a much better language for production code.

IMHO .. PSM

Last edited by paulsm4; 01-26-2010 at 10:18 AM.
 
Old 01-26-2010, 02:13 PM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,283

Rep: Reputation: 172Reputation: 172
yes C++ isn't fully OO.
but if you read Stroustrup's book you understand why.
his main goal was to maintain run time efficiency.
and C++ is blisteringly fast. (well it is unless you use iostreams!)
 
Old 01-26-2010, 02:37 PM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
dmuffet, I think the point needs to be raised that using text files such as you are suggesting will not allow you to simply read a certain value from a row/column arrangement, modify that 'cell' only and close the file. To modify anything in such a file, you will need to read the entire file, row by row, saving it in memory with appropriate modifications, and then close the file and re-open it for writing. Then re-write then entire content back to the disk file and close it off.
Alternatively, you can rename the original file, open a new file with the original name and write out the modified data to the new file. You could then either remove the original, now renamed, file or simply leave it as a backup file, in the way many text editors do.
I hope this touches on the subtle point of your original question.

--- rod.

Last edited by theNbomr; 01-26-2010 at 02:39 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ - Store a text file in a Data Structure nilly16 Programming 3 05-26-2009 06:42 AM
How to modify a field in few lines in a file and save the new file - in Perl rounak94 Programming 1 10-02-2008 07:43 PM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
What file to modify to change a mac address or to save eth0 configuration ikw38 Linux - Networking 2 04-26-2006 04:52 PM


All times are GMT -5. The time now is 08:45 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration