strange behaviour when using fprintf within an if comparison
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int value; //adc bit value int value1; //LSB adc value int value2; //HSB adc value float voltage; float previous_voltage = 0; //used for comparing current and previous voltage value
main(int argc, char **argv) {
FILE *fp; //pointer to file
if (ioperm(base,5,1)) fprintf(stderr, "Couldn't get the port at %x\n", base), exit(1);
fp = fopen("voltage.table", "w"); //open file for writing aquired voltage values
outb(0x20, 0x37A); //set D0-D7 as inputs
while(1) { usleep(50000); //pause system
value1 = inb(base); //read first 8 bits of adc output //value1 ^= 0x80; value2 = inb(base + 1); //read last 2 bits of adc output value2 = value2 & 0xC0; //mask register 37a for adc bits value2 = value2 << 2; //shift last 2 adc bits to correct bit place [bit 8 and bit 9] value2 ^= 0x200; //invert bit 9
value = (int)value1 + (int)value2; //calculate total adc value
The fprintf statement is supposed to print some data to a file, this however doesnt happen, i know that the if block is entered because "is unequal" is printed on the screen. When the fprintf statement is outside of the if block it then prints data to the given ouput file voltage.table. Anybody had any similiar experience.. am i doing something wring with the fprintf function?
thanks for the reply. I however know for certain that the if statement is entered because the "is unequal" statement is printed to the screen when previous_voltage and voltage are unequal.
ya, heres a sample of what it prints to the screen. Where "is unequal" is printed, it ought to write to the file (like it does when there is no if statement).
Code:
--------------------------------------------------------------------------------------------------------
value1 : 0
value2 : 0
value : 0
voltage : 0.000000
previous_voltage : 0.000000
--------------------------------------------------------------------------------------------------------
value1 : 0
value2 : 0
value : 0
voltage : 0.000000
previous_voltage : 0.000000
--------------------------------------------------------------------------------------------------------
is unequal
2.465820
value1 : 249
value2 : 256
value : 505
voltage : 2.465820
previous_voltage : 0.000000
--------------------------------------------------------------------------------------------------------
value1 : 249
value2 : 256
value : 505
voltage : 2.465820
previous_voltage : 2.465820
--------------------------------------------------------------------------------------------------------
value1 : 249
value2 : 256
value : 505
voltage : 2.465820
previous_voltage : 2.465820
--------------------------------------------------------------------------------------------------------
value1 : 249
value2 : 256
value : 505
voltage : 2.465820
previous_voltage : 2.465820
--------------------------------------------------------------------------------------------------------
try "return(0);" in if block after fprintf statement.
if it write anything, then you got the problem . (need to find solution)
else need to find actual problem
are you closing program with control-c ?
if yes use signal handler to close file pointer i.e. fclose(fp) ;
this simple code all make empty file if you close it with ctrl-c
main()
{
FILE *fp ;
fp=fopen("abcd.test","w");
fprintf(fp,"test");
usleep(50000); //pause system
//fclose(fp);
}
yup i'm using ctrl+c to exit, gonna try out the exta bit of code, thanks . however i would find it strange if the file all of a sudden has contents, as when i ctrl+c when the fprintf statement is not within the if statement, then the file is written to.
try "return(0);" in if block after fprintf statement.
if it write anything, then you got the problem . (need to find solution)
else need to find actual problem
yup, when i return(0) then there is a line printed to the file, the stream is therefore lost somewhere, i take it that judges code will help fix that,..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.