Thread: File I/)

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    74

    File I/)

    I need to ask the user to type in the file name, and open that file. Here are my functions, can you tell me what I'm doing wrong?


    Code:
    long AbsoluteLoader (char *ExecutableFilename)
    {
    	int status = 0;
     	tfile.open (ExecutableFilename);
    
    	if (! tfile) //If File cannot be opened
     	{	
     		cout << "Unable to open";
    		status = -1;
     		return status;
     	};
    	
     	while ( ! tfile.eof() ){
    
     		tfile>>addr >>content;
    		
    		//Valid address - load memory
    		if(addr >= 0 && addr < 10000)
    		{
                      hypomem[addr] = content;			
    		}  
    		
    		// End of Program
    		if (addr == -9999) //If PC contains value -9999, End Program
    		{
    			cout << "End of Program" << endl ;
    			status = 0;
    			return 0;
    		}
     	}
    
    	tfile.close();
    	return status; 
    }
    
    int main()
    {
    
    	//	Read command (Interrupt to the operating system) from the user
    	cout << "What is the program to run?";
    	cout << endl <<  endl;
    	cin >> ExecutableFilename;
    
    	//Load the program
    	PC = AbsoluteLoader(ExecutableFilename);
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What are all of those variables defined as? What errors are you getting?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    74
    I am getting ExecutableFilename as undeclared identifier as it's written here. I'm not sure where I declare it. I tried global and local without luck. I'm not good with pointers, so that is where I am confused. My problem is just with what variable to pass the user input filename, and how to pass that to the AbsoluteLoader function to open it.

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    In main your trying to input into ExecutableFilename

    In main you have
    Code:
    cin >> ExecutableFilename;
    From my understanding you would need to declare it as either a string or character array to hold the filename that you are typing in at the prompt.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    74
    I've tried that, and I"m getting error about converting char to char*. I'm not sure how to properly declare the variable, load the user input into that variable, and pass it to AbsoluteLoader function.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    74
    It works if I do:

    Code:
    int main()
    {
    	Filename = "test.txt"; 
    	//Load the program
    	PC = AbsoluteLoader(Filename);
    	return 0;
    }
    But not if I do...

    Code:
    int main()
    {
    
    	cout << "What is the program to run?";
    	cout << endl <<  endl;
    	cin >> Filename;
     
    	//Load the program
    	PC = AbsoluteLoader(Filename);
    	return 0;
    }
    I'm getting unable to open file error.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You need to declare Filename as either a char array or string:

    Either:
    char Filename[100];

    Or:
    string Filename;

    The string is better, but you would have to change some other things in your code.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    74
    Thanks Swoopy!!! That works! I appreciate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM