Thread: [Help] Reading in data from .csv file

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    11

    [Help] Reading in data from .csv file

    I don't know how to read a csv file into an array since my c experiance is not great.

    This is what I have so far
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main() {
      char buffer[1024];
      int line_count = 0;
      FILE *file = fopen("1000.csv", "r");
     
      if (!file) {
        fprintf(stderr, "ERROR! Could not open file\n");
        exit(EXIT_FAILURE);
      }
     
      while (fgets(buffer, sizeof buffer, file) != NULL)
      {
        if (++line_count >= 1)
        {
          break;
          }
      }
     
      while (fgets(buffer, sizeof buffer, file) != NULL) {
        double value;
     
        // read in first colum
        if (sscanf(buffer, "%lf,%*lf,%*lf", &value) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
     
        printf("%f\n", value);
        if (++line_count >= 1000)
          break;
      }
     
      fclose(file);
      return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You'll need to provide a small sample of your input file as well and you'll need to explain what you're trying to accomplish.

    Jim

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    11
    [Help] Reading in data from .csv file-screenshot_1211-png
    this is the kind of data I am trying to read into an array.

    Essentially, the end goal is to take all the information from the csv file and place it into 8 arrays (one for each column)

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    gcc gives the warning: "use of assignment suppression and length modifier together in gnu_scanf format". That might seem strange until you realize that the scanf family of functions don't do any range checking. I.e., if you were to read the following sequence of chars with "%hd" (short int), "%d" (int), and "%ld" (long int) you would get different results (assuming 2, 4, and 8 bytes, respectively):
    Code:
    short hd;   // assuming 2 bytes
    int d;      // assuming 4 bytes
    long ld;    // assuming 8 bytes
    sscanf("99111222333 99111222333 99111222333",
           "%hd %d %ld", &hd, &d, &ld);
    printf("%d %d %ld\n", hd, d, ld);
    
    // Output: 15421 326974525 99111222333
    If we wanted to skip either the first or last field, we would just use "%*d", not "%*hd" or "%*ld", since all we need from the spec is the format of the characters to skip and we don't need to know the number of bytes the result needs to be stuffed into.

    So you need to change your "assignment suppressing" formats to "%*f" instead of "%*lf". But since you're reading a whole line with fgets and then using sscanf, as long as you just want the first value in the line you could use a single "%lf" and leave out the rest.

    BTW, "buffer" is almost as non-descript a name as "temp". In this case "line" would be more accurate.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    this is the kind of data I am trying to read into an array.
    That appears to be a spreadsheet not a CSV file. While showing the data in this format helps it doesn't really show the exact format of your input file.

    Essentially, the end goal is to take all the information from the csv file and place it into 8 arrays (one for each column)
    Okay, where are your arrays? But why not consider an array of a struct that contains the information for one complete record?

    Look at this snippet:
    Code:
        if (sscanf(buffer, "%lf,%*lf,%*lf", &value) != 1) {
    If you want to read every field why are you reading one value and throwing away two others?

    Next:
    Code:
      while (fgets(buffer, sizeof buffer, file) != NULL)
      {
        if (++line_count >= 1)
        {
          break;
          }
      }
    What is the purpose of the loop? You read at most one line of text then you "break" out of the loop?

    Lastly you need to ask specific questions about the code you provided.

    Jim

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    That appears to be a spreadsheet not a CSV file. While showing the data in this format helps it doesn't really show the exact format of your input file.
    [Help] Reading in data from .csv file-screenshot_1212-png

    Windows is telling me it a .csv file. It just opens in excel.

    Quote Originally Posted by jimblumberg View Post
    Okay, where are your arrays? But why not consider an array of a struct that contains the information for one complete record?

    Look at this snippet:
    Code:
        if (sscanf(buffer, "%lf,%*lf,%*lf", &value) != 1) {
    If you want to read every field why are you reading one value and throwing away two others?
    I was testing at the time to see which values I was getting. The updated code below shows what I would like it to do: for every next column, throw away the previous one (as that should now be in the array) and move onto the next.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main() {
      char line[1024];
      int line_count = 0;
      FILE *file = fopen("1000.csv", "r");
      
      // Declare Arrays
      int LineNum[1000] = {0}; //ID for the line number
      int type[1000] = {0}; //Type code to idtentify the device (0x20)
      int version[1000] = {0}; //software version
      char counter[1000][2]; //A rolling 8-bit, ever increasing number. Used to show how many messages are being missed by the receiver
      int via[1000] = {0}; //Which receiver picked up this device’s transmission
      char address[1000][8]; //The address of the transmitter
      char status[1000][1]; //The status code of the device
      char sensorData[1000][128]; //10-bytes of sensor data in hex (each sensor being 1 byte and 10 sensors) 
     
      if (!file) {
        fprintf(stderr, "ERROR! Could not open file\n");
        exit(EXIT_FAILURE);
      }
     
      while (fgets(line, sizeof line, file) != NULL) {
        int lCounter = 0;
     
        // read in first colum
        if (sscanf(line, "%d", &type[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in second colum
        if (sscanf(line, "%*d,%d", &version[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in third colum
        if (sscanf(line, "%*d,%*d,%d", &counter[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in fourth colum
        if (sscanf(line, "%*d,%*d,%*d,%s", &via[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in fifth colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%d", &address[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in sixth colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%*d,%s", &status[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in seventh colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%*d,%*s,%s", &sensorData[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
     
        printf("%d", type[lCounter]);
        printf("%d", version[lCounter]);
        printf("%d", counter[lCounter]);
        printf("%s", via[lCounter]);
        printf("%d", address[lCounter]);
        printf("%s", status[lCounter]);
        printf("%s", sensorData[lCounter]);
        if (++line_count >= 1000)
          break;
      }
     
      fclose(file);
      return 0;
    }


    Quote Originally Posted by jimblumberg View Post
    Next:
    Code:
      while (fgets(buffer, sizeof buffer, file) != NULL)
      {
        if (++line_count >= 1)
        {
          break;
          }
      }
    What is the purpose of the loop? You read at most one line of text then you "break" out of the loop?
    before I came to the forum I asked some of my piers for help. For some reason (using the old code) I can read in the first 3 columns and print them (if I read them individually) otherwise I get the sscanf failed error. I have removed it

    Quote Originally Posted by jimblumberg View Post
    Lastly you need to ask specific questions about the code you provided.
    How do I read a string containing letters and numbers (from the screenshot) I cannot use a float.

    I keep getting the sscanf failed error and I'm not sure why. How do I fix this?

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    That appears to be a spreadsheet not a CSV file. While showing the data in this format helps it doesn't really show the exact format of your input file.
    [Help] Reading in data from .csv file-screenshot_1212-png

    Windows tells me it is a .csv file. It just opens in excel.

    Quote Originally Posted by jimblumberg View Post
    Okay, where are your arrays? But why not consider an array of a struct that contains the information for one complete record?

    Look at this snippet:
    Code:
        if (sscanf(buffer, "%lf,%*lf,%*lf", &value) != 1) {
    If you want to read every field why are you reading one value and throwing away two others?
    I have updated the code below, the reason I was reading one value and throwing away the rest was for 2 reasons.
    1. I was testing
    2. Reading all three during the same runtime gives me an error

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main() {
      char line[1024];
      int line_count = 0;
      FILE *file = fopen("1000.csv", "r");
      
      // Declare Arrays
      int LineNum[1000] = {0}; //ID for the line number
      int type[1000] = {0}; //Type code to idtentify the device (0x20)
      int version[1000] = {0}; //software version
      char counter[1000][2]; //A rolling 8-bit, ever increasing number. Used to show how many messages are being missed by the receiver
      int via[1000] = {0}; //Which receiver picked up this device’s transmission
      char address[1000][8]; //The address of the transmitter
      char status[1000][1]; //The status code of the device
      char sensorData[1000][128]; //10-bytes of sensor data in hex (each sensor being 1 byte and 10 sensors) 
     
      if (!file) {
        fprintf(stderr, "ERROR! Could not open file\n");
        exit(EXIT_FAILURE);
      }
     
      while (fgets(line, sizeof line, file) != NULL) {
        int lCounter = 0;
     
        // read in first colum
        if (sscanf(line, "%d", &type[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in second colum
        if (sscanf(line, "%*d,%d", &version[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in third colum
        if (sscanf(line, "%*d,%*d,%d", &counter[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in fourth colum
        if (sscanf(line, "%*d,%*d,%*d,%s", &via[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in fifth colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%d", &address[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in sixth colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%*d,%s", &status[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in seventh colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%*d,%*s,%s", &sensorData[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
     
        printf("%d", type[lCounter]);
        printf("%d", version[lCounter]);
        printf("%d", counter[lCounter]);
        printf("%s", via[lCounter]);
        printf("%d", address[lCounter]);
        printf("%s", status[lCounter]);
        printf("%s", sensorData[lCounter]);
        if (++line_count >= 1000)
          break;
      }
     
      fclose(file);
      return 0;
    }
    Quote Originally Posted by jimblumberg View Post
    Next:
    Code:
      while (fgets(buffer, sizeof buffer, file) != NULL)
      {
        if (++line_count >= 1)
        {
          break;
          }
      }
    What is the purpose of the loop? You read at most one line of text then you "break" out of the loop?
    I was having a friend helping me with this and he added that. I don't know too well what It does apart from count the number of lines?
    I removed it.

    Quote Originally Posted by jimblumberg View Post
    Lastly you need to ask specific questions about the code you provided.
    My if statement keeps tripped and giving me the sscanf failed error how can I resolve this?

    How would I go about making an structure array for the arrays in my new code?

  8. #8
    Registered User
    Join Date
    May 2017
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    That appears to be a spreadsheet not a CSV file. While showing the data in this format helps it doesn't really show the exact format of your input file.
    [Help] Reading in data from .csv file-screenshot_1212-png

    Windows tells me it is a .csv file. It just opens in excel.

    Quote Originally Posted by jimblumberg View Post
    Okay, where are your arrays? But why not consider an array of a struct that contains the information for one complete record?

    Look at this snippet:
    Code:
        if (sscanf(buffer, "%lf,%*lf,%*lf", &value) != 1) {
    If you want to read every field why are you reading one value and throwing away two others?
    I have updated the code below, the reason I was reading one value and throwing away the rest was for 2 reasons.
    1. I was testing
    2. Reading all three during the same runtime gives me an error

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main() {
      char line[1024];
      int line_count = 0;
      FILE *file = fopen("1000.csv", "r");
      
      // Declare Arrays
      int LineNum[1000] = {0}; //ID for the line number
      int type[1000] = {0}; //Type code to idtentify the device (0x20)
      int version[1000] = {0}; //software version
       char counter[1000][2]; //A rolling 8-bit, ever increasing number. Used  to show how many messages are being missed by the receiver
      int via[1000] = {0}; //Which receiver picked up this device’s transmission
      char address[1000][8]; //The address of the transmitter
      char status[1000][1]; //The status code of the device
      char sensorData[1000][128]; //10-bytes of sensor data in hex (each sensor being 1 byte and 10 sensors) 
     
      if (!file) {
        fprintf(stderr, "ERROR! Could not open file\n");
        exit(EXIT_FAILURE);
      }
     
      while (fgets(line, sizeof line, file) != NULL) {
        int lCounter = 0;
     
        // read in first colum
        if (sscanf(line, "%d", &type[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in second colum
        if (sscanf(line, "%*d,%d", &version[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in third colum
        if (sscanf(line, "%*d,%*d,%d", &counter[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in fourth colum
        if (sscanf(line, "%*d,%*d,%*d,%s", &via[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in fifth colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%d", &address[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in sixth colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%*d,%s", &status[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in seventh colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%*d,%*s,%s", &sensorData[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
     
        printf("%d", type[lCounter]);
        printf("%d", version[lCounter]);
        printf("%d", counter[lCounter]);
        printf("%s", via[lCounter]);
        printf("%d", address[lCounter]);
        printf("%s", status[lCounter]);
        printf("%s", sensorData[lCounter]);
        if (++line_count >= 1000)
          break;
      }
     
      fclose(file);
      return 0;
    }
    Quote Originally Posted by jimblumberg View Post
    Next:
    Code:
      while (fgets(buffer, sizeof buffer, file) != NULL)
      {
        if (++line_count >= 1)
        {
          break;
          }
      }
    What is the purpose of the loop? You read at most one line of text then you "break" out of the loop?
    I was having a friend helping me with this and he added that. I don't know too well what It does apart from count the number of lines?
    I removed it.

    Quote Originally Posted by jimblumberg View Post
    Lastly you need to ask specific questions about the code you provided.
    My if statement keeps tripped and giving me the sscanf failed error how can I resolve this?

    How would I go about making an structure array for the arrays in my new code?

  9. #9
    Registered User
    Join Date
    May 2017
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    That appears to be a spreadsheet not a CSV file. While showing the data in this format helps it doesn't really show the exact format of your input file.
    [Help] Reading in data from .csv file-screenshot_1212-png

    Windows tells me it is a .csv file. It just opens in excel.

    Quote Originally Posted by jimblumberg View Post
    Okay, where are your arrays? But why not consider an array of a struct that contains the information for one complete record?

    Look at this snippet:
    Code:
        if (sscanf(buffer, "%lf,%*lf,%*lf", &value) != 1) {
    If you want to read every field why are you reading one value and throwing away two others?
    I have updated the code below, the reason I was reading one value and throwing away the rest was for 2 reasons.
    1. I was testing
    2. Reading all three during the same runtime gives me an error

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main() {
      char line[1024];
      int line_count = 0;
      FILE *file = fopen("1000.csv", "r");
     
      // Declare Arrays
      int LineNum[1000] = {0}; //ID for the line number
      int type[1000] = {0}; //Type code to idtentify the device (0x20)
      int version[1000] = {0}; //software version
      char counter[1000][2]; //A rolling 8-bit, ever increasing number. Used to show how many messages are being missed by the receiver
      int via[1000] = {0}; //Which receiver picked up this device’s transmission
      char address[1000][8]; //The address of the transmitter
      char status[1000][1]; //The status code of the device
      char sensorData[1000][128]; //10-bytes of sensor data in hex (each sensor being 1 byte and 10 sensors)
     
      if (!file) {
        fprintf(stderr, "ERROR! Could not open file\n");
        exit(EXIT_FAILURE);
      }
     
      while (fgets(line, sizeof line, file) != NULL) {
        int lCounter = 0;
     
        // read in first colum
        if (sscanf(line, "%d", &type[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in second colum
        if (sscanf(line, "%*d,%d", &version[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in third colum
        if (sscanf(line, "%*d,%*d,%d", &counter[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in fourth colum
        if (sscanf(line, "%*d,%*d,%*d,%s", &via[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in fifth colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%d", &address[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in sixth colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%*d,%s", &status[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in seventh colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%*d,%*s,%s", &sensorData[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
     
        printf("%d", type[lCounter]);
        printf("%d", version[lCounter]);
        printf("%d", counter[lCounter]);
        printf("%s", via[lCounter]);
        printf("%d", address[lCounter]);
        printf("%s", status[lCounter]);
        printf("%s", sensorData[lCounter]);
        if (++line_count >= 1000)
          break;
      }
     
      fclose(file);
      return 0;
    }
    Quote Originally Posted by jimblumberg View Post
    Next:
    Code:
      while (fgets(buffer, sizeof buffer, file) != NULL)
      {
        if (++line_count >= 1)
        {
          break;
          }
      }
    What is the purpose of the loop? You read at most one line of text then you "break" out of the loop?
    I was having a friend helping me with this and he added that. I don't know too well what It does apart from count the number of lines?
    I removed it.

    Quote Originally Posted by jimblumberg View Post
    Lastly you need to ask specific questions about the code you provided.
    My if statement keeps tripped and giving me the sscanf failed error how can I resolve this?

    How would I go about making an structure array for the arrays in my new code?

  10. #10
    Registered User
    Join Date
    May 2017
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    That appears to be a spreadsheet not a CSV file. While showing the data in this format helps it doesn't really show the exact format of your input file.
    [Help] Reading in data from .csv file-screenshot_1212-png

    Windows tells me it is a .csv file. It just opens in excel.

    Quote Originally Posted by jimblumberg View Post
    Okay, where are your arrays? But why not consider an array of a struct that contains the information for one complete record?

    Look at this snippet:
    Code:
        if (sscanf(buffer, "%lf,%*lf,%*lf", &value) != 1) {
    If you want to read every field why are you reading one value and throwing away two others?
    I have updated the code below, the reason I was reading one value and throwing away the rest was for 2 reasons.
    1. I was testing
    2. Reading all three during the same runtime gives me an error

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main() {
      char line[1024];
      int line_count = 0;
      FILE *file = fopen("1000.csv", "r");
     
      // Declare Arrays
      int LineNum[1000] = {0}; //ID for the line number
      int type[1000] = {0}; //Type code to idtentify the device (0x20)
      int version[1000] = {0}; //software version
      char counter[1000][2]; //A rolling 8-bit, ever increasing number. Used to show how many messages are being missed by the receiver
      int via[1000] = {0}; //Which receiver picked up this device’s transmission
      char address[1000][8]; //The address of the transmitter
      char status[1000][1]; //The status code of the device
      char sensorData[1000][128]; //10-bytes of sensor data in hex (each sensor being 1 byte and 10 sensors)
     
      if (!file) {
        fprintf(stderr, "ERROR! Could not open file\n");
        exit(EXIT_FAILURE);
      }
     
      while (fgets(line, sizeof line, file) != NULL) {
        int lCounter = 0;
     
        // read in first colum
        if (sscanf(line, "%d", &type[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in second colum
        if (sscanf(line, "%*d,%d", &version[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in third colum
        if (sscanf(line, "%*d,%*d,%d", &counter[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in fourth colum
        if (sscanf(line, "%*d,%*d,%*d,%s", &via[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in fifth colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%d", &address[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in sixth colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%*d,%s", &status[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
        
        // read in seventh colum
        if (sscanf(line, "%*d,%*d,%*d,%*s,%*d,%*s,%s", &sensorData[lCounter]) != 1) {
          fprintf(stderr, "sscanf failed.\n");
          exit(EXIT_FAILURE);
        }
     
        printf("%d", type[lCounter]);
        printf("%d", version[lCounter]);
        printf("%d", counter[lCounter]);
        printf("%s", via[lCounter]);
        printf("%d", address[lCounter]);
        printf("%s", status[lCounter]);
        printf("%s", sensorData[lCounter]);
        if (++line_count >= 1000)
          break;
      }
     
      fclose(file);
      return 0;
    }
    Quote Originally Posted by jimblumberg View Post
    Next:
    Code:
      while (fgets(buffer, sizeof buffer, file) != NULL)
      {
        if (++line_count >= 1)
        {
          break;
          }
      }
    What is the purpose of the loop? You read at most one line of text then you "break" out of the loop?
    I was having a friend helping me with this and he added that. I don't know too well what It does apart from count the number of lines?
    I removed it.

    Quote Originally Posted by jimblumberg View Post
    Lastly you need to ask specific questions about the code you provided.
    My if statement keeps tripped and giving me the sscanf failed error how can I resolve this?

    How would I go about making an structure array for the arrays in my new code?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What's with all of those sscanf() calls? You should only need one sscanf() call to process the entire line. However sscanf() may not be the best tool to parse those lines. Without seeing the actual file format I can only guess. But, because of the magnitude of some of those numbers you may want to read them as strings instead of numbers.

    Perhaps something like:
    Code:
    #define LINE_SIZE 1000
    int main()
    {
        FILE* file;
    ...
    // Don't forget to open and check the file.
        unsigned int line_number;  // A
        double version; // B
        unsigned int missed; // C
        unsigned int counter; // D
        unsigned int receiver; // E
        unsigned long receiver_address; // F
        unsigned int status; // G
        unsigned long long message; // H
        char line[LINE_SIZE];
    
        while(fgets(line, LINE_SIZE, file) != NULL)
        {
            int good = sscanf(line, "%u,%lf,%u,%u,%u,%lx,%u,%llx", &line_number, &version, &missed, &counter, &receiver, &receiver_address, &status, &message);
            if(good != 9)
            {
                puts("Error reading line.");
            }
        }
    }
    Windows tells me it is a .csv file. It just opens in excel.
    Can't you tell Windows to open the file with some other program other than excel, something like wordpad or notebook? By the way that link you posted is broken.

    Jim

  12. #12
    Registered User
    Join Date
    May 2017
    Posts
    11
    What does this part do?
    Code:
     if(good != 9)        {
                puts("Error reading line.");
            }
    I keep getting this message in the console

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well if I could count the it would be telling you there was an error reading the data. Remember sscanf() returns the number of items read (that 9 should probably be 8).

    Jim

  14. #14
    Registered User
    Join Date
    May 2017
    Posts
    11
    I see what you mean now by it not looking like a csv file since in the raw data it is all separated by commas.

    [Help] Reading in data from .csv file-screenshot_1255-png

    Trying a new approach now where I get the entire line of the file and try to separate it into 8 different strings.
    How would I go about doing this?
    I have tried things like strtok and strncpy and not having any luck.
    Last edited by darkrage453; 05-23-2017 at 05:43 AM.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post a sample of your file as text (inside code tags to preserve formatting) into a post, but you should be able to use fgets(), sscanf() to process the file.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from a file
    By SherryAli in forum C Programming
    Replies: 1
    Last Post: 04-04-2013, 03:44 PM
  2. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  3. It is not reading the data from the file...
    By musique in forum C++ Programming
    Replies: 5
    Last Post: 05-01-2009, 03:19 PM
  4. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  5. Reading in data from a file
    By neandrake in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 09:04 PM

Tags for this Thread