简体   繁体   English

C编程代码不会停止从文本文件读取

[英]C Programming code will not stop reading from text file

I am trying to read data from a text file and print when a person enters a room and when someone exited the room. 我正在尝试从文本文件中读取数据并在有人进入房间和有人离开房间时进行打印。 The text file is data from a sensor our Professor helped us collect. 文本文件是来自我们教授帮助我们收集的传感器的数据。 The file contains angles and ranges detected by the sensor (he spelled “angle” as “angel” in the text file). 该文件包含传感器检测到的角度和范围(他在文本文件中将“ angle”拼写为“ angel”)。

The problem I am having is the program will read the file and print what I want it to but it will not stop reading the file. 我遇到的问题是程序将读取文件并打印我想要的文件,但不会停止读取文件。 I also need to add a counter that will add one each time a person walked in and subtract one every time a person walked out but I cannot add the counter and get it to print until I fix the code and get it to stop reading the text file. 我还需要添加一个计数器,该计数器将在一个人每次走入时增加一个,而每次一个人走出去时将减去一个,但是我无法添加该计数器并将其打印,直到我修复代码并使其停止阅读文本为止文件。 My current code is below. 我当前的代码如下。 Please help me solve this and Thank You!! 请帮我解决这个问题,谢谢!

#include <stdio.h>
int main() {
FILE *fp;
FILE *op;
int time;
int range;
float i;
float angel;
char str;
fp = fopen("/home/chris/Desktop/test.txt", "rt");
op = fopen("/home/chris/Desktop/Pro.csv", "w");
i=0;
while((str = getc(fp)) != EOF) {
fscanf(fp,"range: %d, ",&range);
fscanf(fp,"angel: %f,",&angel);

if (angel == -10.75)
{
if(range<4100) {

printf("Someone has entered the room\n"); 
}
}
if (angel == -10.75)
{
if (range>4100 && range<6000)


printf("Someone has left the room\n");
i++;


}
}

fclose(fp);
fclose(op);
return 0;
}

The return type of getc() is int , but your str variable is char . getc()的返回类型为int ,但您的str变量为char It might be the case that your system uses a value of EOF that is outside the range of a char . 在某些情况下,您的系统使用的EOF值可能超出char范围。

line 13, getc() returns an int. 第13行,getc()返回一个int。 you need to convert it to char 您需要将其转换为char

since the int is not possible you are getting a NULL 由于int是不可能的,你得到一个NULL

fgetc() is a more likely answer. fgetc()是更可能的答案。 look at; 看着; https://stackoverflow.com/questions/1835986/how-to-use-eof-to-run-through-a-text-file-in-c https://stackoverflow.com/questions/1835986/how-to-use-eof-to-run-through-a-text-file-in-c

it better explains the return 它更好地解释了回报

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM