473,378 Members | 1,507 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,378 software developers and data experts.

error checking for opening a file

I m trying to use a iterative function for error checking to open a file?

the problem is that once it enters the error loop , it does not come out .. i think its due to the object (ifile) .. which is not changing ...with input of new filename...
void getfile()
{
char *filename;
filename=new char[256];
cout<<"\nEnter the source file path:";
cin>>filename;
ifile.open(filename,ios::in);
while(!ifile)
{
cerr<<"Error opening file , enter a new path "<<endl;
cin>>filename;
ifile.open(filename,ios::in);
}
ifile.read(filename,ios::in);
}
Jul 21 '07 #1
3 3347
archonmagnus
113 100+
I m trying to use a iterative function for error checking to open a file?

the problem is that once it enters the error loop , it does not come out .. i think its due to the object (ifile) .. which is not changing ...with input of new filename...
void getfile()
{
char *filename;
filename=new char[256];
cout<<"\nEnter the source file path:";
cin>>filename;
ifile.open(filename,ios::in);
while(!ifile)
{
cerr<<"Error opening file , enter a new path "<<endl;
cin>>filename;
ifile.open(filename,ios::in);
}
ifile.read(filename,ios::in);
}
Try using the good() and/or fail() members of the ifstream class as follows:
Expand|Select|Wrap|Line Numbers
  1. void getfile (void)
  2. {
  3.     // ... get the filename...
  4.  
  5.     // ifile.open(filename,ios::in);
  6.     ifstream ifile (filename, ios::in);
  7.  
  8.     while(!ifile.good())
  9.     {
  10.         cerr<<"Error opening file , enter a new path "<<endl;
  11.         cin>>filename;
  12.         ifile.open(filename,ios::in);
  13.     }
  14.  
  15.     // ... read the file
  16. }
  17.  
I believe that should work, although I have not tested it to confirm. For more information, see http://www.cplusplus.com/reference/iostream/ifstream/
Jul 21 '07 #2
Try using the good() and/or fail() members of the ifstream class as follows:
Expand|Select|Wrap|Line Numbers
  1. void getfile (void)
  2. {
  3.     // ... get the filename...
  4.  
  5.     // ifile.open(filename,ios::in);
  6.     ifstream ifile (filename, ios::in);
  7.  
  8.     while(!ifile.good())
  9.     {
  10.         cerr<<"Error opening file , enter a new path "<<endl;
  11.         cin>>filename;
  12.         ifile.open(filename,ios::in);
  13.     }
  14.  
  15.     // ... read the file
  16. }
  17.  
I believe that should work, although I have not tested it to confirm. For more information, see http://www.cplusplus.com/reference/iostream/ifstream/
i have used that too .. but its not helping ... i think the actual object i.e. ifile is not changing ... in this iterative loop
Jul 21 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You need to:

Expand|Select|Wrap|Line Numbers
  1. ifile.clear();
  2.  
to turn off the fail bit. Do this before you try to reopen the file.
Jul 22 '07 #4

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

Similar topics

8
by: middletree | last post by:
I had some text links at the top of all my pages (in one include file), which worked just fine. But I was asked to make it so that people in a certain department, (this is an Intranet app) would...
3
by: windandwaves | last post by:
Hi Gurus Does anyone know how I set the error trapping to option 2 in visual basic. I know that you can go to tools, options and then choose on unhandled errors only, but is there a VB command...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
3
by: Chozomaster747 | last post by:
I'm in my last week in High School and my last program has seemed to have been shot. My teacher blames it on the computers and says that my program should run, but I have an error opening my files. ...
29
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to...
6
by: =?Utf-8?B?UGF1bA==?= | last post by:
I am getting an "Access to the path "xxxx" is denied error. I believe is because the file that I am writing to programatically is being read/written to by another end user. These files have the...
2
by: akhilesh.noida | last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $...
3
by: GazK | last post by:
I have been using an xml parsing script to parse a number of rss feeds and return relevant results to a database. The script has worked well for a couple of years, despite having very crude...
4
Fr33dan
by: Fr33dan | last post by:
Hi, I'm having trouble with a multi-threaded program crashing on a specific machine when the worker thread is not initialized at when the _FormClosing method of my main form called. Here is my...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.