Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Playing with Strings and sorting it all out

Status
Not open for further replies.

fergmj

Programmer
Feb 21, 2001
276
US
I have an ini file that is set up something like this:

[receiver1]
1="11:00:00PM","11:30:00PM","WUSA","ftp.location","K:\cc\filename.txt"


There are many keys in many sections in the ini file.

I want people to be able to access and edit the information in the ini file (which is stored on a company LAN).

I can access it by LoadFromFile into a ListBox or Memo but there are only certain strings that I want displayed in the ListBox or Memo and then when they are edited, I want them to be saved back to their original location and overwrite anything older.

For instance, taking the example above....if someone wanted to change the first time from "11:00:00pm" to "11:01:00PM". I can do that by displaying the entire value of the key and then having the person edit the line and then save it back to the file but the line (comma delimited) isn't very user friendly in look and feel and someone is going to mess it up.

I'd like to display the individual strings of the ini Value in a better way and then have the person edit them and save them back to the ini file in the first format (one long string).

Any ideas.

Thanks.

Mindy
 
[tab]This is how I parse a comma delimited line in a file. It seems to work best for what I do but that doesn't mean that its the only or even the best way. It just means that it works best for me.

[tab]For the above example, I am assuming that you have figured out how to find the correct line to edit. (Meaning that you have read the file to the "[receiver1]" line and are expecting to read the next line in as a comma delimited line.) At the begining of your CPP code, just after "#pragma hdrstop", I include the following headers...
Code:
#include <fstream>
#include <sstream>

[tab]The first header includes the standard library's file stream. I personally prefer this type of file input and output. My examples below will use this I/O structure.

[tab]The second line includes the standard library's string stream. This allows me to parse the line easily.

[tab]In the section of code where you get the file's info, I would do something like this...
Code:
// Open the file
ifstream IniyFile; // stream set up for reading
IniyFile.open(&quot;MyFile.INI&quot;);

// Process the file
if (!IniyFile)
{
    // Error opening file
    // Do something about warning the user, etc.
    return;
}
else
{
    // The file is ready for reading
    string ImpLine;

    // Warning about the following line. There is a bug in BCB 5 & C++ 5.5
    // that makes the following line fail. Upgrade to the latest patch to fix!!!!
    while (getline(IniyFile, ImpLine, '\n'))
    {
        // The above code gets the entire line (to the new line symbol)
.
.
.
        // Once you get to the line you want to parse
        // Parse the string from input file by first setting up the string stream
        istringstream StrLine(ImpLine);

        // First set up some variables that will get the data
        string LineNumber, FirstTime, SecondTime, Something, FTPLoc, SomeFile;

        // Now parse the input line into parts using getline again
        getline(StrLine,LineNumber,'='); // This parses the line to the equal sign
        getline(StrLine,FirstTime,','); // This parses the line to the first comma
        getline(StrLine,SecondTime,','); // This gets to the next comma
        getline(StrLine,Something,','); // etc.
        getline(StrLine,FTPLoc,','); // etc.
        getline(StrLine,SomeFile,'\n'); // This get the rest of the line

       // At this point you could send each variable to it own edit component on the screen
    } // no error
} // end of while statement

IniFile.close();

[tab]By putting each variable into its own Edit Box, the user could then edit each variable seperately. Once the editting is done, the user could press a button to update the file. You could use fstream to output the data.
Code:
// Open the file
ofstream IniyFile; // stream set up for writing
IniyFile.open(&quot;MyFile.INI&quot;);
.
.
.
        // output each variable
        IniyFile << LineNumber<< &quot;,&quot; <<  FirstTime << &quot;,&quot; <<  SecondTime << &quot; ,&quot;;
        IniyFile << Something << &quot;,&quot; << FTPLoc << &quot;,&quot; << SomeFile << endl;
.
.
.

[tab]You would have to make certain that you append the file and not overwrite and that you put the line into the correct spot. Personally, I would do this by changing the original file name to &quot;MyFile.BAK&quot; then copying the entire file to &quot;MyFile.INI&quot; except for the lines I am changing. That way, you have the both the old and the new files.

Good luck.
James P. Cottingham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top