使用fscanf()扫描到C中的数组时出现问题 [英] Problems using fscanf() to scan into an array in C

查看:113
本文介绍了使用fscanf()扫描到C中的数组时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要扫描的文本文件。文件中的第一个数字是矩阵中的行数,第二个数字是矩阵中的列数。

I have a text file that I'm scanning in. The first number in the file is the number of rows in the matrix, the second number is the number of columns in the matrix.

文本文件

4    
10
3.000000,1.000000,1180.000000,1955.000000,221900.000000
3.000000,2.250000,2570.000000,1951.000000,538000.000000
2.000000,1.000000,770.000000,1933.000000,180000.000000
.
.
.

n(4,10)矩阵

我正在使用fscanf来存储读取的数组,并使用两个循环读取在二维数组中接收的值。

I'm using fscanf to store read the array and two loops to read the values received in a two d array.

double hold;
fscanf(fpointer,"%d",&value);//gets me 4
fscanf(fpointer,"%d",&lastvalue);/*gets me 10*/
for (i=0; i<value; i++)
{
for (j=0; j<lastvalue; j++)    //Supposed to input the other values
{
  fscanf(fpointer,"%lf",&hold); array[i][j]=hold;

我正在通过两个for循环打印数组内容。

I'm printing the array contents through two for loops.

for(i=0;i<value;i++){
        for(j=0;j<lastvalue;j++){

  printf("%lf\t", array[i][j]); 
                            }
  printf("\n");

但是我收到循环的第一个索引重复作为输出。

Yet I receive the first index of the loop repeating as an output.

3.000000    3.000000    3.000000    3.000000    3.000000    3.000000    3.000000    3.000000    3.000000    3.000000    
3.000000    3.000000    3.000000    3.000000    3.000000    3.000000    3.000000    3.000000    3.000000    3.000000    

我已经花了很多时间,但是我不确定我的逻辑是否正确或者我对fscanf不了解。我最初以为fscanf遇到逗号问题。我尝试使用逗号作为分隔符,就像我在另一篇文章中看到的那样( fscanf(...,%lf [^,] ...)。我收到了

I've spent quite some time on this and I'm not sure if my logic is incorrect or something I'm not understanding about fscanf. I initially thought fscanf was having trouble with the comma. I tried using the comma as a delimiter as I saw on another post(fscanf(...,"%lf[^ ,]"...).I received the same output. Please let me know what's wrong and how to fix it.

推荐答案

您需要处理逗号。这是一个相同的输出。方式:

You need to deal with the commas. Here's one way:

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


void die(char *msg) {
  fprintf(stderr, "Error: %s\n", msg);
  exit(1); 
}

double **read_matrix(FILE *f) {
  int m, n;
  if (fscanf(f, "%d%d", &m, &n) != 2) die("Couldn't scan matrix size");
  if (m <= 0 || n <= 0) die("Bad matrix size");
  double **matrix = malloc(m * sizeof(double*));
  if (!matrix) die("Couldn't allocate matrix spine");
  for (int i = 0; i < m; ++i) {
    matrix[i] = malloc(n * sizeof(double));
    if (!matrix[i]) die("Couldn't allocate matrix row");
    // Read the first column. No comma.
    if (fscanf(f, "%lf", &matrix[i][0]) != 1) die("Couldn't read matrix (1)");
    // Read the other columns. Skip preceding commas.
    for (int j = 1; j < n; ++j)
      if (fscanf(f, ",%lf", &matrix[i][j]) != 1) die("Couldn't read matrix (2)");
  }
  return matrix;
}

int main(void) {
  double **matrix = read_matrix(stdin);
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) printf(" %lf", matrix[i][j]);
    printf("\n");
  } 
  return 0;
}

这篇关于使用fscanf()扫描到C中的数组时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆