Thread: String searching

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Singapore
    Posts
    24

    String searching

    Code:
    char fileName[100];
    	int a;
    	
    	do{
    			printf("Enter the full file path that you would like to save the file to \n");
    			printf("e.g. C:\\Temp\\Employee.csv\n");
    			printf("NOTE THE .CSV EXTENSION\n\n");
    
    			gets(fileName);
    			a = (stricmp(strpbrk(fileName, ".csv" ), ".csv"));
    			printf("%d", a);
    			if(strcmp(strpbrk(fileName, ".csv" ), ".csv") < 0)
    				printf("Invalid file extension\n\n");
    
    
    	}while((stricmp(strpbrk(fileName, ".csv" ), ".csv") < 0));
    hi, i got a problem here, my a prints 53 no matter what extensions i type instead of a negative number.. can anyone tell me how to overcome it??

    When i enter a.csv it gives me the 0, however i need to allow user to specify the drives when i enter c:\a.txt it gives me 53, when i enter c:\a.csv it also prints 53
    Last edited by Alander; 12-15-2007 at 03:21 AM.

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I don't think you understand what strpbrk does. It returns the location of the first character it finds so it will return the first '.' or the first 'c' or the first 's' or the first 'v' that it finds. In "c:\a.txt", c is the first character found. Use strstr().
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Singapore
    Posts
    24
    Hey thanks, heres the correct code.. cheers

    Code:
    do{
    			printf("Enter the full file path that you would like to save the file to \n");
    			printf("e.g. C:\\Temp\\Employee.csv\n");
    			printf("NOTE THE .CSV EXTENSION\n\n");
    
    			gets(fileName);
    
    	
    			if(strstr(fileName, ".csv" )== NULL)
    				printf("Invalid file extension\n\n");
    
    
    	}while(strstr(fileName, ".csv" )== NULL);

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    gets is very unsafe. Recommend you try fgets instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. Need help on String Searching.
    By RP319 in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2005, 10:33 PM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM