need to read multiple inputs.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • childofthehorn
    New Member
    • Feb 2007
    • 2

    need to read multiple inputs.

    Okay, so I can get this to read the first line of a txt document. I just can't figure out how to get it to read the next lines. I have to use "user_input " in a function. The wait function just waits until the user hits enter. The text document has 11 lines of code. I know this is a total newb question, but I am baffled. The function gets called at the begining of the file.
    Code:
    int file_run(void){
    	char buffer[255];
    	ifstream command_input_file;
    	command_input_file.open("p3test.txt", ios::in);
        command_input_file.getline(buffer, 255);
    	if(command_input_file == NULL){ return 0;};
    	strcpy(user_input,buffer);
    	wait();
    	return 0;
    HERE IS A PART OF MY CODE -SMALL PART:

    Code:
    int parse_command_line(void)
    {				if(COMMANDS_FROM_FILE == 1){
    				file_run();}
    
    				if(COMMANDS_FROM_FILE ==0){
    				cout << "Enter the Commmand: \n";
                    cin.getline(user_input,' ');
    				}
    				// deliminates spaces and uses to separate tokens
    				character_string = strtok(user_input," ");
                    //compares the length of the string to allowable amount of characters.
    				if( (strlen(user_input)) >= MAX_CMD_LINE_LENGTH){
    						cout << "Please Use " << MAX_CMD_LINE_LENGTH << " Characters or Less.\n";
    						return 0;}
    				//cout << "Your Tokens Are: \n \n";
    				j = 0;
    				//This continues to process each character in the string until it reaches the end.
                    while(character_string != NULL)
                    { //Analyzises each character in the string, one at a time.
                        //character[j] = character_string;
    					strcpy(cStrings[j],character_string);
    					//error case if there are more than the 10 allowable tokens.
    					if(j > (MAX_TOKENS_ON_A_LINE)-1){
    						cout << "Please Enter " << MAX_TOKENS_ON_A_LINE << " Tokens or Less.\n";
    						 return 0;
    					}
                        //cout << character[j] << endl;
                        j = j + 1;
                        character_string = strtok(NULL, " ");
    				}  
    
    if
    Last edited by horace1; Feb 6 '07, 07:41 AM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you could have two seperate functions, one which opens the file and another which reads lines of text, e.g.
    Code:
    #include <fstream.h>
    #include<iomanip.h>
    ifstream command_input_file;
    void file_open()
    {
    	command_input_file.open("p3test.txt", ios::in);
    	if(! command_input_file)
    	  {
              cout << " error opeing file " << endl;
              exit(-1);
             }
    }
    
    int file_run(void){
    	char buffer[255];
        command_input_file.getline(buffer, 255);
    	if(command_input_file == NULL){ return 0;};
    	strcpy(user_input,buffer);
    	wait();
    	return 1;  // return 1 for OK 0 for fail
    }
    you keep calling file_run() until it returns 0

    Comment

    • childofthehorn
      New Member
      • Feb 2007
      • 2

      #3
      Thank You very much it was very helpful!

      Doing this fixed it all:

      Code:
      	if(COMMANDS_FROM_FILE == 1){
      					if(k == 1){ 
      						file_open();
      						k = k-1;
      					}
      
      				file_run();}
      
      	if(COMMANDS_FROM_FILE ==0){
      			cout << "Enter the Commmand: \n";
      I had been staring at this for a couple hours yet.....

      Comment

      Working...