473,394 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Reuse of File pointer

Digital Don
Hi,

I have been using C++ for the past 6 months and always had a problem of using the same IFSTREAM variable to read multiple files.

Expand|Select|Wrap|Line Numbers
  1. do//Repeat while quit not entered
  2. {
  3.     ipf.clear();
  4.     cout<<"\nGive file name (Q for Quit):";
  5.     cin>>ipf;
  6.  
  7.     ipfilename.open(ipf.c_str(),ifstream::in);
  8.  
  9.     //Not able to reuse the above pointer with new filename (ipf)
  10.  
  11.     if(ipfilename.fail())
  12.         . . .
  13.     else
  14.     {            
  15.         while(!ipfilename.eof())
  16.         {
  17.             getline(ipfilename,tempread);
  18.             ...
  19.         }
  20.         //ipfilename.clear();//Clearing the file pointer –Didn’t work 
  21.         ipfilename.close();//Closing the file
  22.     }        
  23. }while(!(ipf=="Q" || ipf=="quit" || ipf=="Quit" || ipf=="q"));
  24.  
Please help with problem...I searched a lot in google but couldnt find the solution...

I asked a few friends and they gave me an option of using an array of File Pointers instead of reading...which is a waste of memory I feel. I want to know Why am not able to use this way...what am I doing wrong...

Hoping for solutions...

will be great if solved...

Thank You in advance....

Regards,
Digital Don
Feb 5 '07 #1
4 3164
macklin01
145 100+
Good question.

On the one hand, the istream pointers probably wouldn't take _that_ much memory, but on the other hand, I agree that it's unsatisfactory that you can't take your approach. (Which I feel is more elegant.)

Are you sure this isn't a compiler bug? Have you tried another compiler?

Also, I'd recommend trying a smaller code snippet until you can track something down that works. e.g.,

Expand|Select|Wrap|Line Numbers
  1. ifstream inputfile; 
  2. for( int i=0 ; i < 200 ; i++ )
  3. {
  4.  inputfile.open( "testfile.txt", , ifstream::in );
  5.  inputfile.close();
  6. }
  7.  
See if that compiles and runs cleanly, first. Then, start adding in additional file operations.

Finally, another option you might consider is putting the ifstream constructory inside your loop. Then, the object will be deallocated from memory on each iteration, getting around any memory waste issues, and probably getting around any silly bugs.

At any rate, I find it useful to sometimes first test loops with many object creations and/or destructions and watch the memory useage. It can be very helpful for finding bugs, etc. (Although in that case, it's more for debugging my own classes, and not for searching for potential bugs in a compiler.)

Thanks, and good luck! -- Paul
Feb 7 '07 #2
Hi Macklin

Thank You for your response. I am using Visual Studio 2005 and in that I havent got any compiling error.

It compiles fine and reads input any number of times...

The loop works fine if an invalid filename is given. when first time a valid filename is given it works and reads it...when a different file name is provided it doesnt open the new file, instead it opens the first file or in case of ifstream constructor putting inside loop, it exits abruptly....

Why is what I wanna know.
Thanks again and Hope someone has solution for it...


Good question.

On the one hand, the istream pointers probably wouldn't take _that_ much memory, but on the other hand, I agree that it's unsatisfactory that you can't take your approach. (Which I feel is more elegant.)

Are you sure this isn't a compiler bug? Have you tried another compiler?

Also, I'd recommend trying a smaller code snippet until you can track something down that works. e.g.,

Expand|Select|Wrap|Line Numbers
  1. ifstream inputfile; 
  2. for( int i=0 ; i < 200 ; i++ )
  3. {
  4. inputfile.open( "testfile.txt", , ifstream::in );
  5. inputfile.close();
  6. }
  7.  
See if that compiles and runs cleanly, first. Then, start adding in additional file operations.

Finally, another option you might consider is putting the ifstream constructory inside your loop. Then, the object will be deallocated from memory on each iteration, getting around any memory waste issues, and probably getting around any silly bugs.

At any rate, I find it useful to sometimes first test loops with many object creations and/or destructions and watch the memory useage. It can be very helpful for finding bugs, etc. (Although in that case, it's more for debugging my own classes, and not for searching for potential bugs in a compiler.)

Thanks, and good luck! -- Paul
Feb 8 '07 #3
horace1
1,510 Expert 1GB
before you can use ipfilename again you need to clear the error condition from the eof of the previous file, e.g.
Expand|Select|Wrap|Line Numbers
  1.     do//Repeat while quit not entered
  2. {
  3.     ipf.clear();
  4.     cout<<"\nGive file name (Q for Quit):";
  5.     cin>>ipf;
  6.     cout << ipf << endl;
  7.     ipfilename.clear();   //  clear any error condition!
  8.     ipfilename.open(ipf.c_str(),ifstream::in);
  9.  
  10.     //Not able to reuse the above pointer with new filename (ipf)
  11.  
  12.     if(ipfilename.fail())
  13.        {
  14.         cout << "\nUnable to open file " << ipf << " " << strerror(errno);
  15.         cin.get();cin.get();
  16.         return 1;                                                 // open failed
  17.         }
  18.      else
  19.     {            
  20.         while(!ipfilename.eof())
  21.         {
  22.             getline(ipfilename,tempread);
  23.             cout << tempread << endl;
  24.         }
  25.         //ipfilename.clear();//Clearing the file pointer –Didn’t work ?
  26.         ipfilename.close();//Closing the file
  27.     }        
  28. }while(!(ipf=="Q" || ipf=="quit" || ipf=="Quit" || ipf=="q"));
  29.  
Feb 8 '07 #4
Thank You Man...I used Clear at the end before close and it worked fine...


before you can use ipfilename again you need to clear the error condition from the eof of the previous file, e.g.
Expand|Select|Wrap|Line Numbers
  1. do//Repeat while quit not entered
  2. {
  3.     ipf.clear();
  4.     cout<<"\nGive file name (Q for Quit):";
  5.     cin>>ipf;
  6.     cout << ipf << endl;
  7.     ipfilename.clear(); // clear any error condition!
  8.     ipfilename.open(ipf.c_str(),ifstream::in);
  9.  
  10.     //Not able to reuse the above pointer with new filename (ipf)
  11.  
  12.     if(ipfilename.fail())
  13. {
  14. cout << "\nUnable to open file " << ipf << " " << strerror(errno);
  15. cin.get();cin.get();
  16. return 1; // open failed
  17. }
  18.     else
  19.     {            
  20.         while(!ipfilename.eof())
  21.         {
  22.             getline(ipfilename,tempread);
  23.             cout << tempread << endl;
  24.         }
  25.         //ipfilename.clear();//Clearing the file pointer –Didn’t work ?
  26.         ipfilename.close();//Closing the file
  27.     }        
  28. }while(!(ipf=="Q" || ipf=="quit" || ipf=="Quit" || ipf=="q"));
  29.  
Feb 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: DPfan | last post by:
What's exactly the meaning of "code reuse" in C++? Why such kind of reuse have more advantages over the counterpart in other language like in C? How is "code reuse" realized in C++? By...
11
by: Damon | last post by:
Hi, Can someone point out to me what's wrong with the code below? I'm trying to reuse the deleted pointer but it won't compile. My reason may be wrong, but I thought reusing the pointer would...
3
by: danra | last post by:
Hi, I have a question which seems to me pretty basic, unfortunately I can't seem to figure it out. Let's say I have an abstract base class Vehicle and classes Car and Truck which derive from...
7
by: hank | last post by:
Hi All In the Circular Logging when the Primary Log file fill up, the database manager will creat a secondary log files for the transaction; when this transaction finished, the secondary log...
48
by: avasilev | last post by:
Hi all, my question is: if i allocate some memory with malloc() and later free it (using free()), is there a possibility that a consequent malloc() will allocate memort at the same starting...
4
by: sam | last post by:
hello all, pretty straightforward question here, i think. i'm experimenting with classes and have written a File class that looks like it's worth keeping for future use. however, i cannot work...
19
by: jacob navia | last post by:
There is an interesting discussion running in Slashdot now, about code reuse. The thema of the discussion is here: < quote > Susan Elliot Sim asks: "In the science fiction novel, 'A Deepness...
8
by: WebSnozz | last post by:
I have an application written in C that does a lot of low level stuff. It does a lot of things like casting from void*'s. I want to create a new GUI for it in either C# or MC++, but reuse the...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.