Quote from ES335:
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.
openMode - a variable used to hold a value constructed from two other constants which is then passed to the call to open. In this case it means open for reading and don't create the file if it is not already there.
lineBuffer - this is array declaration for a 4K array, i.e. please Mr. compiler allocated me 4,096 single byte char variables in an array.
char* filePath - an argument (aka parameter) declaration for the function which means that later in your code you can call the function ReadFromSampleFile with any character pointer and the function will attempt to open the file defined by that char*.
Character pointers i.e. (char*) are simply the memory address the contains a string of characters. In C and C++ these are used to represent strings, i.e. "C:\Program Files\Program X\datafile.csv" or "datafile.csv", in these cases the last character is a zero marking the end of the string.
The argument declaration "char* filePath" means that you can call ReadFromSampleFile passing it a string representing the location of the file to be read:
Code:
ReadFromSampleFile( "C:\Program Files\Program X\datafile.csv" );
You can call ReadFromSampleFile many different times and it will repeat the code for each instance:
Code:
ReadFromSampleFile( "File1.csv" );
ReadFromSampleFile( "File2.csv" );
ReadFromSampleFile( "File3" );
This shows the function being called three times with three different values. Inside the code for ReadFromSampleFile the filePath variable name is a placeHolder for the actual string value that is passed into that function.
- Curtis
P.S. Technically the (char*) &lineBuffer should not have been necessary since an array of char is the same as a (char*).
This means that:
Code:
lineBuffer
&lineBuffer
(char*) &lineBuffer
are all three equivalent. So you should have been able to pass lineBuffer directly without the cast like:
Code:
sampleFile.getline( lineBuffer, 4096 );
If I recall there was some strange behavior in the particular version of the Microsoft Visual C++ Compiler (i.e. the not very ANSI compliant VC 6.0?) that required this and this is no longer the case for the last several years.