Beginner:Needs help with fread().. 
Author Message
 Beginner:Needs help with fread()..


Quote:
>Hi!
>I'm a beginner in C programming and
>caught some problems using fread().
>I'm trying to copy every item in an file
>into an array. The file consists of
>3 rows and N rows, like this:
>130695 1234 342.3
>130695 1245 443.5

I assume you mean 3 columns and N rows. The basic question is: Is
your file a binary file or a formatted text file?

Quote:
>I tried to use fread() like this:
>    for(y=0;y<=N;y++)
>     for(x=0;x<=2;x++)
>      {
>      fread((char*)&ptr,sizeof(ptr),1,in);

What is ptr, why are you taking it's address, and why are you casting
it to a pointer to char. If the file contains binary data (e.g.
binary doubles), and if ptr has the right size  to read one data
item at a time, your method should succeed.

Quote:
>      array[y][x]=(float)ptr;

What is the cast to float supposed to do? If ptr has a type for which
the cast to float makes some sense, it should have worked without the
cast. OTOH, if ptr is, e.g., defined as

  char ptr[sizeof(float)];

and if your file contains binary floats, this should read

       array[y][x] = *((float *)ptr);

Quote:
>      }
>but this doesn't work because the values
>in the array becomes totally different
>than the values in the file.
>Whats's wrong and how could I get the
>file(s) read into arrays most easy??

Depends a lot on what your file contains. If it is formatted text,
i.e. something similar to what you posted as a description of
your files contents, you should reasearch formatted input, via
fscanf() in the easiest case. For machine written formatted files,
fscanf() is acceptable, if you know the program that has written
the file and if you are prepared to rely on it.

BTW, trim your .signature

Kurt

--
| Kurt Watzka                             Phone : +49-89-2180-6254



Mon, 04 Jan 1999 03:00:00 GMT  
 Beginner:Needs help with fread()..


Quote:
> Hi!
> I'm a beginner in C programming and
> caught some problems using fread().
> I'm trying to copy every item in an file
> into an array. The file consists of
> 3 rows and N rows, like this:
> 130695 1234 342.3
> 130695 1245 443.5

> I tried to use fread() like this:
>     for(y=0;y<=N;y++)
>      for(x=0;x<=2;x++)
>       {
>       fread((char*)&ptr,sizeof(ptr),1,in);
>       array[y][x]=(float)ptr;
>       }

You cant convert strings to floating point variables like that I'm afraid! I
think you'll find that the following code should do what you want (although I
havent actually tested it).

for(y=0;y<=N;y++)
  scanf("%f %f %f", array[y][0], array[y][1], array[y][2]);



Mon, 04 Jan 1999 03:00:00 GMT  
 Beginner:Needs help with fread()..

Hi!
I'm a beginner in C programming and
caught some problems using fread().
I'm trying to copy every item in an file
into an array. The file consists of
3 rows and N rows, like this:
130695 1234 342.3
130695 1245 443.5

I tried to use fread() like this:
    for(y=0;y<=N;y++)
     for(x=0;x<=2;x++)
      {
      fread((char*)&ptr,sizeof(ptr),1,in);
      array[y][x]=(float)ptr;
      }

but this doesn't work because the values
in the array becomes totally different
than the values in the file.
Whats's wrong and how could I get the
file(s) read into arrays most easy??
Any suggestions would help!
thanks,
-mats-

                                 \\|//
                                 (0 0)
     -----------------------oooo0-(-)-0oooo--------------------------

     *  Arkadiankatu 19 C 41    http://www.helsinki.fi/~mgransko    *
     *  FIN-00100 Helsinki      phone +358-0-497980                 *                   *
     *                                                              *
     ~~~~~~~~~~~~~~~~~~~~~~~oooo0~~~0oooo~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Mon, 04 Jan 1999 03:00:00 GMT  
 Beginner:Needs help with fread()..

Quote:

>I'm trying to copy every item in an file
>into an array. The file consists of
>3 [columns] and N rows, like this:
>130695 1234 342.3
>130695 1245 443.5

>I tried to use fread() like this:
>    for(y=0;y<=N;y++)
>     for(x=0;x<=2;x++)
>      {
>      fread((char*)&ptr,sizeof(ptr),1,in);
>      array[y][x]=(float)ptr;
>      }

  I assume that your input file is human-readable.  Then you need to use a
function that interprets the characters "130695" as a floating-point number
and _converts_ it into your machine's representation for floating-point.

  As an example, my machine stores floating point numbers in the space of
four 8-bit chars, using one bit for the sign, eight for the exponent, and
twenty-three bits for the mantissa.  If I print these four characters
directly, they look like garbage.

  fscanf(in, "%f", &array[y][x]) will perform this conversion (if array[y][x]
is of type float).  "%f" asks fscanf to look for human-readable floating-
point numbers separated by whitespace, which is just what you have. (Newlines
count as whitespace.)  Absorb what you can from section 12 of the FAQ as you
begin using fscanf() in other situations.

  fread() simply reads characters from a file and places them directly in
memory.

  A few other things which might be helpful:
The common idiom for stepping through an array is for (y=0; y<N; y++).
Other programmers will recognize this at once; they will have to think a
moment before recognizing that your outer loop tries to read N+1 rows.
(Is this really what you want?)  Also, people generally use the names i, j,
k, etc. for array indices, where you have x and y.

-----
keith



Mon, 04 Jan 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Need fread() help

2. help needed fread'ing data into a buffer using Turbo C++ and huge model

3. Need help with fopen/fread prob in W98 - PLEASE

4. need help with fread BIG probs

5. Need beginner help in c

6. Help needed by beginner!

7. Beginner needs help

8. a.) need help for beginner

9. Beginner need help to understand this.

10. I am a beginner, Need help on variables

11. Beginner needs help to print binary number

12. Beginner need help!

 

 
Powered by phpBB® Forum Software