Solved

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

Posted on 2000-04-24
7
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Focus not getting shifted out of  editbox 2 69
C Language combined operators 28 123
Would like to move button in a function 3 85
Find Visual Studio Tools 2 124
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

733 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