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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a text file

Status
Not open for further replies.

shilpashetty27

Technical User
Apr 12, 2007
32
CA
Hi all,

I’m trying to read the contents of a text file and attempting to compare each content with a particular number in a pre-defined array. I’m unable to proceed as I’m stuck with how I can read contents that are specified in various formats. For eg: below is a sample of how the contents of the file look like:


R [20][00][00][20] // This is the first line
R [21][00][00][40][00][01][00] // This is the second line

How can I read the first character that is, R, in this case, followed by [20] followed by [00] and so on. Please note the values in square brackets are in hex format.

Any ideas on how I can proceed are appreciated.


Thanks in advance!
Shilpa
 
You say that you have a pre-defined array. I'm assuming that the array is a two dimensional array. For example, a 2 x 6 array. You would need two for-loops. One to process the outer part of the array. For example
Code:
for (int i = 0; i < 2; ++i)
{...
}

For the inner part of the array, you call a second loop.
Code:
for (int j = 0; j < 6; ++j)
{...
}

It would look something like
Code:
for (int i = 0; i < 2; ++i)
{
    for (int j = 0; j < 6; ++j)
    {
        char c = R[i][j];
    }
}


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Hi there,

I'm sorry I should have been more specific. The predefined array is just a 1-d 8 element array and the contents of the text file have spaces between the numbers. For eg, the contents are:

R [40] [45] [56]
R [10] [12] [13] [14]


Please note each line like the above lines are variable length lines. I'm having trouble reading each character. I want to read 'R' and then compare it with an element in my array. Then I want to read [40] and compare it with another element in my array so on and so forth.

Please note that in the text files, there are comments that follow each line, like the example I mentioned in my previous post.

The difficulty lies in the reading of the data from the file. I'm stuck on the kind of data type i need to use to read each character/number.


Any more ideas,

Thanks!
Shilpa
 
OK. Off the top of my head, I can think of several ways to read files. My favorite is to use streams. I open a file stream, read in the file line by line then parse the characters. You could also just read each character in the file instead of each line. The advantage the line has is you won't be picking up the return or new line characters in the file.

Code:
AnsiString ProcFile = "C:\\Temp\\MyFile.txt";
std::ifstream FNFile; // stream set up
FNFile.open(ProcFileStr.c_str());

// Process the file
if (!FNFile)                    // Cannot open selected file
{
    // Error opening file
    AnsiString MsgStr = "I cannot open " + ProcFileStr + "!";
    MessageDlg(MsgStr, mtError, TMsgDlgButtons() << mbOK, 0);
    return;
}                               // Cannot open selected file
else                            // Processing
{
    std::string ArcLine;
    while (getline(FNFile, ArcLine, '\n'))  // As long as there is data
    {
        // Do the processing here
    }                           // As long as there is data
}                               // Processing
FNFile.close();



James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
I have a project which amongst other needs to read a Motorola Hex-file which is a text-reprensatation of binary datas. To speed up the program i simply read the size of the textfile, reserves memory for the whole file and reads into memory in one chunk.

From thereon it's almost the same: find a specific character, decoda some, read over whitespace and such.

When i'm finished converting the datas i simply deletes the buffer fro the textfile. It works like a charm for me.

Totte
Keep making it perfect and it will end up broken.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top