-
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;
}
-
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);
-
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
-
>>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.
-
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.
-
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.
-
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.. :D he he.. thnx again
-
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;
}
-
thanks again for all ur help mr. anciend dragon..........
i guess ill be downloading a new compile soon