C++ Program Help

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

  1. You are forgetting there are 2 versions of getline...

    A global function...
    http://www.cplusplus.com/reference/string/getline.html

    A member of std::istream...
    http://www.cplusplus.com/reference/iostream/istream/getline.html

    Just as an aside, this is a good example for using EXPLICIT namespaces. No confusion of which streams are being used (generally a compile error mixing C streams and C++ streams) if author declared C++ stream as ...

    std::ifstream sampleFile;

    BTW; the file can be opened in the constructor.
     
    #11     Mar 3, 2007
  2. goslow

    goslow

    These two lines should be equivalent:

    sampleFile.getline( (char *) &lineBuffer, 4096 );
    sampleFile.getline( lineBuffer, 4096 );
     
    #12     Mar 3, 2007
  3. Nope.
    Read the variable declaration...

    char lineBuffer[4096];

    ifstream::getline requires a char*
     
    #13     Mar 3, 2007
  4. rosy2

    rosy2

    both versions compile and work. but why would anyone cast to a pointer in getline(). other than this example i have never seen it. now i evern wonder if there is a memory leak lurking in there.
     
    #14     Mar 3, 2007
  5. heres something from a previous Qt project, maybe you will find something useful in it, or maybe not, who knows. maybe i will revive the project at some point if distractions fail to override devotion of free time.

    the CSV dialog, written using Qt and a little bit of C, brings data into a multidimensional string container then converts it based on user selection..

    the code sucks, but it works :) in particular, the method <i>void CSVDialog::initOpenFile()</i> has what you are looking for - the block of code <i>while(fgets(linee,256,datafile) ){ </i> in the .cpp file has the goodies.

    -Paul
     
    #15     Mar 3, 2007
  6. Disregard please. Im a moron.

    Osorico [weekend frame of mind] :cool:
     
    #16     Mar 3, 2007
  7. andread

    andread

    True, both versions compile and work, with my surprise.
    It doesn't make any sense. And that cast makes me wonder if the code can be considered correct. If you want to give a char* you just give the array.
     
    #17     Mar 3, 2007
  8. This little crappile of code somehow takes raw CME Globex data and brings it into the proprietary binary file format used in the rest of the application. It creates volume bars. Running the app, I almost forgot how to use it because I was too lazy to label the dialog's form properly. Plus it crashes if you use it wrong, haha, what crap. I never got around to fixing that because only I was using it. Maybe I would be motivated if this were a public project. But, it does work. Maybe the Volume Bar creation algorithm that I concocted one night will be of some use to somebody somewhere, somehow. The method for that is called <i>void CMEDialog::convertToVolumeBars()</i>

    I ultimately wanted it to be nice and organized and also have a streamlined way to create and manage continuous contracts as well as make it quick and easy to select any type of bar/interval that you want.. its just such inefficient code, and so disorganized.

    Hope someone gets some use out of it, anyway.
     
    #18     Mar 3, 2007
  9. 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.
     
    #19     Mar 3, 2007
  10. ES335

    ES335

    Curtis,

    Thank You for graciously explaining all that, very much appreciated. Your example code has been a good starting point for me to study and learn from, thx for posting it. I've got a lot to learn, and will need a lot of perserverance to get it done on my own, but everyone on this thread has been helpful and I'm grateful for all the replies. My short term goal is to be able to calculate simple mathematical functions such as calculating Average True Range using ohlc data files and a simple program.
    I know that this can be done much easier in easylanguage and wealthlab etc, but since I'm learning C++ for the long haul I just figured I'd start there.

    Thx to all who have posted so far with their comments/suggestions.
     
    #20     Mar 3, 2007