help with strcmp

This is a discussion on help with strcmp within the C Programming forums, part of the General Programming Boards category; Hello.. havent used C in a while... got a question about using the strcmp function to compare two strings, one ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    12

    help with strcmp

    Hello.. havent used C in a while... got a question about using the strcmp function to compare two strings, one inputted froma file and the other by a user... Ive included part of the code below..

    my problem is that the program seems to crash when it reaches the line with the strcmp.......... am I passing the right parameters to the strcmp function? thanks

    here's the code:

    Code:
    #include <stdio.h>
    #include <string.h>
     
    
    struct data {
    	char date[10];
    	double price;
    };
    
    typedef struct data Data;
    
    int Write_Data( Data data_1[], int size);
    
    int main()
    {
    	int i = 0, test;
    	Data price_data[252];
    	char from[10], to[10];
    	
    	
    	Write_Data( price_data, 252 );
    	
    	printf("Please enter the  start date in mm/dd/yyy format.\n");
    	scanf("%s", from);
    
    	printf("Please enter the end date in mm/dd/yy format.\n");
    	scanf("%s", to);
    
    	test = strcmp( from, price_data[i].date ); 
    	
    	
    	return 0;
    }
    
    int Write_Data( Data data_1[], int size )
    {
    	FILE *cfptr;
    	Data a = {"",0.0};
    	int i = 0;
    
    	if (( cfptr = fopen("SPXPricesFromCBOE.dat","rb")) == NULL)
    		{ printf("File could not be opened"); }
    	else {
    		while ( !feof( cfptr)) {
    			fscanf( cfptr, "%s%lf", &a.date, &a.price);
    			data_1[i] = a;
    			i++;
    		}
    	}
    	fclose( cfptr);
    	return 0;
    	
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    1. it is not reserving enough space for the string's null terminator. variables from and to are too small.

    2. Don't use scanf() to get strings from the keyboard because it causes buffer overflow. use fgets() instead
    Code:
    fgets(from,sizeof(from),stdin);

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    hi, thanks for the help...

    still havin problems though...... i get an error message everytime the program hits the strcmp function........

    i know that the prototype of the strcmp is

    int strcmp( const char *s1, const char *s2);

    should i change the declaration of

    char from[12]; to char *from[12]; ?

    I was under the impression that char from[12] is ok... thnx

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>fscanf( cfptr, "%s%lf", &a.date, &a.price);

    The problem might be here -- look at the datafile using Notepad.exe (or other text editor). Are there any spaces at all in the line? with "%s" scanf() reads up to the first space which will probably overflow the a.date buffer. When writing the strings make sure you write at least one space between the date string and the price.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    hi......

    yup.. the format of the file.txt is.. there is a space in between the date and the prices
    the function that takes the stuff from the file seems to work fine.. all the dats and prices are printed out fine.
    the program only stops working once it reaches the strcmp function... and a window pops up and says that the .exe file has encountered a problem and needs to close.

    so its ok to use char from[50] as the parameters to the strcmp function? thanks again for all ur help

    file. txt format

    mm/dd/yyy 1923
    mm/dd/yyy 2000
    etc.
    etc.
    Last edited by blork_98; 02-21-2006 at 04:31 PM.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    how large is that file? Are there more than 252 lines? display the value of the variables are they are being read from the file. And again just before the strcmp() so that you can see what is being passed to strcmp(). Your program is trashing memory someplace, probably in Write_Data() or some other function that you have not posted.

    you didn't mention what compiler/operating system you are using. If you have a good compiler it probably has a good debugger that you should be using.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    hi... thnx for the help..

    I pretty much posted the whole program that I have written for now in the earlier post. The file has only 252 lines. I did print the
    price_data array.. and it shows the desired results.

    Im thinking there is a problem in the write_data function, but have no idea since Im not to familiar working with files.

    Im using Microsoft Visual C++ 6.0 Intro Edition ( the one u can get free from the Deitel Books). I guess I gotta go learn how to use debug on it.. wanted to write it on Linux but my cousin wont let me install it on his computer.. he he.. thnx again
    Last edited by blork_98; 02-21-2006 at 05:00 PM.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    This works without error. You might want to toss that compiler out and get a free modern one, such as Dev-C++ from www.bloodshed.net or Visual C++ 2005 Express from Microsoft. The compiler you are using is pretty old, has a few bugs, and does not conform to current C and C++ standards (its older than those standards).

    Code:
    #include <stdio.h>
    #include <string.h>
     
    
    #include <stdio.h>
    #include <string.h>
     
    
    struct data {
    	char date[12];
    	double price;
    };
    
    typedef struct data Data;
    
    int Write_Data( Data data_1[], int size);
    
    int main()
    {
    	int i = 0, test;
    	Data price_data[252];
    	char from[12] = {0}, to[12] = {0};
    	
    	
    	Write_Data( price_data, 252 );
    	
    	printf("Please enter the  start date in mm/dd/yyy format.\n");
    	fgets(from,sizeof(from),stdin);
    	i = strlen(from)-1;
    	if(from[i] == '\n')
    		from [i] = 0;
    
    	printf("Please enter the end date in mm/dd/yy format.\n");
    	fgets(to,sizeof(to),stdin);
    	if(to[i] == '\n')
    		to [i] = 0;
    
    	i = 0;
    	printf("from: '%s', price_data[%d].date = '%s'\n",from,i,price_data[i].date);
    	test = strcmp( from, price_data[i].date ); 
    	
    	printf("test = %d\n",test);
    	getchar();
    	return 0;
    }
    
    int Write_Data( Data data_1[], int size )
    {
    	FILE *cfptr;
    	Data a = {"",0.0};
    	int i = 0;
    
    	if (( cfptr = fopen("D:\\dvlp\\test\\SPXPricesFromCBOE.dat","rb")) == NULL)
    		{ printf("File could not be opened"); }
    	else {
    		while ( i < size && !feof( cfptr)) {
    			fscanf( cfptr, "%s%lf", &a.date, &a.price);
    			data_1[i] = a;
    			i++;
    		}
    		fclose( cfptr);
    	}
    	return 0;
    	
    }
    Last edited by Ancient Dragon; 02-21-2006 at 06:45 PM.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    thanks again for all ur help mr. anciend dragon..........
    i guess ill be downloading a new compile soon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 05:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01:07 PM
  4. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 12:04 PM
  5. strcmp, any borders?
    By demonus in forum C Programming
    Replies: 3
    Last Post: 11-15-2002, 02:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21