Solved

Read/Write to txt file as Database

Posted on 2014-09-24
4
770 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 83

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 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