Solved

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

Posted on 2000-04-24
7
205 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

895 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

13 Experts available now in Live!

Get 1:1 Help Now