Thread: Can't open save File!!

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    56

    Post Can't open save File!!

    It seems that I can't open the save file, I have a checker in the code and it says Unable to open file. Can someone tell me why I can't open the file so that I can write to it.

    Thank You

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <ctime>
    #include <cstdlib>
    #include <fstream>
    #include <algorithm>
    int getNumbers(int &);
    
    
    using namespace std;
    int main()
    
    {
    
    	//declare Variables
    
    	string clerkid   = "";
    	string name      = "";
    	string address   = "";
    	string city      = "";
    	string state     = "";
    	string hyphfill  = "";
    
    	int zip  = 0;
        int ssn  = 0;
        int visa = 0;
    	int pin  = 0;
    
    	//opens file CCApp.txt
    
    	ifstream inFile;
    	inFile.open("WACKOFILE.dat", ios::in);
    	if(inFile.is_open())
    	
    	{
    
    	   cout << "Unable to open file.  Press <Enter> to quit." << endl;
    	   cin.get();
    	   return 1;
    		    
    		
    getline(inFile,clerkid,'#');
    
    		
    			
    while(!inFile.eof())
    
    		 {	
    	
    			inFile>>ssn;
    		    inFile.ignore(1);
    			getline(inFile,name,'#');
    			getline(inFile,address,'#');
    			getline(inFile,city,'#');
    			getline(inFile,state,'#');
    
    			inFile>>zip;
    			inFile.ignore(1);
    			inFile>>pin;
    			inFile.ignore(1);
    			inFile>>visa;
    			inFile.ignore(2);
    
    
    			cout<<left<<setw(25)<<setfill(' ')<<"NAME"<<"  "<<setw(25)<<"ADDRESS"<<"  "<<left<<setw(16)<<"CITY"<<"  "<<setw(2)<<
    			left<<"ST"<<" "<<setw(5)<<left<<"ZIP";
            
    	
    			cout<<left<<setw(25)<<setfill('-')<<hyphfill<<"  "<<setw(25)<<setfill('-')<<hyphfill<<"  "<<left<<setw(16)<<setfill('-')<<hyphfill<<"  "<<setw(2)<<left<<setfill('-')<<hyphfill<<" "<<
    			setw(5)<<left<<setfill('-')<<hyphfill;
    
    			cout<<left<<setw(25)<<setfill(' ')<<name<<"  "<<setw(25)<<address<<"  "<<left<<setw(16)<<city<<"  "<<setw(2)<<left<<state<<" "<<setw(5)<<right<<
    			setfill('0')<<zip<<endl;
            
    			cout<<setw(7)<<left<<"CLERKID"<<"  "<<setw(9)<<left<<setfill(' ')<<"SSN"<<"  "<<setw(4)<<left<<setfill(' ')<<"PIN"<<"  "<<setw(5)<<left<<setfill(' ')<<"VISA"<<"  "<<endl;
    			
    			cout<<setw(7)<<left<<setfill('-')<<hyphfill<<"  "<<setw(9)<<left<<setfill('-')<<hyphfill<<"  "<<setw(4)<<left<<setfill('-')<<hyphfill<<"  "<<setw(5)<<left<<setfill('-')<<hyphfill<<"  "<<endl;
        
    			cout << setw(7) << left << setfill(' ') << clerkid
    				<<"  "<<setw(9)<<right<<setfill('0')<<ssn<<"  "<<setw(4)<<left<<setfill(' ')<<pin<<"  "<<setw(5)<<left<<setfill(' ')<<visa<<"  "<<endl;
    				
    
    
    		
    			cout<<endl;
    			cout<<endl;
    			
    			getline(inFile,clerkid,'#');
    
    					
    			
    		}
    	}
    	  else
    	  
    	  {cout<<"File not opened"<<endl;}
    	  inFile.close();
    
    	  cout << endl;
    	  return 0;
    
    }//end main

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    56

    Post Driveing me Nuts..

    I have been trying to figure this out for over 2 days now, Any and help needed.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if(inFile.is_open())
    If the file is open you print an error saying that it's not open and terminate?

    >inFile.open("WACKOFILE.dat", ios::in);
    inFile is an ifstream, you don't need to specify an open mode unless you need extra stuff like binary orientation.

    >return 1;
    1 isn't a portable return value. Perhaps EXIT_FAILURE from <cstdlib> would be more appropriate.

    >while(!inFile.eof())
    Bad idea. <stream>.eof() returns true only after a read attempt has failed, so you'll process the last valid record twice.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  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. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM