Reading data from text file WITHOUT using standard template library

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Myxamatosis
    New Member
    • Feb 2008
    • 30

    Reading data from text file WITHOUT using standard template library

    Currently I need to read data from a file, such like this

    Measurements for Lansing, Michigan during April, 2000
    63 32 0.00
    54 43 0.10
    59 39 0.00
    46 24 0.00
    52 20 0.00
    54 30 0.00

    The first line is obviously disregarded, since all we want are the numbers.

    We are not allowed to use anything from standard template library, vectors to be more precise

    I had something like this for now, though I'm not sure if it cuts the first line out or not...I'm also cutting main() off the post, I know it works, its the I/O I don't get.

    Code:
    void read( ifstream& In, ReadData List[], int Size, int& Num )
    {
      ReadData Temp;
    
      Num = 1;
    
      for (;;)
      {
        In >> Temp.Min >> Temp.Max >> Temp.Precipt;
    
        if (In.fail() || Num >= Size) break;
    
        List[Num] = Temp;
    
        Num++;
      }
    }
    
    
    void process( const ReadData List[], int Num )
    {
      int I;
      //***************************
      // Display the column headers
      //***************************
    
      cout << "\n";
      cout << "Minimum Value      Maximum Value     Precipitation\n";
      cout << "-------------      -------------     -------------\n";
    
      for(I=0;I<Num;I++)
      {
        cout << resetiosflags( ios::right ) << setiosflags( ios::left );
        cout << setw(20) << List[I].Min << "          "<< List[I].Max << "          "<< List[I].Precipt <<endl;
      }
    }
    as for declaring the struct, its
    Code:
    struct ReadData
    {
      int Min,Max;
      double Precipt;
    };
    Currently the output yeilds
    0 0 (some reference address)

    ...and thats it

    I know i'm somewhere close, but I'm drawing a blank. I set Num = 1 in hopes of skipping the first set in the data, which would be the string of words.

    Your Help is Greatly Appreciated.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I hate to break this to you but you are using ifstream and the >> operator and these are part of the STL.

    You have to throw your C++ book in the garbage and a copy of the ANI C Programming Language from , oh say, 1988 and then learn how it was done twenty years ago.

    That is you need to use the C FILE*.

    Is this some kind oddball class problem??? If so, your course needs severe updating.

    Comment

    • Myxamatosis
      New Member
      • Feb 2008
      • 30

      #3
      Hmm. Considering That i'd have to use a time machine to write this program, perhaps our professor just wants us to read and process data without using vector streams.

      ...at least i hope.

      So without using vector streams, am i close?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Yes, you are close.

        However, if you use an array you code:
        [code=c]
        List[Num] = Temp;
        [/code]

        and if you use a vector=<ReadDat a> you code:
        [code=c]
        List[Num] = Temp;
        [/code]

        In the first case you have to worry about max array size, overruning the array bounds, etc.

        With a vector, you don't.

        That is, assume the array and the vector have 10 elements and for some reason Num is 20. In the first case, no error occurs. Temp just get written as element 21, which is outside the array and this stomps on something else. This means the buggy code (using arrays) corrupts memory and just keeps going.

        With a vector, you crash right now tryinf to go outside the vector.

        Arrays are built-in containers from C and have no real place in C++ other than to compile relic C code. The less you know about them, the better. However, an old teaching paradigm was to learn C (like arrays) and then learn C++ (like vectors). This approach has been debunked many times as atavistic.

        Plus a vector is an array. It just has the code you would need to write to use the array. So, every time you use an array you are re-inventing the wheel and duplicating already written code.

        Comment

        • Myxamatosis
          New Member
          • Feb 2008
          • 30

          #5
          alright, upon reviewing the assignment, it seems we're supposed to use something along the lines of getline() from the string class library.

          Now granted if this was python, I could do this no problem, but I'm a bit rusty at my c++, it's been 3 years since I've used it.

          So Is there a way to salvage my code by using getline?

          Comment

          • Myxamatosis
            New Member
            • Feb 2008
            • 30

            #6
            I'm on the right track with the sample code I had originally posted...we only needed to use getline() to possibly skip the first line of data, which is the string.

            However, I'm still not sure why I'm getting the output I have, which is 0 0 and some reference address.

            I need to use arrays, which I understand has the potential to overload itself...but I don't know where in my code I went wrong for reading and outputting the data.

            If you need the main() part to test it yourself, i'll post if needed.

            Thanks Much

            Comment

            Working...