Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2000-04-24
7
Medium Priority
?
241 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
Technology Partners: 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 150 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

636 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