Solved

Read/Write to txt file as Database

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

627 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