//Quick concatenate, I should use concatain but nah.
snprintf(filePath,sizeof(filePath), "%s/%s/%s",FOLDER_MAIN, "test", "data.txt");
fp = fopen(filePath, "r");
if (fp != NULL)
while ((read = getline(&line, &len, fp)) != -1)
if(startsWith(line, "value:"))
removeSubstring(line, "value:");
removeSubstring(line, " ");
removeSubstring(line, "\t");
removeSubstring(line, "\n");
printf("%d\t%s\n", 0, line);
fclose(fp);
//Free line pointer, is it really needed ?
if (line)
free(line);
int StartsWith(const char *a, const char *b)
if(strncmp(a, b, strlen(b)) == 0)
return 1;
return 0;
void removeSubstring(char *s,const char *toremove)
while( s=strstr(s,toremove))
memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
另外,'free(line)'也使程序崩溃。
我添加了removeSubstring和StartsWith函数,希望能有所帮助。总之,我认为这对程序影响不大,因为问题出在FP上......
1
个回答
0
人赞同
你的代码是未定义的行为。
getline的原型是明确的
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
,你必须使用
correct
类型,特别是当你使用指针!