C++ Program Help

Discussion in 'Trading Software' started by ES335, Mar 2, 2007.

  1. ES335


    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

    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.

    // Close the sample file.

    } // ReadFromSampleFile
  2. nitro


  3. nkhoi

    nkhoi Moderator

    Murach series are excellent, I had his CICS books.
  4. ES335



    I understand 80% of the program. Since my understanding of pointers is still basic and I haven't found a reference to LineBuffer in my text, I am not sure about the code fragment I highlighted above.

    You are an experienced programmer, could you maybe take 1 or 2 lines to explain what the char linebuffer [4096] and char *filepath means, I would appreciate it. My hunch is that the first is some sort of initializing of an array using char as datatype. For the second, is filepath a function which creates a character array?
  5. Hi ES335,

    <i>filePath</i> (filePath is of type pointer-to-char. In that context, the asterisk means pointer. A pointer stores the memory location of an object. You will learn more on this when you read your C++ Primer!!) is the argument passed into the function <i>ReadFromFileSample</i>, and subsequently used by the ifstream object, which expects as its first argument a pointer-to-char. The void in front of the function indicates that there is no value to return when you call the function from other code. <i>filePath</i> remains in scope within the <i>ReadFromFileSample</i> function (between the curly braces). As you may have guessed, filePath is, quite literally, the path of the file that you are processing. The openMode is also used by the ifstream object to determine, again - as you may have guessed, the mode that you would like to open the file. The [4096] just means that you would like to allocate 4096 bytes to your character array <i>lineBuffer</i>. I would suggest getting a C++ book or reading C++ online tutorials, as these topics are almost certainly covered in the vast majority of elementary C++ educational resources.

    The object <i>count</i> (of type int) has not really been used in the code, but you may choose/need to implement it into your splitter/storage logic.

    Unfortunately, the poster did not suggest how to actually split the lines, but now you can take the next step and investigate how to do so. Google will provide the answers you seek - try googling for 'C++ CSV splitter' or something to that effect.

    If you want to learn C#, learn C#, otherwise I would recommend getting a book on C++ so as to not deviate too much from your goal. If C++ is your chosen path, check out Qt by Trolltech - it provides plenty of classes to do what you seek (split the lines, store them into containers, etc.)

    Good luck.

    ps: if anyone can clarify on any incorrect use of terminology that I have used, please post!
  6. ES335



    Thx much for your explanations. Here's another question:

    // Get a line from the input file.
    sampleFile.getline( (char *) &lineBuffer, 4096 );

    In the above, we are using the getline function (which is a cin object) to reach into the input file. Then the & operator assigns that line to the character array named lineBuffer. I think this is correct. However, what role does (char*) play here?

    In my text, the getline function was introduced as getline (cin, name); This is probably the most basic format.

    Is the (char*) in the code above some sort of cin which is then stored in the linebuffer array of size 4096 bytes through the & operator?
  7. Let us use google to investigate the usage of the getline method which we know is a method of ifstream (right?)

    We get:

    (char *) means cast to pointer-to-char. We know that a pointer stores an address. And, we know that lineBuffer is not a pointer, and the & symbol in that context means 'the address of'. So, we are saying, take the address of our <i>lineBuffer</i>, convert (cast) that to a pointer-to-char so the getline method can understand it, and store a maximum number of 4096 characters (which conveniently corresponds to the size of the character array). Remember, a pointer is just a memory address of some other type, so we need the address of our buffer object when casting it to a pointer, not the object itself.

    Hope this helps.
  8. andread


    Actually, if you look at the example on the same page, you'll see that a char array is a char*. I think there is an error in the code.
    Damn, too much time with too little C++. I'll have to pick it up
  9. rosy2


    i have never seen this approach and have no idea why he did it. arrays and pointers are technically different but stick with convention. anyway c++ has string() and you can look at the boost.org libraries for some good classes
  10. ES335


    I think the original programmer who wrote the code was mixing in C concepts with C++ concepts, but I'm not sure. (char*) for ex, is casting the &linebuffer as a pointer to character, but it seems that the use of () around the char* is distinctively C, and not C++

    My textbook on C++ does not have that in it. But I will buy the book suggested by YSPW, since it looks great and has had excellent reviews for thoroughness.

    I also think the way he opened the file was a bit odd, since in C++ you can open the file in one line. But then again, what do I know, I've just begun.
    #10     Mar 3, 2007