Hi,
i am working on Numerical Computation with C.... i have a data file which has two columns separated by a tab in between... it looks like

43.0000000000000    43.0000000000000
42.0000000000023    43.0000000000006
41.0000000000000    43.0000000000056
40.0000000000000    43.1999996948242

i have written this code which seems to work out smoothly...

#include <stdio.h>

main() {
  FILE *file;
  float nx[100],ny[100]; 
  /* make sure it is large enough to hold all the data! */
  int i,j;

  file = fopen("INNERXY.txt", "r");

  if(file==NULL) {
    printf("Error: can't open file.\n");
    return 1;
  }
  else {
    printf("File opened successfully.\n");

    i = 0 ;    

    while(!feof(file)) { 
      /* loop through and store the numbers into the array */
      fscanf(file, "%f\t%f",&nx[i],&ny[i]);
      i++;
    }

    printf("Number of numbers read: %d\n\n", i);
    printf("The numbers are:\n");

    for(j=0 ; j<i ; j++) { /* now print them out 1 by 1 */
      printf("%0.13f\t%2.13f\n", nx[j],ny[j]);
    }

    fclose(file);
    return 0;
  }
}

i get an output which has been rounded off (the decimals).. and the output turns out to be

File opened successfully.
Number of numbers read: 4

The numbers are:
43.0000000000000    43.0000000000000
42.0000000000000    43.0000000000000
41.0000000000000    43.0000000000000
40.0000000000000    43.2000007629395

Note that the last number... is being rounded off...

Guys please help me out!!!
i dont want the numbers to be rounded off to the decimals.....

Thanks in advance....
--
SandyJain

Recommended Answers

All 12 Replies

Does changing the type from float to double do the trick ? My guess is that the number is being rounded because it is reaching its precision value.

Does changing the type from float to double do the trick ? My guess is that the number is being rounded because it is reaching its precision value.

A double gives me nothing more than junk values of -9178987657894600000000000000000000000000000000.000000

and so on...!!

That's coz you're using %f for scanning a double. Use %lf.

Thanks buddy.... i figured that out right now...!!

> while(!feof(file))
This doesn't do what you think it does. feof() only becomes true when some file reading function (say fscanf has already FAILED). But since you don't check that, you potentially end up with some junk on the last record.

Consider this an alternative

while( i < 100 && fscanf(file, "%f\t%f",&nx[i],&ny[i]) == 2 ) {
      i++;
    }

That's coz you're using %f for scanning a double. Use %lf.

Hi,
rather old post but correct one for disscussions.
i once used the %lf in one thread but it was said that there is no such specifier.


http://www.daniweb.com/forums/thread241546.html

can some one explain whcih is the corect fomat specifier for reading double values using scanf statement.

In that thread you specified, (I believe) Narue was saying that it was not a legal specifier for use with printf. That is the correct format specifier for a double in scanf. I thought she gave an explanation for that asymmetry at one point but I may be thinking of something else.

Narue is right in the other thread you link: she's talking about PRINTF

this thread is also right. it's talking about SCANF. as stated earlier -- much earlier, like 3 years ago -- use "%la" to scanf a double.

"printf" and "scanf" are different functions, they do not necessarily use the same format specifiers.

the fact that many of the format specifiers happen to be the same, is merely a coincidence that was apparently encouraged to trick unsuspecting new programmers.

.

I doubt it was merely a coincidence. It was in fact well planned, with whatever differences designed for input vs. output.

I doubt it was merely a coincidence. It was in fact well planned, with whatever differences designed for input vs. output.

okay, fine, it was a well planned effort to trick new programmers.

I'll give you that one :)

One of the major differences is that floats get promoted to doubles when used as parameters in functions that have no declaration or in the variable parameter list of varidac functions (like printf). So printf never sees a float in the parameter list and does not need to differetiate between floats and doubles.

On the other hand scanf deals with pointers to data types and a pointer to float and a pointer to double are to very different beasties so scanf must be informed which one to expect.

A similar thing happens with short and int. shorts are automatically promoted to int in the same situation so printf does not need to know if you are passing a short or int to it %D does for both. I know that %hd exists for printf but if you read around you will find some places that correctly say "causes printf to expect a int sized integer argument which was promoted from a short".

I am not quite sure why printf would need this information but at least then the same format string can be used for printf and scanf. However again calling scanf it is imperative that %hd is used for short and %d for int because again short * and int * are quite different.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.