Thread: save a spicific column from a csv file into an array

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    2

    save a spicific column from a csv file into an array

    if i have a file.txt with comma separated value and i need to access the 10th and 15th columns ofthis file and save each one in an array
    i tried this code (which was attached in the site) but it didn't work it always print the 3rd column of the file and i still don't know why!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define FIRST_LINE   0
    #define LAST_LINE 10
    
    int main() {
    
     char buf[512];
      int line_count = 0;
      FILE *file = fopen("min_chest.txt", "r");
    
    
      while (fgets(buf, sizeof buf, file) != NULL) {
        double value;
    
        if (sscanf(buf, "%*lf,%*lf,%lf,*%lf,%lf", &value) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
    
        printf("%f\n", value);
        if (line_count++ >= 10)
          break;
      }
    
      fclose(file);
    
    
      return 0;
    }
    it was refreenced here

    Reading values from a csv file in c into an array

    why it always prints the 3rd column ?
    and if i need to know the size of the file automatically how to know that ??
    any advice of how to get it well ?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Code:
    src/test.cpp:17:53: error: format ‘%lf’ expects a matching ‘double*’ argument [-Werror=format=]
    src/test.cpp:17:53: error: format ‘%lf’ expects a matching ‘double*’ argument [-Werror=format=]
    You are trying to read 2 double values of 5, but provide only 1 variable. This is undefined behavior. So your are lucky your program works at all. Fix it before asking why it does something specific.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    2
    am sorry i don't understand what u mean ??
    "You are trying to read 2 double values of 5, but provide only 1 variable." ??

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    Code:
    if (sscanf(buf, "%*lf,%*lf,%lf,*%lf,%lf", &value) != 1)
    What is this line supposed to do ?

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Quote Originally Posted by OldGuy2 View Post
    What is this line supposed to do ?
    Quoted from scanf - C++ Reference
    An optional starting asterisk indicates that the data is to be read from the stream but ignored (i.e. it is not stored in the location pointed by an argument).
    Looking at the OP's code again though, I think they made a typo there. They meant for all reads except one to have an asterisk.
    Last edited by GReaper; 09-07-2017 at 10:11 AM.
    Devoted my life to programming...

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by Tasneem Gamal View Post
    am sorry i don't understand what u mean ??
    "You are trying to read 2 double values of 5, but provide only 1 variable." ??
    %*lf will skip the number in the input stream

    %lf will read a number into double variable (passed by pointer)

    You have 3 %*lf and 2 %lf - it means you expect to have 5 double values and read 2 of them.

    To read 2 values you need 2 variables
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-04-2014, 02:23 PM
  2. Replies: 5
    Last Post: 03-04-2014, 10:09 AM
  3. Subtracting one column in a 2d array from another?
    By zaihed13 in forum C Programming
    Replies: 6
    Last Post: 10-19-2013, 09:29 AM
  4. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  5. read spicific parts from a file
    By devour89 in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2002, 10:24 AM

Tags for this Thread