Thread: extracting and printing data from a .dat file

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    9

    extracting and printing data from a .dat file

    I've been given an assignment to calculate the determinant of a 3x3 matrix which I must read from a .dat file. The contents of the .dat file should look something like this:

    1 3 2
    2 3 3
    1 2 2

    Basically, I can't work out how to read these values from the .dat file and print them in my program. If anyone can offer any help I'll be very grateful, thanks in advance

    EDIT: what I have so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char* argv[])
    {
    	FILE        *input;
    	float       v1[3], v2[3], v3[3];
    	const char  inp_fn[]="matrix.dat";
    	
    	input = fopen(inp_fn, "r");
    Last edited by z0diark; 11-16-2009 at 05:00 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Two basic approaches:
    1. .dat is a text file and you read strings from it, then use scanf() or atoi() on the string
    2. .dat is a binary file and you read datatypes (such as int) directly


    Probably you want to do the former, as this makes creating and checking the .dat file easier. So you read a line of text with fgets(), then you pull numbers from it with sscanf(), or else you use fscanf() to do both.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char* argv[])
    {
    	FILE        *input;
    	float       v1[3], v2[3], v3[3];
    	const char  inp_fn[]="matrix.dat";
    	
    	input = fopen(inp_fn, "r");
    That's what I have so far, I now want to know how to actually read and print the data within the .dat file, how would I do that?

    P.S. I'm very inexperienced at C Programming so sorry if it takes a while for me to understand.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by z0diark View Post
    P.S. I'm very inexperienced at C Programming so sorry if it takes a while for me to understand.
    That's fine, but you are going to have to read the documentation for the commands I recommended -- not because I am a cantankerous type, but because if you want to program, you gotta learn "documentation literacy".

    Anyway, so if you add this to what you just posted, you will print out each line in the file:
    Code:
    char buffer[256] /* put this with your other variables */
         while (fgets(buffer,256,input)) {       /* until fgets() returns NULL at EOF */
               printf("%s",buffer);  
          }
          fclose(input);
    Now, to get the individual numbers, you will want to use sscanf() on "buffer" inside the while loop. You can also try using fscanf() to do that, replacing fgets().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed