Solved

How do I open a filestream and read in the data?

Posted on 2000-04-24
7
222 Views
Last Modified: 2010-04-02
Alright, I am trying to write a program where the user reads in a file name via a function, and then the compiler reads in the next data from that file.  Here is my function:

void InteractiveOpen (ifstream & theIFStream)  // OUT: input file stream
{
   string inputFileName;  // input file name

   cout << "\nEnter the name of the input file: ";
   cin >> inputFileName;

   theIFStream.open(inputFileName.c_str());

   if (!theIFStream.is_open())
   {
      cerr << "\n***InteractiveOpen(): unable to open "
           << inputFileName << endl;
      exit(1);
   }  // end if
}  // end InteractiveOpen


And I'm doing this inside a class named VideoStore, here's the Read function:

void VideoStore::Read()
{
      vector <VideoTape> inCollection;
      VideoTape input;
      int inCopies,
            inTotal;
      ifstream in;

      InteractiveOpen(in);

    in >> inCopies;                                         // read the values

    for (int c = 0; c < inCopies; c++)          // from the stream
    {   in >> input;
         inCollection.push_back(input);
    }

      for (c = 0; c < inCopies; c++)
      {
            inTotal += inCollection[c].Copies();
      }

    myTotalCopies = inTotal;
    myTotalRentedCopies = 0;
    for (int i = 0; i < inCopies; i++)
         myCollection[i] = inCollection[i];

}  // end Read


It opens the file, but thebn it aborts my program saying that an illegial operation has been performed.  

I noticed in the function definition OF A DIFFERENT class, I used istream, and inside of THIS read function, I'm using ifstream.  I'm unsure if this is the problem.  THANKS!!!

0
Comment
Question by:SmackFu
  • 5
  • 2
7 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2744508
I don't see anythign that should cause a problem.  Do you know which like it is crashing on?
0
 

Author Comment

by:SmackFu
ID: 2745229
Well, I know it makes it through InteractiveOpen.  It dies when it's back to the Read Function.  
0
 
LVL 22

Expert Comment

by:nietod
ID: 2745245
The likely cuprits are then

   in >> input;

Is there a problem with operator >> for a VideoTape?  (You didn't post the code, so I can't say.)

or

inCollection.push_back(input);

This will use the copy constructor for the VideoTape class.  Did you write a copy constructor for it?  If so, does it work correctly?  If not, does it need one?  If it has pointers it probably needs one.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:SmackFu
ID: 2745454
I am still having problems.  :(

Is there something wrong regarding the "istream" and the "ifstream"?  I don't understand the difference between the two.  Could that be a reason for illegal operation?  It seems like it still doesn't read in the input after the file has been opened.  I think there may be something wrong with my VideoTape class Read function:


void VideoTape::Read (istream & in)
{
    string inTitle,
            input,
            inGenre;
      vector <string> inCast;
      int inCopies,
            numCast;

   in >> inTitle >> inGenre >> numCast;     // read the values

   for (int c = 0; c < numCast; c++)          // from the stream
   {   in >> input;
         inCast.push_back(input);
   }
   
   in >> inCopies;

   myTitle = inTitle;
   myGenre = inGenre;
   myCopies = inCopies;
   myRentedCopies = 0;
   for (int i = 0; i < numCast; i++)
         myCast[i] = inCast[i];

}  // end Read


Ok, if I change my input to this for the vector <VideoTape> inCollection will I need an operator= function?  

for (int c = 0; c < inCopies; c++)          // from the stream
    {   input.Read(in);
         inCollection[c] = input;
    }


If I do have to create an operator= function, what am I doing wrong here?


VideoTape VideoTape::operator=(VideoTape rightOperand)
{
      string inTitle,
             inGenre;                  
        vector <string> inCast;
        int inCopies,
              inRentedCopies;

      inCast = rightOperand.Cast();
      inGenre = rightOperand.Genre();
      inTitle = rightOperand.Title();
      inCast = rightOperand.Cast();
      inCopies = rightOperand.Copies();
      //myRentedCopies = rightOperand.RentedCopies();

   return VideoTape(inTitle, inGenre, inCast, inCopies);

}  // end operator=



Thanks a lot for all your help!
0
 
LVL 22

Expert Comment

by:nietod
ID: 2745663
>> I don't understand the
>> difference between the two
Istream is an abstract base class that reads  information from "something".  But that soemthing varies depending on the actual class type of the stream object.    ifstream is a derived class of istream.  it reads information from a file.  i.e. for an ifstream, the "something" is a file.

>> will I need an operator= function?  
Yes.

I don't know anything about the Video Tape class.  You never posted its decinition.   But operator = should be nore like

VideoTape &VideoTape::operator=(const VideoTape &rightOperand)
{
   Cast = rightOperand.Cast();
   Genre = rightOperand.Genre();
   Title = rightOperand.Title();
   Copies = rightOperand.Copies();

   return *this;
}  // end operator=

However this is probably only needed if the VideoTape class has data members that are pointers.  Does it?   Does the VideoTape class have a copy constructor?  Usually if you need a copy constructor you need an operator = and vice versa.  Usually if you don't need one you also don't need the other.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2745680
I see the problem.  (I think,  I still have to guess at your class definitions!)

For some reason you seem to be reading the information into temporary variables and the information to the object.  That is wasteful, but is not inherintly a problem.  However you made it a problem because you have the code

for (int i = 0; i < numCast; i++)
    myCast[i] = inCast[i];

This is to copy the infor from the temporary inCast to myCast which must be data member of the class.  the inCast[i] is fine since you added numCast entries to inCast[].  but you haven't added entries to myCast, so myCast[i] might fail because i might be too big for myCast[].

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2745686
The best way to fix this is to read the information directly into the object, like

void VideoTape::Read (istream & in)
{
   int numCast;

   in >> myTitle >> myGenre >> numCast;     // read the values

   // you may want to clear MyCast[] so it has not entries before
   // you start addint to it, this woudl be done with
   // MyCast.clear();
   for (int c = 0; c < numCast; c++)     // from the stream
   {  
      string input;

      in >> input;
      MyCast.push_back(input);
   }
                         
   in >> MyCopies;
   myRentedCopies = 0;
}  // end Read
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question