Like Tree4Likes
  • 1 Post By Salem
  • 1 Post By anduril462
  • 2 Post By sparkomemphis

file operation for floating point data for window OS

This is a discussion on file operation for floating point data for window OS within the C Programming forums, part of the General Programming Boards category; The following program is not giving the desired output. No warning is being shown. i am using an ide named ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    11

    Question file operation for floating point data for window OS

    The following program is not giving the desired output. No warning is being shown. i am using an ide named "dev c++" . My main concern is to perform this operation on floating point no.s . But i am presently struck at integer itself. So please help me out
    1)To get an output out of this
    2)Suggest something to workout this on float no.s too..

    thank you...
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    
    {
    
    FILE *fp,*f2;
    
    int i,n,x[100],num;
    
    int add,mul,y[10];
    
    int h[10]={5,2,3,6,1};
    
    printf("contents of DATA file");
    
    fp=fopen("DATA.c","w");
    
    
    /*writing integers as an array into "data" file*/
    
    for(i=0;i<100;i++)
    
    {
    
    n=i;
    
    x[n]=2*i;
    
    putw(x[n],fp);
    
    }
    
    
    /*printing the written data*/
    
    while ((num=getw(fp))!=EOF)
    
    printf("%d",num);
    
    fclose(fp
    
    );
    
    fp=fopen("DATA.c","r");
    
    f2=fopen("result.c","w");
    
    
    /*using the file "data" for further processing*/
    
    while((num==getw(fp))!=EOF)
    
    {
    
    
    /*logic for convolution*/
    
    for(n=0;n<=104;n++)
    
    {
    
    add=0;
    
    for(i=0;i<=n;i++)
    
    {
    
    mul=num*h[n-i];// hope 'num' holds the values of "data" file
    
    add=mul+add;
    
    }
    
    y[n]=add;
    
    putw (y[n],f2);
    
    }
    
    }
    
    fclose(fp);
    
    fclose(f2);
    
    
    /*printing the contents of file "result"*/
    f2=fopen("result.c","r");
    while((num=getw(f2))!=EOF)
    printf("%d",num);
    fclose(f2);
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    If you get no warnings compiling that then you should turn the warning level up. It should at least be complaining that :
    Code:
    main()
    should be:
    Code:
    int main(void)
    Secondly, if you want to make it use floats, then make it use floats. Try this and post your attempt, instead of expecting someone else to do it for you (which wont happen).

    Last (but not least), study up on how to do indentation and apply that to your code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    11
    actually i was taliking about the putw() and getw() functions which doesnt work for float.... so any other function to read and write float??? and does the read() and write() work on window OS, coz i heard that they are used in unix only....

    sorry that the indentation came up like this but its not same in my original code... may b somthng went wrong with my copy-paste

    and coming to the point... to be specific the above code prints the 1st printf() statement and then prints few rows of "0000000"(zeros).

    guidance needed....

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    Well I'm going to guess you're fresh from being a TurboC user, where integers are 16-bit, and this putw/getw stuff happily worked by coincidence.

    But now your int's are 32-bit, so putw/getw are losing information.

    > and does the read() and write() work on window OS, coz i heard that they are used in unix only....
    Anything you find in stdio.h is portable across all operating systems that support stdio.

    > sorry that the indentation came up like this but its not same in my original code... may b somthng went wrong with my copy-paste
    You could try "go advanced" and preview your post to make sure everything is OK, and try to fix your process before pressing submit.

    > for(n=0;n<=104;n++)
    Given that y and h have only 10 elements, you have some big overrun issues to fix first.
    saya407 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,496
    Yes, read and write a *nix specific. Look into fread and fwrite.
    saya407 likes this.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    11
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	FILE *fp,*f2;
    	int i,n,x[100],num;
    	int add,mul,y[110];
    	int h[10]={5,2,3,6,1};
    	printf("contents of DATA file");
    	fp=fopen("DATA.c","w");
    	for(i=0;i<100;i++)
    	{
    		n=i;
    		x[n]=2*i;
    		fwrite(x[n],fp);
    	}
    	while ((num=fread(fp))!=EOF)
    		printf("%d",num);
    	fclose(fp);
    	fp=fopen("DATA.c","r");
    	f2=fopen("result.c","w");
    	while((num==fread(fp))!=EOF)
    	{
    		for(n=0;n<=104;n++)
    		{
    			add=0;
    			for(i=0;i<=n;i++)
    			{
    				mul=num*h[n-i];
    				add=mul+add;
    			}
    			y[n]=add;
    			fwrite (y[n],f2);
    		}
    	}
    	fclose(fp);
    	fclose(f2);
    	f2=fopen("result.c","r");
    	while((num=fread(f2))!=EOF)
    	printf("%d",num);
    	fclose(f2);
    }
    yeah its true... infact i stopped working in turboc just today and switched over to new ide.
    the code above is what i modified following the suggestions.
    and this is what i get after compiling.
    I am sorry but couldn't really understand what they meant.

    In function `main':
    15 [Warning] passing arg 1 of `fwrite' makes pointer from integer without a cast
    15 [Warning] passing arg 2 of `fwrite' makes integer from pointer without a cast
    15 too few arguments to function `fwrite'
    17 too few arguments to function `fread'
    22 too few arguments to function `fread'
    33 [Warning] passing arg 1 of `fwrite' makes pointer from integer without a cast
    33 [Warning] passing arg 2 of `fwrite' makes integer from pointer without a cast
    33 too few arguments to function `fwrite'
    39 too few arguments to function `fread'

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    Did you even read the manual page before trying to use those functions?
    fread(3): binary stream input/output - Linux man page
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    67
    Quote Originally Posted by saya407 View Post
    Code:
    #
    	fp=fopen("DATA.c","w");
    	
    	fp=fopen("DATA.c","r");
    	f2=fopen("result.c","w");
    	
    	f2=fopen("result.c","r");
    	w
    	fclose(f2);
    }
    Are you sure you want to use result.c and DATA.c ? one can end up overwriting source files using the .c "extension"
    Salem and iMalc like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a floating point from a .txt file into an array
    By astrogal in forum C Programming
    Replies: 13
    Last Post: 09-28-2011, 02:56 AM
  2. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM
  3. Reading 64 bit IEEE floating point from a file
    By rcobb in forum C Programming
    Replies: 1
    Last Post: 04-23-2003, 07:28 PM
  4. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 10:34 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21