The program below was written by Curtis Faith over at his TradingBlox forum. He was responding to a beginner C++ programmer who did not know how to read from a comma delineated data file and input the data into a multidimensional array for further processing.
I've been trying to teach myself the basics of C++, including reading/writing from a file and I'm having some trouble understanding the first function's declarations, namely:
void ReadFromSampleFile( char* filePath )
{
char lineBuffer[4096];
int openMode;
ifstream sampleFile;
int count = 0;
My understanding is that Curtis here was just declaring openMode as an int data type, same for count which he initialized to 0, and he also used the ifstream object to read the file. But I'm stumped on char lineBuffer [4096]. Also, how does void ReadFromSampleFile( char* filePath ) relate to the rest of the code. I think it's a function header with an argument, but I don't understand char*filePath.
Can any of you expert programmers help me understand this?
Would greatly appreciate it
Thx
Here's the program in its entirety:
#include <iostream.h>
#include <fstream.h>
void ReadFromSampleFile( char* filePath )
{
char lineBuffer[4096];
int openMode;
ifstream sampleFile;
int count = 0;
// Open the sample file.
openMode = ios::in | ios::nocreate;
sampleFile.open( filePath , openMode );
// Check for errors opening.
if ( !sampleFile.good() )
{
// Report the error here.
}
// Loop over the file reading in a line at a time.
while ( !sampleFile.eof() )
{
// Get a line from the input file.
sampleFile.getline( (char *) &lineBuffer, 4096 );
// If we have not had a problem reading the data.
if ( sampleFile.good() )
{
// process the new line here.
}
// Increment the count.
count++;
}
// Close the sample file.
sampleFile.close();
} // ReadFromSampleFile
I've been trying to teach myself the basics of C++, including reading/writing from a file and I'm having some trouble understanding the first function's declarations, namely:
void ReadFromSampleFile( char* filePath )
{
char lineBuffer[4096];
int openMode;
ifstream sampleFile;
int count = 0;
My understanding is that Curtis here was just declaring openMode as an int data type, same for count which he initialized to 0, and he also used the ifstream object to read the file. But I'm stumped on char lineBuffer [4096]. Also, how does void ReadFromSampleFile( char* filePath ) relate to the rest of the code. I think it's a function header with an argument, but I don't understand char*filePath.
Can any of you expert programmers help me understand this?
Would greatly appreciate it
Thx
Here's the program in its entirety:
#include <iostream.h>
#include <fstream.h>
void ReadFromSampleFile( char* filePath )
{
char lineBuffer[4096];
int openMode;
ifstream sampleFile;
int count = 0;
// Open the sample file.
openMode = ios::in | ios::nocreate;
sampleFile.open( filePath , openMode );
// Check for errors opening.
if ( !sampleFile.good() )
{
// Report the error here.
}
// Loop over the file reading in a line at a time.
while ( !sampleFile.eof() )
{
// Get a line from the input file.
sampleFile.getline( (char *) &lineBuffer, 4096 );
// If we have not had a problem reading the data.
if ( sampleFile.good() )
{
// process the new line here.
}
// Increment the count.
count++;
}
// Close the sample file.
sampleFile.close();
} // ReadFromSampleFile