从在C文件中读取逗号分隔的数字 [英] Read comma separated numbers from a file in C

查看:357
本文介绍了从在C文件中读取逗号分隔的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图读取逗号分隔的数字文件时,我有一个问题,我想有一个创建整数数组(不知道有多少参数数组有先)在这样的文件中的函数:

  1,0,3,4,5,2
3,4,2,7,4,10
1,3,0,0,1,2

和等。我要的结果是一样的东西。

  INT v [] = {1,0,3,4,5,2}

为文件中的每一行(很明显,在每一行中的值),所以我可以将此阵列添加到基质。我试着用的fscanf,但我似乎无法使其停止在每一行的末尾。我也试过与fgets,strtok的,和其他许多建议,我在网上找到的,但我不知道该怎么办了!

我在32位机使用Eclipse靛蓝。


解决方案

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释主(){
    FILE * FP;
    int数据,行,列,C,计数,INC;
    为int *阵列,容量= 10;
    焦炭CH;
    阵列=(INT *)malloc的(的sizeof(int)的*容量);
    FP = FOPEN(data.csv,R);
    排= COL = C =计数= 0;
    而(EOF =(INC =的fscanf(FP,%D%C,&安培;!数据,和放大器; CH))及和放大器; INC == 2){
        ++℃; //列数
        如果(容量==计数)
            阵=为(int *)的realloc(阵列的sizeof(int)的*(容量* = 2));
        数组[统计++] =数据;
        如果(CH =='\\ n'){
            ++排;
            如果(COL == 0){
                COL = C;
            }否则如果(COL!= C){
                fprintf中(标准错误,在%d个\\ n个不同的列行的格式错误,行);
                转到退出;
            }
            C = 0;
        }否则如果(CH!=','){
            fprintf中(标准错误,在%d个\\ n行的不同的分隔符(%C)的格式错误,CH,行);
            转到退出;
        }
    }
    {//检查打印
        INT I,J;
// INT(*矩阵)[COL] =阵列;
        对于(i = 0; I<排; ++ I){
            为(J = 0; J<西++ j)条
                的printf(%d个,数组[我* COL + J]); //矩阵[I] [J]。
            的printf(\\ n);
        }
    }
出口:
    FCLOSE(FP);
    免费(数组);
    返回0;
}

I have a problem when trying to read a file with comma separated numbers, I want to have a function that creates arrays of integers (not knowing how many parameters the array has at first) in a file like this:

1,0,3,4,5,2
3,4,2,7,4,10
1,3,0,0,1,2

and so on. The result I want is something like

int v[]={1,0,3,4,5,2}

for every line in the file (obviously with the values in each line) so I can add this array to a matrix. I tried using fscanf, but I can't seem to make it stop at the end of each line. I also tried fgets, strtok, and many other suggestions I found on the Internet, but I don't know how to do it!

I'm using Eclipse Indigo in a 32-bit machine.

解决方案

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

int main(){
    FILE *fp;
    int data,row,col,c,count,inc;
    int *array, capacity=10;
    char ch;
    array=(int*)malloc(sizeof(int)*capacity);
    fp=fopen("data.csv","r");
    row=col=c=count=0;
    while(EOF!=(inc=fscanf(fp,"%d%c", &data, &ch)) && inc == 2){
        ++c;//COLUMN count
        if(capacity==count)
            array=(int*)realloc(array, sizeof(int)*(capacity*=2));
        array[count++] = data;
        if(ch == '\n'){
            ++row;
            if(col == 0){
                col = c;
            } else if(col != c){
                fprintf(stderr, "format error of different Column of Row at %d\n", row);
                goto exit;
            }
            c = 0;
        } else if(ch != ','){
            fprintf(stderr, "format error of different separator(%c) of Row at %d \n", ch, row);
            goto exit;
        }
    }
    {   //check print
        int i,j;
//      int (*matrix)[col]=array;
        for(i=0;i<row;++i){
            for(j=0;j<col;++j)
                printf("%d ", array[i*col + j]);//matrix[i][j]
            printf("\n");
        }
    }
exit:
    fclose(fp);
    free(array);
    return 0;
}

这篇关于从在C文件中读取逗号分隔的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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