4 Replies - 1150 Views - Last Post: 01 November 2010 - 09:45 PM Rate Topic: -----

#1 need_helpp   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Reading binary file in C

Posted 01 November 2010 - 08:42 PM

Hi,

I'm trying to read a binary file of a matrix. And print out the text file version of it. But I can't get it to read the binary file.



Output should be something like this

2.00 5.45 6.22 9.10
5.66 2.44 10.55 6.66
9.40 1.22 11.42 5.03

Can someone give me a hint on reading a binary file?
Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Reading binary file in C

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading binary file in C

Posted 01 November 2010 - 08:45 PM

Please post your code. Hard to offer advice sight unseen.
Was This Post Helpful? 0
  • +
  • -

#3 need_helpp   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: Reading binary file in C

Posted 01 November 2010 - 09:16 PM

.

This post has been edited by need_helpp: 02 November 2010 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading binary file in C

Posted 01 November 2010 - 09:28 PM

Read the documentation for fscanf()
Was This Post Helpful? 0
  • +
  • -

#5 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Reading binary file in C

Posted 01 November 2010 - 09:45 PM

Good code layout makes it far easier to write correct code.
A good cook has a clean and well organised kitchen. You need to have a clean and well organised code layout if you are going to see easily what you have written and where you have made a mistake. You will also find that once you are no longer a beginner you may find people refuse to help you if your code is so badly laid out that they have to struggle to read it.

So choose one of the common indentation styles and use it consistently.
http://en.wikipedia....ki/Indent_style

Here is your original code laid out in something approaching a readable manner.
Please take the time to bring your code up this standard of layout before sharing it with strangers.
It is not only likely to get you more and better help it will help you.

#include <stdio.h> 
#include <string.h>  
#include <unistd.h>
#include <fcntl.h>


int main(int argc, char * argv[])
{   
    if (argc!=2)                                              
    { 
        printf("error");
        exit(1); 
    }
    
    int r, c, rows, cols;
    
    FILE *file = fopen(argv[1], "r+b");  //argv[1] is the filename
    if(file)
    {
        if ( fscanf(file, "%d rows  ", &rows) == 1 &&  fscanf(file, ",%d cols; ", &cols) == 1 )
        {
            for ( r = 0; r < rows; ++r )
            {   
                for ( c = 0; c < cols; ++c )
                {          
                    double value;
                    if ( fscanf(file, "%lf", &value) != 1 )
                    {           
                        break;
                    }
                    printf("%4f ", value);
                }
                putchar('\n');
            }
        }
    
        close(f);
        }
        else    
        {     
        perror(argv[1]);     
        }
    }
    else
    {
        printf("%s\n", "ERROR: File open failed!!")
    }

    return 0;
}



Was This Post Helpful? 1
  • +
  • -

Page 1 of 1