Solved

How do I write/read to/from a specific line of a txt(or ini) file in C++?

Posted on 2008-06-26
4
334 Views
Last Modified: 2013-11-17
How do I write/read to/from a specific line of a txt(or ini) file in C++? e.g I want to write this string "abc" in the line 13 of text file c:\text.txt , or return label1's caption the string in line 14 of c:\text.txt .
thanks
0
Comment
Question by:Chrysaor
4 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 50 total points
ID: 21876912
Take a look at this good tutorial on file I/O in C++ :

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

Accepted Solution

by:
trinitrotoluene earned 75 total points
ID: 21877887
inserting is not usually possible, but you can try overwriting existing content. Check my sample code for this.

A better way would be read the file content to a buffer and then modify the contents of the buffer and then write it back to the file. You can try this exercise.
#include <iostream>

#include <fstream>
 

using namespace std;
 
 

main()

{

 ifstream fin("c:\\text.txt", ios::in);

 
 

  string s;

  int linecnt = 0;
 

 while(getline(fin, s))

 {

   linecnt++;

   cout<<s<<endl;    

   if (linecnt==13)

   {

     long changepos = fin.tellg();     

     cout<<changepos;

     

     fin.close();

     ofstream fop("c:\\text.txt", ios::ate|ios::out|ios::in);      

        

          

     fop.seekp(changepos);

     fop<<"abc";

     fop.close();

   }

   

 } 
 

}

Open in new window

0
 
LVL 25

Assisted Solution

by:kode99
kode99 earned 75 total points
ID: 21879316
CBuilder actually makes this really really simple.  By using a TStringList class  you can load a text file into a array of strings which then lets you edit as you wish then save.  This is the same method a TMemo component uses for the text it contains.

So to set one line to 'abc' and read back a caption title would be as follows:

TStringList *sList = new TStringList();
sList->LoadFromFile("text.txt");

sList->Strings[12] = "abc";   // note that the index starts with line 1 at 0

Label1->Caption = sList->String[13];

sList->SaveToFile("text.txt");

Of course you would want to check and make sure there are 12 or 13 lines before trying to access them.  Check out the help file on TStringList for more examples.

Further if you want to use .ini files there is also a TIniFile class that will take care of the formatting.  So the file will use the keyname = value format along with sections.  The help file contains a good example of its use.  There is also a class for using the registry instead of ini files.

0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 50 total points
ID: 21880380
Hi Chrysaor,

Though there are a lot of similarities, there are many differences in writing a random line in a text file and a random line in an configuration file.  In both cases, you must rewrite the file past the location that you change.

If you're writing a configuration file, there may be a very good "easy way" for you.  The TIniFile class manages configuration files and lets you easily change specific values or add/delete values (lines).  The class will do all of the work of rewriting the file for you.

Check out the help on the TIniFile.  It's worth knowing.

Kent
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use tracing facilities in your browser steps 2 110
IdTCPClient1->Disconnect(); not working 3 61
Best IDE for PHP starters 11 113
egit plugin on eclipse 8 40
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

895 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

15 Experts available now in Live!

Get 1:1 Help Now