C:我需要从一个文本文件中提取一些数据,但它一直提示我出现分段错误
C: I need to extract some data from a text file and it keeps erroring me with segmetation fault
我用 C:
写了这段代码
#include <stdio.h>
#include <string.h>
struct Values{
char timestamp[21];
int temperature;
};
char *readString(char out[], FILE *fp){// Reading and storing the input values, out = the string that this func returns
int ch, i;
while(EOF!=(ch=fgetc(fp)))
if(ch == '"') break;
for(i=0;EOF!=(ch=fgetc(fp));++i){
if(ch == '"') break;
out[i] = ch;
}
out[i]='[=10=]';
return out;
}
void printValues(struct Values values[], int i){ //just a printing method, for printing the values, i = the amount of values I have
int j;
for(j=0; j<i; j++){
puts(values[j].timestamp);
}
}
int main(void){ //The beginning of the programm, what did you expect?
struct Values values[10];
FILE *file = fopen("hum1.txt", "r" );
int i=0; //the number of every stored value
if (file != NULL ){
char tempString [25];
while(*readString(tempString, file)){ //if the readStrinf outputs "" == "/0" (end of FILE)
strcpy(values[i].timestamp, tempString);
i++;
}
fclose(file);
}
printValues(values, i);
return 0;
}
我的测试 txt 文件工作正常。 测试文本:
{"Something": "Something else", "Something else else"}
{"Hey": "Is anyone there?"}
{"Something else else else"}
但是当我尝试从 "right" txt 文件中读取数据时,它一直向我返回分段错误
"right" txt 文件是这样的:
{"2014-02-13T06:20:00": "93", "2014-02-13T13:50:00": "66", "2014-02-13T06:00:00": "91"}
{"2014-02-14T04:50:00": "87", "2014-02-14T09:50:00": "87", "2014-02-14T12:20:00": "81"}
包含多行(以这种格式)和更多数据
有人能帮帮我吗?我不知道为什么会这样,
提前致谢。
还有。我忘了提到程序
1.reads 一个名为:hum1.txt 的文件。
然后查找第一个 ' " ' 直到下一个 ' " ' 它将数据存储在结构数组
之间
struct Values values[10];
您为 10 个值分配 space。
{"2014-02-13T06:20:00": "93", "2014-02-13T13:50:00": "66", "2014-02-13T06:00:00": "91"}
{"2014-02-14T04:50:00": "87", "2014-02-14T09:50:00": "87", "2014-02-14T12:20:00": "81"}
您的文件有 12 个值。
当调试这样的代码时,您应该做的第一件事是添加检查以查看是否有任何访问越界。那会立即发现问题。
我用 C:
写了这段代码#include <stdio.h>
#include <string.h>
struct Values{
char timestamp[21];
int temperature;
};
char *readString(char out[], FILE *fp){// Reading and storing the input values, out = the string that this func returns
int ch, i;
while(EOF!=(ch=fgetc(fp)))
if(ch == '"') break;
for(i=0;EOF!=(ch=fgetc(fp));++i){
if(ch == '"') break;
out[i] = ch;
}
out[i]='[=10=]';
return out;
}
void printValues(struct Values values[], int i){ //just a printing method, for printing the values, i = the amount of values I have
int j;
for(j=0; j<i; j++){
puts(values[j].timestamp);
}
}
int main(void){ //The beginning of the programm, what did you expect?
struct Values values[10];
FILE *file = fopen("hum1.txt", "r" );
int i=0; //the number of every stored value
if (file != NULL ){
char tempString [25];
while(*readString(tempString, file)){ //if the readStrinf outputs "" == "/0" (end of FILE)
strcpy(values[i].timestamp, tempString);
i++;
}
fclose(file);
}
printValues(values, i);
return 0;
}
我的测试 txt 文件工作正常。 测试文本:
{"Something": "Something else", "Something else else"}
{"Hey": "Is anyone there?"}
{"Something else else else"}
但是当我尝试从 "right" txt 文件中读取数据时,它一直向我返回分段错误
"right" txt 文件是这样的:
{"2014-02-13T06:20:00": "93", "2014-02-13T13:50:00": "66", "2014-02-13T06:00:00": "91"}
{"2014-02-14T04:50:00": "87", "2014-02-14T09:50:00": "87", "2014-02-14T12:20:00": "81"}
包含多行(以这种格式)和更多数据
有人能帮帮我吗?我不知道为什么会这样,
提前致谢。
还有。我忘了提到程序 1.reads 一个名为:hum1.txt 的文件。 然后查找第一个 ' " ' 直到下一个 ' " ' 它将数据存储在结构数组
之间struct Values values[10];
您为 10 个值分配 space。
{"2014-02-13T06:20:00": "93", "2014-02-13T13:50:00": "66", "2014-02-13T06:00:00": "91"}
{"2014-02-14T04:50:00": "87", "2014-02-14T09:50:00": "87", "2014-02-14T12:20:00": "81"}
您的文件有 12 个值。
当调试这样的代码时,您应该做的第一件事是添加检查以查看是否有任何访问越界。那会立即发现问题。