0

例如,我在记事本中保存了这个表格。

x's 1 2 3 4 5   
y's 2 4 5 6 7

使用fscanf和文件处理。例如,如果我声明了array1 和array2,我如何存储x 和y 的值。(将x存储在array1上,y存储在array2上)

4

2 回答 2

2

这可能对您有用,只需避免 .txt 文件中的多余空格字符

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

void main ()
{
    int *x;
    int *y;
    int d,sizey;
    char ch;
    int sizex = 1;
    int size = 0;
    int i = 0;
    FILE * pFile;

    pFile = fopen ("text.txt","r");
    while (fscanf (pFile, "%d", &d) != EOF)
    {
        size++;
    }

    rewind (pFile);
    fscanf (pFile, "%c", &ch);
    while (ch != '\n')
    {
        if (ch == ' ')
        sizex++;
        fscanf (pFile, "%c", &ch);
    }

    sizey = size - sizex;
    rewind (pFile);

    x = (int*) malloc(sizex*sizeof(int));
    y = (int*) malloc(sizey*sizeof(int));

    for(i = 0;i<sizex;i++)
    {
    fscanf (pFile, "%d", &d);
    x[i]=d;
    }

    for(i = 0;i<sizey;i++)
    {
    fscanf (pFile, "%d", &d);
    y[i]=d;
    }
}
于 2013-08-17T19:13:15.780 回答
0

以下将从文件中读取正整数来创建:

  1. data包含整数值的可变长度二维数组
  2. 一个可变长度的一维数组size,其中包含每行的可变大小data
  3. totalRows包含行数的变量data

C代码:

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

int main() {
  uint32_t fileEnded,lineEnded,tokenEnded,val;
  uint32_t col,row,i,j;
  uint32_t startSize = 5;
  uint32_t totalCols = 2;
  uint32_t totalRows = 2;
  uint32_t bufLen    = 5;
  uint32_t c;

  uint32_t **data = (uint32_t**)malloc(totalRows * sizeof(uint32_t*));
  uint32_t *size  = (uint32_t* )malloc(totalRows * sizeof(uint32_t ));
  char     *buf   = (char*     )malloc(bufLen * sizeof(char));
  FILE     *fp    = fopen("path/to/file.data", "r");
  /* Check for bad initializations */
  if(size==NULL || data==NULL || buf==NULL || fp==NULL)
    return EXIT_FAILURE;

  /* While the file has not ended, for each line in the file*/
  for(fileEnded=row=0; !fileEnded; ++row) {
    if(row >= totalRows) {
      data  = (uint32_t**)realloc(data, (totalRows*=2) * sizeof(uint32_t*));
      size  = (uint32_t* )realloc(size,  totalRows     * sizeof(uint32_t ));
    }
    data[row] = (uint32_t* )malloc(startSize * sizeof(uint32_t));
    totalCols = startSize;

    /* for each line, process each token */
    for(lineEnded=col=0; !lineEnded; ) { 
      /* get each numeric token, character by character */
      for(tokenEnded=i=0; !tokenEnded; ) { 
        c = fgetc(fp);
        switch(c) {
          case EOF : fileEnded = 1; /* Intentional Fall-Through */
          case '\n': lineEnded = 1; /* Intentional Fall-Through*/
          /* Common Token Delimiters */
          case '\t':
          case '\v':
          case ' ' :
          case '|' :
          case ',' :
          case ';' : tokenEnded = 1; break;
          default:
            if(isdigit(c)) {
              if(i >= bufLen)
                buf = (char*)realloc(buf, (bufLen*=2) * sizeof(char));
              buf[i++] = (char)c; /* only increment i when adding data to buffer */
            }
        }
      }
      if(i==0) /* no numeric data gathered, don't increment col */
        continue;
      buf[i] = '\0'; /* terminate the token string */
      sscanf(buf,"%u",&val);
      if(col >= totalCols)
        data[row]  = (uint32_t*)realloc(data[row], (totalCols*=2) * sizeof(uint32_t));
      data[row][col++] = val;
    }
    size[row] = col;
    data[row] = (uint32_t*)realloc(data[row], size[row] * sizeof(uint32_t));
  }
  /* finalize the variables */
  totalRows = row;
  data = (uint32_t**)realloc(data, totalRows * sizeof(uint32_t*));
  size = (uint32_t* )realloc(size, totalRows * sizeof(uint32_t ));


  /* Print back the values just to be sure the were read correctly */
  for(i=0; i<totalRows; ++i) {
    for(j=0; j<size[i]; ++j)
      printf("%u ",data[i][j]);
    printf("\n");
  }
  return EXIT_SUCCESS;
}
于 2013-08-17T18:32:30.103 回答