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 a File once it was opened

Status
Not open for further replies.

Felix25

Programmer
Dec 9, 2002
5
US
Hi! I trying to find out how to read a file once I opened already with a Open File Common Dialgog box.

I already opened doing:

void __fastcall TMain_Menu::ButtonBrowseCFDClick(TObject *Sender)
{
AnsiString CFDFileName;
if (OpenCFDDialog->Execute())
{
CFDFileName = OpenCFDDialog->FileName;
}

Now, the file has the following format(fields)

12.3 2.35 2.32 23.2 3.23
12.3 2.35 2.32 23.2 3.23
12.3 2.35 2.32 23.2 3.23
ect... (4 fields of real numbers)

I've done this in C++ but not in builder,
pleasee heeeelp!!!!!!!!!!!
 
once you've opened the file with:

FILE *fp;
fp = fopen( CFDFileName.c_str(), "r" );

you can read and convert a whole line with:

long f1, f2, f3 ,f4 ,f5;
fscanf( fp, "%f %f %f %f %f\n", &f1, &f2, &f3, &f4, &f5 );
 
Hi! I wonder if I need a preprocessor directive for doing this, When I run the code step by step teh compiler ignores(skips) the following lines.

FILE *fp;
fp = fopen( CFDFileName.c_str(), "r" );

thanks
 
Yes, you need to
Code:
#include <stdio.h>
.

Another option is to use streams.
Code:
#include <fstream>
.
.
.
std::ifstream CommFile; // stream set up
CommFile.open(CFDFileName.c_str());

// Check File
if (!CommFile)
{
    // Something went wrong
    Application->MessageBox(&quot;Could not open initialization file for processing!&quot;, &quot;File Error&quot;, MB_OK);
}
else
{
    long f1, f2, f3 ,f4 ,f5;
    Comfile >> f1 >> f2 >> f3 >> f4 >> f5;
}

CommFile.close();

I'm doing this from memory so there a most likely errors in the code but it should give you an idea. James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top