Solved

Read/Write to txt file as Database

Posted on 2014-09-24
4
734 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  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 learn how to clear a vector as well as how to detect empty vectors in C++.
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.

756 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