Solved

Read/Write to txt file as Database

Posted on 2014-09-24
4
697 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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Title # Comments Views Activity
Fully specialized class template function 21 142
C++ question 3 70
C++ mouse_event mouse look 7 95
gdb doesn't stop on breakpoint 2 93
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

840 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