Solved

Read/Write to txt file as Database

Posted on 2014-09-24
4
589 Views
Last Modified: 2014-09-24
Hi folks I'm trying to use a c++ program to write and read from a text file as if it was a simple database. What I'm building is a calendar of appointments, so I would need to write to several files, like one for each month and ten lines for each day being one line per appointments.
I'm just starting C++ so I'm a complete novice, I'm building using Embarcadero X6, so the Gui part is not that much of a problem to create, but the file read and write part is showing itself more chalenging than I thought it would.

Is ther a way to write to a certain line in a file? Is there a way to read to a certain line of the same file?

Any help with this would be much appreciated since I'm kind of lost to begin with.

Cheers
0
Comment
Question by:Rodric MacOliver
  • 2
4 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40343269

Is ther a way to write to a certain line in a file? Is there a way to read to a certain line of the same file?
When you are using text files, you have to write all that code yourself.  To update a text file database, you have to read the entire file in, make your changes and write your entire file out.  It's a lot of effort which is why most people use 'real' databases.
0
 
LVL 12

Accepted Solution

by:
trinitrotoluene earned 500 total points
ID: 40343277
C++ uses file streams and this greatly simplifies file I/O

here's a snippet for a file write
ofstream fout("myfile.txt");
fout<<somevar<<yetanothervar;
fout.close();

Open in new window

and you will find the values of "somevar" and "yetanothervar" written out to myfile.txt

similarly for reading from a file you can have

string line;
ifstream infile("myfile.txt");
if (infile.is_open())
  {
    while ( getline (infile,line) )
    {
      cout << line << '\n';
    }
    infile.close();
  }

Open in new window


As for positioning at a specific point in the file you will need to use the seek and tell functions and the specific version you use will depend on whether you are reading or writing.

Here's a short tutorial which explains the above in more detail.

http://www.cplusplus.com/doc/tutorial/files/
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40343278
>>>>Is ther a way to write to a certain line in a file? Is there a way to read to a certain line of the same file?
yes there is. refer the seek and tell functions I've mentioned in my earlier post. But you will need to write out your own logic to identify the exact line you are going to read/write.
0
 

Author Closing Comment

by:Rodric MacOliver
ID: 40343287
Thanks it helped a lot! Cheers.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now