LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2009, 11:04 AM   #1
itchy8me
Member
 
Registered: Feb 2007
Location: Rotterdam
Distribution: debian 5
Posts: 33

Rep: Reputation: 15
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
 
Old 05-03-2009, 12:04 PM   #2
judge312
LQ Newbie
 
Registered: Dec 2008
Distribution: fedora,rhel
Posts: 18

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

I think its null != null condition, which is always true.
 
Old 05-03-2009, 12:12 PM   #3
itchy8me
Member
 
Registered: Feb 2007
Location: Rotterdam
Distribution: debian 5
Posts: 33

Original Poster
Rep: Reputation: 15
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.
 
Old 05-03-2009, 12:17 PM   #4
judge312
LQ Newbie
 
Registered: Dec 2008
Distribution: fedora,rhel
Posts: 18

Rep: Reputation: 1
but what is prints ?
is there any value in voltage variable ?
Can you share a sample value in voltage variable ?
 
Old 05-03-2009, 12:33 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
 
Old 05-03-2009, 12:36 PM   #6
itchy8me
Member
 
Registered: Feb 2007
Location: Rotterdam
Distribution: debian 5
Posts: 33

Original Poster
Rep: Reputation: 15
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
--------------------------------------------------------------------------------------------------------
 
Old 05-03-2009, 12:38 PM   #7
judge312
LQ Newbie
 
Registered: Dec 2008
Distribution: fedora,rhel
Posts: 18

Rep: Reputation: 1
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);
}
 
Old 05-03-2009, 12:41 PM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by itchy8me View Post
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?
 
Old 05-03-2009, 12:46 PM   #9
judge312
LQ Newbie
 
Registered: Dec 2008
Distribution: fedora,rhel
Posts: 18

Rep: Reputation: 1
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
 
Old 05-03-2009, 12:49 PM   #10
itchy8me
Member
 
Registered: Feb 2007
Location: Rotterdam
Distribution: debian 5
Posts: 33

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by judge312 View Post
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.

Last edited by itchy8me; 05-03-2009 at 12:51 PM.
 
Old 05-03-2009, 12:50 PM   #11
itchy8me
Member
 
Registered: Feb 2007
Location: Rotterdam
Distribution: debian 5
Posts: 33

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by dmail View Post
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.
 
Old 05-03-2009, 12:53 PM   #12
itchy8me
Member
 
Registered: Feb 2007
Location: Rotterdam
Distribution: debian 5
Posts: 33

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by judge312 View Post
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,..
 
Old 05-03-2009, 12:55 PM   #13
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by itchy8me View Post
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);
 
Old 05-03-2009, 12:57 PM   #14
judge312
LQ Newbie
 
Registered: Dec 2008
Distribution: fedora,rhel
Posts: 18

Rep: Reputation: 1
ok
then a simple solution
use fflush(fp) ; after fprintf
 
Old 05-03-2009, 01:10 PM   #15
itchy8me
Member
 
Registered: Feb 2007
Location: Rotterdam
Distribution: debian 5
Posts: 33

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by judge312 View Post
ok
then a simple solution
use fflush(fp) ; after fprintf
thanks judge, the file is not empty anymore.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ps -ef strange behaviour ?? lionking_x Linux - Newbie 1 11-28-2007 05:22 PM
strange behaviour marsques Slackware 11 02-15-2006 06:05 PM
Strange Behaviour! joshuarowley LQ Suggestions & Feedback 1 12-08-2005 03:36 PM
Strange behaviour Anmol SUSE / openSUSE 2 10-27-2005 11:05 PM
Strange Behaviour mikeyt_3333 Linux - General 4 08-06-2001 03:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration