Thread: Can Not Open File

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    13

    Can Not Open File

    Code:
    [/LEFT]
            
    if(choose=='S'||choose=='D'||choose=='T')
    	{
    	outfile.open(filename,ios::out|ios::app);     
    		   outfile.close();
    			infile.open(filename,ios::in);
                if(!infile)
    	cout<<"Error opening the file"<<endl;
                while(!infile.fail())
    {
    infile>>r_number>>r_name>>r_family>>r_city>>r_telephone>>type>>r_price>>r_time;
    	if(type=='S')
    		{
    			account_s++;
                    }
                    if(type=='D')
                    {
    					account_d++;
                    }
                    if(type=='T')
                    {
                        account_t++;
                    }
                }
                single=single-account_s;
                doubler=doubler-account_d;
                triple=triple-account_t;
                infile.close();
              
    if(n.type_room=='S')
    {
    if(single!=0)
    {
    cout<<"Total single bed room available is "<<single;
      cout<<endl;
                        cout<<"\nHow many single bed room you need?";
                        cout<<endl;
                        cin>>number;     
                        cout<<endl;
                    }
                    else
                    {
    					cout<<"\nSorry,No Room available.";
                        cout<<endl;
                    }
                }
                          cin.get();
                cout<<"First Name : ";
                cin>>n.name;
                cout<<"Family Name : ";
                cin>>n.family;
                cout<<"City : ";
                cin>>n.city;
                cout<<"Telephone : ";
                cin>>n.telephone;
                cout<<"Charge :";
                cin>>n.cost_perday;
                cout<<"Days : ";
                cin>>n.time;
                
    			int flag=0;
                for (int i=1;i<=number;i++)
    			{
    				cout<<"Room NO.: "<<endl;
                    cin>>room;
                    if(n.type_room=='S')
                    {
    					if(room<100 || room>199)
    					{
    						cout<<"You Should Choose The Number Between 100 And 199"<<endl;
                            cout<<endl;
                            i=i-1;
                            continue;
    					}
    					infile.open(filename,ios::in);
    					if(!infile)
    						cout<<"Error opening the file"<<endl;
    					while(!infile.fail())
    					{
    						infile>>r_number>>r_name>>r_family>>r_city>>r_telephone>>type>>r_price>>r_time;
    						if(room==r_number)
    						{
    							cout<<"You Should Choose Other Room.This is occupied."<<endl;
    							cout<<endl;
    							flag=1;
    							i=i-1;
    							break;
    						}
    						else
    						{
    							flag=0;
    						}
    					}
    					infile.close();
    					if(flag==1)
    					{
    						continue;
    					}
    				}


    its not run well, cant open file properly, keep showing
    "Error opening the file" some problem in the loop i cant find out please help

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    1) is 'filename' defined?
    2) does the file exist?

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    after all i redefined fstream outfile,infile; in all the loop its finally work, but still dont know where should i really declare them, but

    thx for remind

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Try "if(infile.fail())" instead.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM