当我尝试将char设置为char *时,我遇到了一些麻烦("字符串")
我得到了行,这是我之前提取的所有txt行,现在我试图为我的char *分配一个char来过滤它。
这是我的实际代码:
void TreatDatas(char** lignes, int sizeLignes){ // all the lines, and the size of it.
char** finalArray;
finalArray = malloc(2048 * sizeof(char*));
int sizeOfFinalArray = 0;
int i;
int j;
char* s;
char* savedCurrentString = "";
int sizeOfCurrentString = 0;
for (i = 0; i< sizeLignes; i++){
s = lignes[i];
for (j = 0; j < strlen(s); j++){ // I don't pass the first condition the first loop
if (s[j] == ',' || s[j] == '.' || s[j] == ' ' || s[j] == '\n' || s[j] == ';' || s[j] == ':'){ // Separators list
finalArray[sizeOfFinalArray] = malloc(strlen(savedCurrentString) + 1);
strcpy(finalArray[sizeOfFinalArray], savedCurrentString);
savedCurrentString = "";
sizeOfCurrentString = 0;
}else{
printf("%c , %s \n", s[j], savedCurrentString); // L - ""
printf("%d", sizeOfCurrentString); // 0
strncpy(savedCurrentString, s[j], 1); // error here
}
}
}
free(finalArray);
}
答案 0 :(得分:0)
好的我改变了一下代码,现在它更好了,但似乎复制了一些元素。我不知道为什么。对不起,我是C的开始,但我很好奇,并试图了解它是如何运作的。
void TreatDatas(char** lignes, int sizeLignes){
char** finalArray;
finalArray = malloc(2048 * sizeof(char*));
int sizeOfFinalArray = 0;
int i;
int j;
char* s;
char* savedCurrentString = "";
int sizeOfCurrentString = 0;
for (i = 0; i< sizeLignes; i++){
s = lignes[i];
printf("line : %d \n %s \n", i, s);
StringSpliter(s);
// for (j = 0; j < strlen(s); j++){
// finalArray[sizeOfFinalArray] = malloc(strlen(savedCurrentString) + 1);
// }
}
free(finalArray);
}
char * StringSpliter(char* input){
char separators[5][2] = {" ", ",", ";", ":", "."};
char ** buffer;
int bufferSize = 0;
int i;
for (i = 0; i < 5; i++){
char* token = strtok(input, separators[i]);
printf("%s", token);
while( token != NULL )
{
printf( " %s\n", token );
token = strtok(NULL, separators[i]);
}
}
printf("\n");
return "OSEF";
}