[英]Program doesnt want to read file
this is my struct 这是我的结构
typedef struct {
char mmsi[10];
char name[20];
double latitude;
double longitude;
int course;
double speed;
}Vessel;
this is my function which doesnt want to work 这是我的功能,不想工作
void searchByLatLong(double latitude, double longitude){
FILE * file;
struct dirent *drnt;
DIR * dir = opendir("./text");
char *path = (char *)malloc(19);
Vessel *vessel = (Vessel *)malloc(sizeof(Vessel));
while((drnt = readdir(dir)) != NULL) {
if(strcmp(drnt->d_name,".") && strcmp(drnt->d_name,"..")) {
strcpy(path,"text/");
strcat(path,drnt->d_name);
file=fopen(path, "r");
fscanf(file," %s %[a-zA-Z0-9 ]19s %lf %lf %d %lf", &vessel->mmsi,&vessel->name,&vessel->latitude,&vessel->longitude,&vessel->course,&vessel->speed);
// if (mmsi+".txt" == drnt->d_name){
printf("%s\n%s\n%lf\n%lf\n%d\n%lf\n\n",vessel->mmsi,vessel->name,vessel->latitude,vessel->longitude,vessel->course,vessel->speed);
//}
fclose(file);
}
seekdir(dir, telldir(dir));
// if(this->mmsi == mmsi){
// printVessel();
// }
}
closedir(dir);
}
When i try to load txt file it loads only two first strings then after it theres some rubbish from memory. 当我尝试加载txt文件时,它只加载两个第一个字符串,然后它从内存中加入一些垃圾。 Loading the data to another variables changes nothing ;/ This is a sample txt file which should be loaded:
将数据加载到另一个变量不会改变任何内容; /这是一个应加载的示例txt文件:
3
RMS Titanic
22.222
33.333
4
5.9
The problem is with your format string. 问题出在你的格式字符串上。 The correct format string is:
正确的格式字符串是:
" %s %19[a-zA-Z0-9 ] %lf %lf %d %lf"
The field width goes before the conversion specifier. 字段宽度在转换说明符之前。 Also, the [...] sequence is a conversion specifier, just like 's'.
此外,[...]序列是一个转换说明符,就像's'一样。 The problem you're seeing is that
fscanf()
processes the '3' because it matches the first %s
. 你看到的问题是
fscanf()
处理'3'因为它匹配第一个%s
。 Then it processes the 'RMS Titanic' because it matches %[a-zA-Z0-9 ]
but then processing stops because there is no '19s' in the input. 然后它处理'RMS泰坦尼克号',因为它匹配
%[a-zA-Z0-9 ]
但随后处理停止,因为输入中没有'19s'。 At this point the remaining arguments are uninitialized. 此时剩余的参数未初始化。
You should check the return value from fscanf()
. 你应该检查
fscanf()
的返回值。 It will tell you how many conversions were actually performed. 它会告诉您实际执行了多少次转换。
Thanks for posting an interesting question; 感谢您发布一个有趣的问题; I learned about
fscanf()
and the []
notation it accepts. 我了解了
fscanf()
和它接受的[]
符号。
The []
notation specifies that a string is being read, therefore, the s
you have appended to it is considered a literal character that should match. []
表示法指定正在读取字符串,因此,您附加到它的s
被视为应该匹配的文字字符。 Similarly, the width-specifier you have provided, 19
should appear prior to the []
. 同样,您提供的宽度说明符
19
应出现在[]
之前。
Your current code would start working if you had a ship named, eg, "RMS Titanic19s". 如果您有一艘名为“RMS Titanic19s”的船,您当前的代码将开始工作。
Change your fscanf to: 将您的fscanf更改为:
fscanf(file," %s %19[a-zA-Z0-9 ] %lf %lf %d %lf",
vessel->mmsi,vessel->name,&vessel->latitude,
&vessel->longitude,&vessel->course,&vessel->speed);
and your code will start working. 并且您的代码将开始工作。
Notice I fixed some compile warnings by dropping the superfluous &
from the char []
members mmsi
and name
-- these already point to the buffers you wish to fill. 注意我通过从
char []
成员mmsi
和name
删除多余的&
修复了一些编译警告 - 这些已经指向你想要填充的缓冲区。 You don't need &
in front of them. 你不需要
&
在他们面前。 A pedagogical alternative form is &vessel->mmsi[0]
-- the address of the first character of mmsi
. 教学的替代形式是
&vessel->mmsi[0]
- mmsi
的第一个字符的mmsi
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.