Thread: File handling with Array filled with a class

  1. #1
    Village id10t
    Join Date
    May 2008
    Posts
    57

    File handling with Array filled with a class

    OK - I have a any array of size 30. Each array element is a class that consists of 5 objects.

    i have declared it as follow
    Code:
    WeatherReport MonthlyReport[DAYS];
    Now I want to read input from a file and store it in the array. The objects are all of type int and its things like HighTemp, LowTemp, Rain etc. How do I use an index with classes in arrays?

    will it be something like
    Code:
    if_stream>>report.HighTemp[i];
    and will it be the say for outputing the array after it has been read into memory?
    thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to do it the fancy way, you would declare a operator << for the class, that in itself reads the hightemp, lowtemp, rain, wind, etc.

    But that may be a bit advanced if you are a beginner, in which case you loop around reading in each element until end of file (unsuccessful reading of value indicates end of file) or full array.

    The syntax would be something like:
    Code:
    if_stream >> report[i].hightemp;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Ok forget my last post: I figured it out- its just like using a normal array (when you overload the << and >> operators

    Code:
    for(i=0;i<DAYS;i++)
    {
    in_stream1>>MonthlyReport[i];
    }
    
    for(i=0;i<DAYS;i++)
    {
    cout<<MonthlyReport[i];
    }
    
    .
    .
    .
    .
    
    istream& operator>> (istream& ins, WeatherReport& report)
    {
    ifstream in_stream1;
    
    		in_stream1>>report.DayOfMonth>>report.HighTemp>>report.LowTemp
    		>>report.AmountRain>>report.AmountSnow;
    }
    ostream& operator<< (ostream& outs,const WeatherReport& report)
    {
    
    cout<<report.DayOfMonth<<" ";
    cout<<report.HighTemp<<" ";
    cout<<report.LowTemp<<" ";
    cout<<report.AmountRain<<" ";
    cout<<report.AmountSnow<<" ";
    cout<<endl;
    }
    Ok ,just one problem I need you help with... my in_stream doesn't read the data from the file... any suggestions?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    istream& operator>> (istream& ins, WeatherReport& report)
    {
    ifstream in_stream1;
    
    		in_stream1>>report.DayOfMonth>>report.HighTemp>>report.LowTemp
    		>>report.AmountRain>>report.AmountSnow;
    }
    You should use "ins", not a local "in_stream1" variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Similarly you should use (and return) the passed stream in operator<<. Now it would compile and even be able to write WhetherReport to file.
    Code:
    ostream& operator<< (ostream& outs,const WeatherReport& report)
    {
        outs<<report.DayOfMonth<<" ";
        ...
        outs<<endl;
        return outs;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    istream& operator>> (istream& ins, WeatherReport& report)
    {
    ifstream in_stream1;
    
    		in_stream1>>report.DayOfMonth>>report.HighTemp>>report.LowTemp
    		>>report.AmountRain>>report.AmountSnow;
    }
    Actually, let me ask...
    WHY are you using a local variable, the in_stream1? What is the purpose of doing so?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Village id10t
    Join Date
    May 2008
    Posts
    57
    because im a total idiot

  8. #8
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Code:
    friend istream& operator>> (ifstream& ins, WeatherReport& report);
    .
    .
    istream& operator>> (ifstream& ins, WeatherReport& report)
    {
    
     ins.open("InputFile.dat");
    		
      for(int i=0;i<DAYS;i++)
      {
      ins>>report[i].DayOfMonth>>report[i].HighTemp>>report[i].LowTemp
      >>report[i].AmountRain>>reporti[i].AmountSnow;
      }
    It gives me the error "No match for operator[] in report[i]. Any suggestions?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are passing ONE element of report to the operator >>, not an array. If you want another operator to take an array, then you need to pass an array of the class. But I would say that your loop should be OUTSIDE.

    Also, you should not open the file in operator >> - it should be already existing and opened by the code outside of the operator.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Ok sorry but i'm slow today - I need to pass an array to the << operator, not just a single element... how will i do that?
    Code:
    istream& operator>> (ifstream& ins, WeatherReport& report[i])
    or is it like when I pass an array to a function?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The easiest way is to just make your array a std::vector and pass that by reference.
    Otherwise you can pass a pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MarlonDean View Post
    Ok sorry but i'm slow today - I need to pass an array to the << operator, not just a single element... how will i do that?
    Code:
    istream& operator>> (ifstream& ins, WeatherReport& report[i])
    or is it like when I pass an array to a function?
    It is like when you pass an array to a function, but in this case, my suggestion is to make a loop OUTSIDE the operator >>, and use your existing "read on report" operator >> to read the required number of elements into the array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How would you write a vector of int's to a file? Writing WhetherReports to a file should not be different at all.

    When overloading operators the general guideline is to make the behaviour similar to built-in (or already existing) types.

    Code:
    int i;
    ofstream fin;
    fin >> i;
    Does this work? No, because no file is opened. Therefore operator>> overloads shouldn't open or close any files either.

    If the behaviour you want from these operators is too different, you should consider a named function instead - operator overloading is just syntactic sugar, not something you absolutely must do. It is sweet only as long as the behaviour of the operator is very similar to other types: the user of your class already knows how to use operator>>, so they should be able to use it right away without having to study references on how the function is exactly supposed to be used.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User
    Join Date
    Jun 2008
    Posts
    2
    Just want to check if you have a solution to the problem as I am experiencing the same problem with the No match for operator[] compilation error.

    Would appreciate any help.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For what code? Are you passing a non-array by reference, as the OP?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM