Thread: need help checking char strings plz

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    53

    Smile need help checking char strings plz

    Hi i just needed a little help in checking strings here is a sample of the code that I need, should be easy to work out what im trying to do.

    Code:
    #include <stdio.h>
    
    
    
    int main(void)
    {
    	char fileName[256];
    	FILE *banana;
    	
    	gets(fileName);
    
    	if (fileName == "banana.dat")
    	{
    		printf("Yes, fileName is banana.dat, now you can do stuff");
    	}
    	return 0;
    }
    unfortunatly when i type banana.dat it skips the if statement (so obviously its returning false) would appreciate the help thanx.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Strings cannot be compared with the operator ==. Use:

    Code:
    if (strcmp(fileName, "banana.dat") == 0)
    Instead.

    Oh, and don't use gets()
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    what should I use instead of gets() could you please write the way you would use a better funciton, thanx again

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dezz101 View Post
    what should I use instead of gets() could you please write the way you would use a better funciton, thanx again
    fgets() is able to take a maximum length, so that makes it safe. If someone enters a filename longer than 256 in your program bad things will happen.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    fgets() is preferrable over gets():

    Code:
    fgets(fileName, 256, stdin);
    Of course, you may encounter the problem that fgets() leaves a newline character ('\n') on your string. strchr() can help you with that. After the call to fgets(), one may do this:

    Code:
    //Searches for a newline character in fileName
    char* location_of_newline = strchr(fileName, '\n');
    //If strchr finds it, then it returns a pointer to its location (otherwise it would return NULL). Replace it with a termination character ('\0')
    if (location_of_newline != NULL) {
       *location_of_newline = '\0';
    }
    EDIT: Oh, and don't forget to include string.h
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    thanx hopefully i'll be able to understand that other bit, but I have tried it and it works a treat so thankyou

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Umm, why involve the user at all if you just want filename to be "banana.dat"?

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    Iv realised that what im trying to do wont work, I need the user to enter a file name to open and then open that file for reading. Something like this

    dataFile = fopen(("&#37;s", &fileName), "r");

    yes I relise this wont work just trying to help you guys understand what I'm trying to do

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    thats after
    insert
    Code:
    {
    fgets(fileName, 256, stdin);
    //Searches for a newline character in fileName
    char* location_of_newline = strchr(fileName, '\n');
    //If strchr finds it, then it returns a pointer to its location (otherwise it would return NULL). Replace it with a termination character ('\0')
    if (location_of_newline != NULL) {
       *location_of_newline = '\0';
    }
    p.s. how do you wrap code tags in quick reply??

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    lol found the code tags

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think what you are not understanding is the fact that a user is useless when it comes to making a program function as it is supposed to. More of your program is dedicated to undoing things that the user breaks. GUI's focus on restricting how the user can interfere with what a program is designed to do. Thus, if you are always getting banana.dat, why even ask the user?

    To answer your question, however:

    Example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(void)
    {
    	char fileName[256];
    	FILE *banana;
    	
    	fgets(fileName, sizeof(fileName), stdin);
    	strtok(fileName, "\n");
    
    	if (stricmp(fileName, "banana.dat") == 0) // I am using a non-standard function here... sue me.
    	{
    		printf("Yes, fileName is banana.dat, now you can do stuff");
    	}
    	return 0;
    }

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    But my program must open any file not just banana.dat, the user should be able to paste a dat file into the program directory and then use the program to perform tasks. sorry for not making that clear

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok... Well what exactly do you want then? I rewrote your code to do exactly what you asked for it to do in the first place, and now it isn't robust enough for your liking. How about something like this?

    Example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(void)
    {
    	char fileName[256];
    	FILE *banana;
    	
    	fgets(fileName, sizeof(fileName), stdin);
    	strtok(fileName, "\n");
    
    	if (strstr(fileName, ".dat") != 0)
    	{
    		printf("Yes, fileName is *.dat, now you can do stuff");
    	}
    	return 0;
    }

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    dont worry I think I got it thanx

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok..

    Perhaps?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(void)
    {
    	char fileName[256];
    	FILE *banana;
    	
    	fgets(fileName, sizeof(fileName), stdin);
    	strtok(fileName, "\n");
    
    	if (strstr(fileName, ".dat") != 0)
    	{
    		printf("Yes, fileName is *.dat, now you can do stuff");
    		banana = fopen(fileName, "rb");
    
    		if(banana)
    		{
    			// do stuff here.
    
    			fclose(banana);
    		}
    	}
    	return 0;
    }
    I know you said you think you have it, but I figured I'd ask.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM