Thread: input question

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    33

    input question

    I know i can get input from a .dat file like this
    Code:
    do		// get maze file name 		{			 			printf("\nPlease enter a maze file name, if finished enter \"Done\"\n"); 			scanf("%s",dataName); 			if (strcmp(dataName,"Done")==0 | strcmp(dataName,"done")==0) 			{			  				printf("\nThank you for running the maze.\n\n");	// accept either "Done" or "done" 				return 0;														// and exit the program 			} 			else  			{ 				if (!strstr(dataName,".dat"))			// validate the maze file name 				{ 					if (strstr(dataName,".txt")) 					{  						i = strlen(dataName)-4; 						dataName[i]='\0'; 					} 					strcat(dataName,".dat"); 				} 				if ((mazeData=fopen(dataName,"r"))==NULL) printf("\n%s does not exist.\n",dataName); 				else break;		// if valid file, exit get name loop 			}  		} while (strcmp(dataName,"done")!=0);
    but what i was wondering is if there is another way to get the same effect without using scanf?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The main problem you have is that you decided that everything should be on the same line, and as such, rendered everything past do as a comment.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by tree_v1 View Post
    I know i can get input from a .dat file like this
    Code:
    do        // get maze file name         
    {
         printf("\nPlease enter a maze file name, if finished enter \"Done\"\n");
         scanf("%s",dataName);
         if (strcmp(dataName,"Done")==0 || strcmp(dataName,"done")==0)
         {
               printf("\nThank you for running the maze.\n\n");
                  // accept either "Done" or "done"
               return 0; 
                  // and exit the program             
          }
          else
          {
                if (!strstr(dataName,".dat"))     // validate the maze file name                 
                {
                         if (strstr(dataName,".txt"))
                         {
                              i = strlen(dataName)-4;
                             dataName[i]='\0';
                         }
                         strcat(dataName,".dat");
                 }
    
                 if ((mazeData=fopen(dataName,"r"))==NULL)
                     printf("\n%s does not exist.\n",dataName);
                 else 
                     break;        // if valid file, exit get name loop
           }
    } while (strcmp(dataName,"done")!=0);
    but what i was wondering is if there is another way to get the same effect without using scanf?
    Not sure if this is what you're looking for but here it is... passing filename argument to program on command-line, i.e. >maze.exe file.dat
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( int argc, char *argv[] )
    {
        if( argc != 2 )
        {
            printf("Usage: %s filename\n", argv[0]) ;
            return 0 ;
        }
    
        char fname[ 50 ] ;
        strcpy( fname, argv[1] );
    
    printf("%s\n", fname ) ;
    
    
    return 0 ;
    }

    Quote Originally Posted by quzah View Post
    The main problem you have is that you decided that everything should be on the same line, and as such, rendered everything past do as a comment.


    Quzah.
    That is a forum posting problem unfortunately.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    I'm not really sure if that is what i'm looking for or not to be honest.

    after i open up my file i have to use the rest of my code to go through the maze, and im not sure if the code you have given will do this?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tree_v1 View Post
    I'm not really sure if that is what i'm looking for or not to be honest.

    after i open up my file i have to use the rest of my code to go through the maze, and im not sure if the code you have given will do this?
    Post a few lines from the maze data file... with carriage returns this time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic input question
    By crossbone in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2011, 04:51 PM
  2. input question
    By piyush_v in forum C Programming
    Replies: 9
    Last Post: 04-12-2007, 07:09 AM
  3. Input question
    By boxsterh in forum C++ Programming
    Replies: 5
    Last Post: 03-02-2006, 05:30 PM
  4. Input Question
    By graceofdragons in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2005, 01:48 PM
  5. Input/output question
    By vice in forum C Programming
    Replies: 8
    Last Post: 04-27-2005, 08:17 AM