首页 > 解决方案 > 读入制表符分隔的文本文件的内容并保存到 C 中的单个数组中

问题描述

我正在尝试使用命令行参数读取文件的内容以读取文件,然后将文件的所有内容存储到单个数组中。

请记住,正在读入的文件实际上是一个矩阵,第一行是一个整数值,表示矩阵的平方大小,后面的行和列是矩阵。

到目前为止,我已经能够动态分配内存,然后使用 fgets 读取文件的内容。在将内容存储到单个数组中以供以后访问时,我有点难过。我尝试使用双变量来保存第二个 while 循环的解析内容,但是当打印到终端时,它只给了我文件中的最后一个变量。

我尝试使用 strdup 来制作字符串的副本,但遇到了一个错误,提示无法将 char 类型的值存储在 char 类型的实体中

这是我的代码。

如果有人可以帮助我,我将不胜感激。

#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);

标签: carraystext-files

解决方案


这是一个例子:

/*
 * 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;
}

推荐阅读