Help with fopen!!

This is a discussion on Help with fopen!! within the C Programming forums, part of the General Programming Boards category; I tried to read a csv file into my array using this code. But it seems to read in only ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    Help with fopen!!

    I tried to read a csv file into my array using this code. But it seems to read in only the first value. When I print it I get
    Code:
    5.0000   0.0000
    0.0000   0.0000
    0.0000   0.0000
    0.0000   0.0000
    where 5 is the first value I have in the file. Please someone help me out.
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    int main(void){
            int rows =4;
            int cols=2;
            int i,j;
            float a[rows][cols];
            FILE *infile;
            infile=fopen("C:\\Documents and Settings\\USER\\Desktop\\Datatest.csv", "r");
            for(i=0; i<rows; i++)
            {
                     for(j=0; j<cols; j++)
                     {
                      fscanf(infile,"%f", &a[i][j]);
                     }
            }
              if ((infile=fopen("C:\\Documents and Settings\\USER\\Desktop\\Datatest.csv", "r"))== NULL)
              {
              printf("Cannot open file. \n");
              exit(1);
              }
            
              for(i=0; i<rows; i++){
              for(j=0; j<cols; j++)
              printf("%f\t", a[i][j]);
              printf("\n");}
              getchar();
              fclose(infile);
     }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,654
    It won't skip the commas for you, if your CSV file is really a comma separated value file.

    Say
    Code:
    if ( fscanf(infile,"&#37;f,", &a[i][j]) != 1 ) {
      // some diagnostic
    }
    Remember, apart from a space (which means any amount of any of the white-space characters, each character stands for itself.

    > if ((infile=fopen("C:\\Documents and Settings\\USER\\Desktop\\Datatest.csv", "r"))== NULL)
    You're opening it again.
    It should be
    if ( infile == NULL )
    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    put the test for infile before the loop. You use infile in the loop, so you may be using an invalid FILE pointer.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Please someone should help me out on how to amend the code, so I can read in my data please.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sailors View Post
    Please someone should help me out on how to amend the code, so I can read in my data please.
    Can please post your current code (assuming you have actually tried to change something)?

    --
    Mats

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Quote Originally Posted by matsp View Post
    Can please post your current code (assuming you have actually tried to change something)?

    --
    Mats
    This is what I've written so far, it works by printing out the values but I wish to store it in as an array of doubles like I was trying to do in my earlier code, but don't know how to incorporate it. The array I wish to store it is declared as dooble array[6][10]. Any suggestion?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( ) {
        char buff[BUFSIZ];
        char sep[] = ",\n"; 
        FILE *fp = fopen( "C:\\Documents and Settings\\USER\\Desktop\\Book_Sheet1.csv", "r" );
        while ( fgets( buff,BUFSIZ,fp) != NULL ) {
            char *p;
            for ( p = strtok(buff,sep) ; p!=NULL ; p = strtok(NULL,sep) ) {
                printf( "%s\t", p );
                
            }
            printf("\n");
            getchar();
        }
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Ok I've solved it

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Sailors View Post
    Ok I've solved it
    No you haven't, for one you don't close the file nor do you check if you actually managed to open it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  2. fopen help.
    By chriscolden in forum C Programming
    Replies: 17
    Last Post: 01-13-2006, 05:27 AM
  3. problem with fopen command
    By emon in forum C Programming
    Replies: 2
    Last Post: 03-11-2004, 11:11 PM
  4. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM
  5. fopen vs open
    By rotis23 in forum Linux Programming
    Replies: 5
    Last Post: 12-10-2002, 01:30 PM

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