KolacheMaster
KolacheMaster

Reputation: 41

Read in the contents of a tab delimited text file and save into a single array in C

I am trying to read in the contents of a file using command line arguments to read the file and then store all contents of the file into a single array.

Keep in the mind, the file being read in is actually a matrix with the first row being an integer value designating the square size of the matrix and the following rows and columns being the matrix.

so far I have been able to dynamically allocate memory and then read the contents of the file using fgets. I am kind of stumped when it comes to storing the contents into a single array to access later. I tried using a double variable to hold the parsed contents of the 2nd while loop but when printed to the terminal it only gave me the last variable in the file.

I have attempted using strdup to make a copy of the string but am running into an error that says a value of type char cannot be stored in an entity of type char

here is my code.

If someone could help me, I would be very grateful.

#include<stdio.h>
#include<stdint.h>
#include<stdlib.h>
#include<string.h>

int main(int argc,char **argv){     //char **argv will be used to read the input file

    int i,j,k,l,m,nn;
    long size;
    double mat;
    char *buff;
    char n[100];
    char *file = argv[1];
    long len =0;
    int h =0;

    FILE *output;                   // need to declare the output file

    FILE *f =fopen(file, "rb+");    //utilize argv to read in file, will read file in as binary file to obtain ASCII
    fseek(f, 0L, SEEK_END);         //Want to determine the size of the file to create a buffer 
    size = ftell(f);                //Will seek to the end of the file, declare that position as the size
    rewind(f);                      //Will rewind to the beginning of the file
    buff = (char *)malloc(size);    //Declare the buffer size from fseek

    //Will read in and print out the contents of the file using fgets and printf

    while(fgets(buff,size - 1,f) != NULL){
        printf( "%s\n", buff);

    }
    rewind(f);
    while(fscanf(f, "%lf", &mat) ==1){
        buff[h] = _strdup(n);
        h++;
    }

    printf("matrix stored is %c\n",mat);

Upvotes: 1

Views: 445

Answers (1)

paulsm4
paulsm4

Reputation: 121649

Here's an example:

/*
 * EXAMPLE FILE:
 *   3,3
 *   1,2,3
 *   4,5,6
 *   7,8,9
 *
 * EXAMPLE OUTPUT:
 *   #/rows=3, #/columns=3
 *   1.000000, 2.000000, 3.000000
 *   4.000000, 5.000000, 6.000000
 *   7.000000, 8.000000, 9.000000
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#define MAXLINE 100
#define DELIMITER ","

void print_matrix (int nrows, int ncols, double **mat) {
  int i, j;
  printf("#/rows=%d, #/columns=%d\n", nrows, ncols);
  for (i=0; i < nrows; i++) {
    for (j=0; j < ncols-1; j++) {
      printf("%lf, ", mat[i][j]);
    }
    printf("%lf\n", mat[i][j]);
  }
}

int main(int argc, char *argv[]) {

  FILE *fp = NULL;
  int iret = 1, n = 0, nrows, ncols;
  double **mat, d;
  char *s, line[MAXLINE], *endptr;
  int i, j;

  /* Read .csv filepath from cmd-line */
  if (argc < 2) {
    printf ("USAGE: app <fname>\n");
    goto the_end;
  }

  /* Open file */
  if (!(fp = fopen (argv[1], "r"))) {
    printf ("File open(%s) failed: errno= %d\n",
      argv[1], errno);
    goto the_end;
  }

  /* Read matrix dimensions */
  iret = fscanf(fp, "%d,%d", &ncols, &nrows);
  if (iret != 2) {
    printf ("Unable to read file dimensions, iret= %d, expected 2\n",
      iret);
    goto the_end;
  }

  /* Flush the extraneous newline left in the read buffer after fscanf()... */
  fgetc(fp);

  /* Allocate space for our matrix */
  mat = (double **)malloc(sizeof(double *)*nrows);
  if (!mat) {
    printf ("Memory allocation error @matrix, errno=%d: exiting progam\n",
      errno);
    goto the_end;
  }
  for (i=0; i < nrows; i++) {
    mat[i] = malloc(sizeof(double)*ncols);
    if (!mat[i]) {
      printf ("Memory allocation error @matrix[%d], errno=%d: exiting progam\n",
        i,  errno);
      goto the_end;
    }
  }

  /* Populate the matrix */
  for (i=0; i < nrows; i++) {
    ++n;
    s = fgets(line, MAXLINE, fp);
    if (s == NULL) {
      printf ("fgets read error, line %d, errno=%d: exiting program\n",
        n, errno);
      goto the_end;
    }
    s = strtok(line, DELIMITER);
    for (j=0; j < ncols; j++) {
      d = strtod(s, &endptr);
      if (s == endptr) {
        printf ("strtod(%s) conversion error, errno %d: exiting program\n",
          s, errno);
        goto the_end;
      }
      mat[i][j] = d;
      s = strtok(NULL, DELIMITER);
    }
  }

  /* Print the matrix */
  print_matrix (nrows, ncols, mat);

  /* Set "OK" status */
  iret = 0;

the_end:
  if (fp)
    fclose(fp);
  printf("Done: status=%d (%s)\n",
    iret,
    (iret == 0) ? "Status: OK" : "Status: FAIL");
  return iret;
}

Upvotes: 2

Related Questions