Change the type of c
from char
to int
. EOF
is outside the range of char
.
To clarify, by "change the type", I mean change the declaration to
int c;
not cast the value back to int
after truncating it to char
.
Question
f=fopen("/dev/ttyUSB0","r")
char c;
c=getc(f);
while(c!=EOF) {
c=getc(f);
}
In the code above loop never breaks on beaglebone ubuntu which is arm architecture when I unplug device. The above code worked on my i386 ubuntu desktop.
--EDIT--
Its very weird! Mike's int c
; didn't work. Watch on c
gives c=255
but even when I changed c!=EOF
to c!=255
loop don't break!! I'm very frustrated after trying for an hour!!!
--EDIT--
I tried fstream fin("/dev/ttyUSB0", fstream::in);
. when I unplug the input serial device fin>>c
never returns and CPU usage is 100%!! I expect its a bug!
Solution
Change the type of c
from char
to int
. EOF
is outside the range of char
.
To clarify, by "change the type", I mean change the declaration to
int c;
not cast the value back to int
after truncating it to char
.
OTHER TIPS
In C++ it is easier to use cin.
int main()
{
char ch;
while (true)
{
cin >> ch;
if (!cin.good())
break;
// Do something with you character
}
}
This will stop when con >> ch fails.