2

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).

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;
}
1
  • Do you really want to be discarding one character of the input on each iteration of the loop? If not, then don't use getc at all. Just check the value returned by fscanf. Commented Apr 26, 2019 at 20:48

2 Answers 2

2

The return type of getc() is int, but your str variable is char. It might be the case that your system uses a value of EOF that is outside the range of a char.

Sign up to request clarification or add additional context in comments.

Comments

1

line 13, getc() returns an int. you need to convert it to char

since the int is not possible you are getting a NULL

fgetc() is a more likely answer. look at; https://stackoverflow.com/questions/1835986/how-to-use-eof-to-run-through-a-text-file-in-c

it better explains the return

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.