LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C++ store,modify,and save data in text file. (http://www.linuxquestions.org/questions/programming-9/c-store-modify-and-save-data-in-text-file-784760/)

dmuffet 01-25-2010 11:29 PM

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

paulsm4 01-25-2010 11:57 PM

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

dmuffet 01-26-2010 12:14 AM

thank you paulsm4.

dmuffet 01-26-2010 12:21 AM

I hope i make my self clear in my post. I wanna use .txt as a database with rows and column.

paulsm4 01-26-2010 12:32 AM

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

dmuffet 01-26-2010 12:32 AM

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.

dmuffet 01-26-2010 12:37 AM

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

paulsm4 01-26-2010 10:15 AM

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

bigearsbilly 01-26-2010 02:13 PM

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

theNbomr 01-26-2010 02:37 PM

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.


All times are GMT -5. The time now is 07:39 PM.