reading in a file and storing the words in a char[]
-
I am reading in a file and trying to put words read in a char[]. My array in within a struct and the notation is:
struct TSimHeader
{
char Name[47];
char Unit[47];
double Min[47];
double Max[47];
int SignalCount;
int SimStatus;
int simdkid;
int modmode;
int write;
int call;
}static TSimHeader_arr[10];This is where I read in the file and try to put the values in the appropiate containers:
string name, unit,min,max,value;
for(int i=0; i<47; i++){ getline(fin, name, ','); if(name == "STOP") break; getline(fin, unit, ','); getline(fin, min, ','); getline(fin, max, ','); getline(fin, value); strcpy(TSimHeader\_arr\[0\].Name\[i\], name.c\_str()); strcpy(TSimHeader\_arr\[0\].Unit\[i\], unit.c\_str()); //TSimHeader\_arr\[0\].Name\[i\] = name; //strcpy(TSimHeader\_arr\[0\].Name, name.c\_str()); TSimHeader\_arr\[0\].Min\[i\] = atof(min.c\_str()); TSimHeader\_arr\[0\].Max\[i\] = atof(max.c\_str()); TSimSignal\_arr\[0\].Value\[i\] = atof(value.c\_str()); sig\_count ++;
}
The error I get is:
:*.cpp(104) : error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Thanks for the help, Steven -
I am reading in a file and trying to put words read in a char[]. My array in within a struct and the notation is:
struct TSimHeader
{
char Name[47];
char Unit[47];
double Min[47];
double Max[47];
int SignalCount;
int SimStatus;
int simdkid;
int modmode;
int write;
int call;
}static TSimHeader_arr[10];This is where I read in the file and try to put the values in the appropiate containers:
string name, unit,min,max,value;
for(int i=0; i<47; i++){ getline(fin, name, ','); if(name == "STOP") break; getline(fin, unit, ','); getline(fin, min, ','); getline(fin, max, ','); getline(fin, value); strcpy(TSimHeader\_arr\[0\].Name\[i\], name.c\_str()); strcpy(TSimHeader\_arr\[0\].Unit\[i\], unit.c\_str()); //TSimHeader\_arr\[0\].Name\[i\] = name; //strcpy(TSimHeader\_arr\[0\].Name, name.c\_str()); TSimHeader\_arr\[0\].Min\[i\] = atof(min.c\_str()); TSimHeader\_arr\[0\].Max\[i\] = atof(max.c\_str()); TSimSignal\_arr\[0\].Value\[i\] = atof(value.c\_str()); sig\_count ++;
}
The error I get is:
:*.cpp(104) : error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Thanks for the help, StevenHere's your problem: strcpy() copies the contents from one character-array to another. in the string class, c_str points to the char-array (which is good). However, you're telling strcpy() to copy the contents of c_str to a single character in the array (Name[i] signifies the i-th character in Name). Looking at your strcuture, it "LOOKS" like Name and Unit are supposed to be character-arrays (strings) of length 47, and there are 10 elements of type TSimHeader, which equates to 10 character-arrays (string) of length 47. If this is the case, you must change the loop for 10 entries instead of 47. string name, unit,min,max,value ; for(int i = 0 ; i < 10 ; i++){ getline(fin, name, ',') ; if(name == "STOP") break ; getline(fin, unit, ',') ; getline(fin, min, ',') ; getline(fin, max, ',') ; getline(fin, value) ; // The following assumes that name and unit // are less than 47 characters long strcpy(TSimHeader_arr[i].Name, name.c_str()) ; strcpy(TSimHeader_arr[i].Unit, unit.c_str()) ; TSimHeader_arr[i].Min[i] = atof(min.c_str()) ; TSimHeader_arr[i].Max[i] = atof(max.c_str()) ; TSimSignal_arr[i].Value[i] = atof(value.c_str()) ; sig_count ++ ; } Again, this is assuming I interpretted this correctly.
-
Here's your problem: strcpy() copies the contents from one character-array to another. in the string class, c_str points to the char-array (which is good). However, you're telling strcpy() to copy the contents of c_str to a single character in the array (Name[i] signifies the i-th character in Name). Looking at your strcuture, it "LOOKS" like Name and Unit are supposed to be character-arrays (strings) of length 47, and there are 10 elements of type TSimHeader, which equates to 10 character-arrays (string) of length 47. If this is the case, you must change the loop for 10 entries instead of 47. string name, unit,min,max,value ; for(int i = 0 ; i < 10 ; i++){ getline(fin, name, ',') ; if(name == "STOP") break ; getline(fin, unit, ',') ; getline(fin, min, ',') ; getline(fin, max, ',') ; getline(fin, value) ; // The following assumes that name and unit // are less than 47 characters long strcpy(TSimHeader_arr[i].Name, name.c_str()) ; strcpy(TSimHeader_arr[i].Unit, unit.c_str()) ; TSimHeader_arr[i].Min[i] = atof(min.c_str()) ; TSimHeader_arr[i].Max[i] = atof(max.c_str()) ; TSimSignal_arr[i].Value[i] = atof(value.c_str()) ; sig_count ++ ; } Again, this is assuming I interpretted this correctly.
You are correct but, There are 47 names, units, min, max. At each (ith) position the data is related. If I just run through the loop 10 times then I won't get the rest of the data all put in the correct array within the struct. I have it working with a 2 dimensional array. I have limited memory so I need to really be sparing on memory. So, I really need to store the names and units in a single dimension array I hope all of that made sense. I have a feeling that it won't work. thanks, Steven