Thread: string comparing

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Question string comparing

    I can’t get my head around this problem. In the following code I want to enter a string that is a filename, for example hello.txt, myprog.bin assuming that they both exist on my c drive. However I want the program to accept .bin, .txt files only. If you don’t enter the 2 extensions I mentioned an error is displayed. Im unsure on how to do the decision making part with the file extensions. I think there should be a for loop to loop around string or strcpy command used within the program to compare extensions but not to sure. I’ve attached a piece of code that relates to it. Any help would be grateful.


    #include <stdio.h>
    #include <conio.h>
    #include <string.h>

    main()
    {
    char string[10];
    int result;

    printf("Enter filename : "); //enter filename - example hello.txt would be valid
    gets(string); //get file name


    if (string != //unfinished here, unsure how to test condition
    printf("Limit your entries to .bin, .txt extensions only"); //error message
    else
    printf("You entered %s with the correct extension\n", string); //otherwise prints correct text to screen
    getch();
    }

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Must use strcmp (or testing each array element). When you use the array ("string"), you are testing the addresses. You must do something like:
    Code:
    if (strcmp(string1, string2) == 0) // the strings are equal
        // ...etc...
    Yes, if the strings are equal, strcmp returns 0.
    1978 Silver Anniversary Corvette

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    I was playing with your code, and to my shock, I couldn't get it to work, here's what I've got.

    Code:
    #include <stdio.h> 
    #include <conio.h> 
    #include <string.h> 
    
    
    int getStringLength(char inputString[BUFSIZ])
    {
     int i = 0;
     do
     {
      i++;
     }
     while(inputString[i]);
     return i;
    }
    
    int validExtension(char fileName[BUFSIZ])
    {
     int fileLength = getStringLength(fileName);	
    
     char goodext1[4] = ".txt";
     char goodext2[4] = ".bin";
     char extension[4];
     extension[0] = fileName[fileLength-4];
     extension[1] = fileName[fileLength-3];
     extension[2] = fileName[fileLength-2];
     extension[3] = fileName[fileLength-1];
    
     printf("%s",extension);
     if(strcmp(goodext1,extension) || strcmp(goodext2,extension))
     {
      return 0;
     }
     else
     {
      return -1;
     }
    }
    
    int main(void) 
    { 
    char string[BUFSIZ]; 
    int result; 
    
    printf("Enter filename : "); //enter filename - example hello.txt would be valid 
    gets(string); //get file name 
    
    
    if (!validExtension(string))  
    printf("Limit your entries to .bin and .txt extensions only"); //error message 
    else 
    printf("You entered %s with the correct extension\n", string); //otherwise prints correct text to screen 
    getch(); 
    return 0;
    }

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Well, just glancing at your code for a couple of seconds, I founds some stuff (maybe you intended, maybe not). You use the constant looking constant BUFSIZE, but I don't see it declared anywhere.

    Also, you may be a little confused with the return value of strcmp here:
    Code:
    if(strcmp(goodext1,extension) || strcmp(goodext2,extension))
    Are you trying to test if these strings are alike? Because, if two strings are the same, strcmp returns 0, not a non-zero number. So, if you want to test if they are equal, it should be:
    Code:
    if(!strcmp(goodext1,extension) || strcmp(!goodext2,extension))
    Or more easier readably:
    Code:
    if((strcmp(goodext1,extension) == 0) || (strcmp(goodext2,extension)==0))
    Tell me if this is the problem. If not I'll study the code a little harder.
    1978 Silver Anniversary Corvette

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    nope that ain't the problem..

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    strcmp will not help you in this case. You need to search the string for the substring extensions.

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
    	char a[30];
    
    	do
    	{
    		printf("Enter file path: ");
    		fgets(a,30,stdin);
    		a[strlen(a) - 1] = '\0';
    
    	}
    	while ( strstr(a,".txt")  == NULL && strstr(a,".bin") == NULL );
    
    	printf("\n%s",a);
    	return 0;
    }
    Last edited by Troll_King; 01-20-2002 at 01:11 PM.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    yeah but then if it's called a.txt.exe ...

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Nope, not sure what you are talking about Brian, this is for a datafile, and 'a' is an array variable name. If you study the code carefully you can figure it out.
    Last edited by Troll_King; 01-20-2002 at 01:10 PM.

  9. #9
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    if they try to open "hello.txt description.exe", it will think it is correct, it should only test the last 4 chars

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    If the file has a .txt or .bin anywhere than the path is accepted. If they actually screwed up and have a file called C:\directory\textfile.txt.exe than it will accept that but when you try to open that file with:

    FILE *fptr = fopen(a,"r");
    if(fptr == NULL) exit(1);

    Than fopen wil catch it so long as the file pointer is checked. If you want to check the last 4 characters in the filepath than use a pointer, but that is ugly code

    //point to the end of a
    char *ptr = a + strlen(a);
    //loop backwards 4 characters and validate.
    // *(--ptr);
    Last edited by Troll_King; 01-20-2002 at 02:38 PM.

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    another stab at it

    Try this

    Code:
    //trap the input in the char array 
    
    
    //get the index of where the extension begins
    iExtPosition = strlen(szInputString) - 4;
    
    if( strcmp( &(szInputString[iExtPosition]), ".txt") == 0 )
        //this is a txt file
    else if( strcmp( &(szInputString[iExtPosition]), ".bin") == 0)
       //this is a bin file
    else 
       //it is neither type of file
    zMan

  12. #12
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Re: another stab at it

    Good looking code! I agree with your algorithm. Good job!
    1978 Silver Anniversary Corvette

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    Brian,

    One thing I noticed:
    Code:
    char goodext1[4] = ".txt";
    char goodext2[4] = ".bin";
    You are writing beyond these two char arrays. They should both be declared as having 5 chars instead of 4.

    Another thing:
    You are treating "extension" as if it is null terminated, which it is not.
    Code:
     extension[0] = fileName[fileLength-4];
     extension[1] = fileName[fileLength-3];
     extension[2] = fileName[fileLength-2];
     extension[3] = fileName[fileLength-1];
    
     printf("%s",extension);
     if(strcmp(goodext1,extension) || strcmp(goodext2,extension))
     ....
    Either declare extension as "char extension[5]" and add a null "extension[4] = '\0' "
    -or-
    printf("%4.4s", extension) and use strncmp's instead of strcmp's

    You need to also use && instead of || since one of the strcmp's will return a 1 if the other returns a 0.

    Hope this helps.

  14. #14
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    You are writing beyond these two char arrays.
    Actually, the initialization is legal, but you are correct: there is no room for the null zero. The quickest fix here is:

    Code:
    char goodext1[] = ".txt";
    char goodext2[] = ".bin";
    Cheers,
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Comparing string contents
    By Slavakion in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2004, 12:14 PM