Solved

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

Posted on 2000-04-24
7
194 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
Comment Utility
I don't see anythign that should cause a problem.  Do you know which like it is crashing on?
0
 

Author Comment

by:SmackFu
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:SmackFu
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now