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

Reading Lines from Text File

Status
Not open for further replies.

fergmj

Programmer
Feb 21, 2001
276
US
I have a test file (ASCII) that has lines that look like this:

$GPRMC,060001,A,3348.795,N,11754.064,W,000.0,000.0,110698,013.7,E*62
$GPRMB,A,,,,,,,,,,,,V*71
$GPGGA,060001,3348.795,N,11754.064,W,1,07,1.0,65.8,M,-31.9,M,,*4F
$GPGSA,A,3,14,15,16,18,22,25,29,,,,,,2.2,1.0,1.9*38
$GPGSV,2,1,07,14,70,322,55,15,31,212,53,16,32,317,54,18,10,282,47*7C
$GPGSV,2,2,07,22,40,120,53,25,36,045,52,29,28,073,50,,,,*4B
$PGRME,19.5,M,44.7,M,48.8,M*10
$GPGLL,3348.794,N,11754.064,W,060002,A*3E
$PGRMZ,217,f,3*1F
$PGRMM,WGS 84*06

I am reading the first line of the file like this:

OpenDialog1->Options.Clear();
OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
OpenDialog1->Filter = &quot;RESON Simulator GPS Files (*.txt)|*.txt|All files (*.*)|*.*&quot;;
OpenDialog1->FilterIndex = 2; // start the dialog showing all files
if (OpenDialog1->Execute())
{
edtLoadFile->Text = OpenDialog1->FileName;
for (int I = 0; I < OpenDialog1->Files->Count; I ++)
{
stream = fopen(OpenDialog1->Files->Strings.c_str(), &quot;r&quot;);

if (stream)
{
// read the first line from the file
fgets(FirstLine, sizeof(FirstLine), stream);
rchEdit1->Lines->Append(FirstLine);
fclose(stream);
}
}
}

I need to know how to read the other lines of the file, line by line.

Thanks!

Fergmj
 
The easier method is to use a TStringList.

TStringList *FileStrings = new TStringList();

FileStrings->LoadFromFile(&quot;filename.abc&quot;);

Now FileStrings contains a String list, one line for each line in the text file separated by new lines or carriage returns.

Chris
 
Thanks. Great idea. I'll do it.

fergmj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top