LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   strange behaviour when using fprintf within an if comparison (https://www.linuxquestions.org/questions/programming-9/strange-behaviour-when-using-fprintf-within-an-if-comparison-723327/)

itchy8me 05-03-2009 11:04 AM

strange behaviour when using fprintf within an if comparison
 
hi there, i have the following code :

PHP Code:

#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/parport.h>
#include <linux/ppdev.h>
#include <sys/types.h>
#include <sys/io.h>

#include <time.h>

#include <stdio.h>
#include <stdlib.h>

#define base 0x378        // I/O address to read

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 argcchar **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(0x200x37A);                            //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


        //system("clear");
        
voltage value*0.0048828125;

        if (
voltage != previous_voltage)
        {
                
printf ("\nis unequal\n");
                
fprintf(fp"%f\n"voltage);
        }
        
        
printf("\nvalue1 : %i\nvalue2 : %i"value1value2);
        
printf("\n\nvalue : %i\nvoltage :\t\t%f\nprevious_voltage :\t%f\n"valuevoltageprevious_voltage);
        
previous_voltage voltage;
    }


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,
wernher

judge312 05-03-2009 12:04 PM

try
printf ("\n %f is unequal\n",voltage);
in if loop

I think its null != null condition, which is always true.

itchy8me 05-03-2009 12:12 PM

hoi,

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.

judge312 05-03-2009 12:17 PM

but what is prints ?
is there any value in voltage variable ?
Can you share a sample value in voltage variable ?

dmail 05-03-2009 12:33 PM

I do not see the problem of the fprintf statement but I do see other problems
Quote:

main(int argc, char **argv)
Invalid main must return an int.
Quote:

fp = fopen("voltage.table", "w");
Could theoretically fail check the return value.
Quote:

value2 = value2 << 2;
Undefined there is a signed bit involved here.
Quote:

value = (int)value1 + (int)value2;
Why the casts? both values are of type int already.
Quote:

voltage = value*0.0048828125;
Truncation. int promoted to double then multiplied by double and truncated to float.
Quote:

if (voltage != previous_voltage)
This may not be what you want, both values are calculated and floating point comparison has it's problems.

itchy8me 05-03-2009 12:36 PM

hey,

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


judge312 05-03-2009 12:38 PM

hey

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);
}

dmail 05-03-2009 12:41 PM

Quote:

Originally Posted by itchy8me (Post 3528602)
hey,
is unequal
2.465820
value1 : 249
value2 : 256

value : 505
voltage : 2.465820
previous_voltage : 0.000000

So the file output is being redirected to the stdout?

judge312 05-03-2009 12:46 PM

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 :(

itchy8me 05-03-2009 12:49 PM

Quote:

Originally Posted by judge312 (Post 3528605)
hey

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.

itchy8me 05-03-2009 12:50 PM

Quote:

Originally Posted by dmail (Post 3528607)
So the file output is being redirected to the stdout?


thanks for the tips, gonna clean the code up.

no, i'm not redirecting, i'm prtinting to the screen and the file, what i print on the screen has more information than what imn printing to the file.

itchy8me 05-03-2009 12:53 PM

Quote:

Originally Posted by judge312 (Post 3528613)
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,..

dmail 05-03-2009 12:55 PM

Quote:

Originally Posted by itchy8me (Post 3528621)
no, i'm not redirecting

I did not say you were redirecting it, just that it is getting redirected.
Quote:

, i'm prtinting to the screen and the file, what i print on the screen has more information than what imn printing to the file.
You are not printing to screen the value there.
Quote:

printf ("\nis unequal\n");
fprintf(fp, "%f\n", voltage);

judge312 05-03-2009 12:57 PM

ok
then a simple solution
use fflush(fp) ; after fprintf

itchy8me 05-03-2009 01:10 PM

Quote:

Originally Posted by judge312 (Post 3528631)
ok
then a simple solution
use fflush(fp) ; after fprintf

thanks judge, the file is not empty anymore.


All times are GMT -5. The time now is 11:27 PM.