使用C语言在多个CSV文件之间读取、翻译和写入数据。

我有3个CSV文件。其中第一个文件包含4列。

ID,Section1,Section2,Secion3
1,23,12,7
2,11,26,9
. . . .
. . . .
19,30,22,4
20,5,6,16

第一列是ID,其他三列是0到30的随机数。下一个文件是一个 "转换 "文件。它显示了每个数字的对应值。

30,45,44,45
29,44,42,43
28,43,42,41
. . . .
. . . .
1,22,21,22
0,20,21,21

第一列是要读取的数字 接下来的几列是代替这些数字的值 在每一节中。比如,如果你在Section1中读到30,就会被45取代,如果你在Section2中发现29,就会被42取代,以此类推。如果我把转换后的数字写进第三个CSV文件,同样是4列格式,我应该怎么做?到目前为止,除了 "转换 "文件外,由于文件是随机的,所以我在生成文件时没有任何问题,但我不知道如何进行转换。

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

int main (int argc,char *argv[]){
    int i, num1;
    char numbers[20][20];
    char conversions[20][20];

    //File with the initial numbers
    FILE *registry = fopen("registry.csv","w+");
    if (registry == NULL){
        fputs ("File error",stderr); 
        exit (1);
    }
    //Conversion file
    FILE *conversion = fopen("conversion.csv","r");
    if (conversion == NULL){
        fputs ("File error",stderr); 
        exit (1);
    }
    //File where the resulting values will be written
    FILE *results = fopen("results.csv","a");
    if (results == NULL){
        fputs ("File error",stderr); 
        exit (1);
    }

    //Writing the headers
    fprintf(results,"ID,Results1,Results2,Results3\n");
    fprintf(registry,"ID,Section1,Section2,Section3\n");
    srand(time(0));

    //Generating the random numbers
    for(i=1;i<21;i++){
        sprintf(numbers[i],"%d,%d,%d,%d\n",i,rand()%31,rand()%31,
            rand()%31);
        fputs(numbers[i], registry);
    }

    //This is where I don't know how to proceed

    for(i=1;i<21;i++){
        sprintf(conversions[i],"%d,%d,%d,%d\n",i,
            fscanf(registry,"%d,%*s,%*s,%*s\n",num1)...);


    }

我是想做类似于我生成随机数的方法,把所有的东西都保存到一个缓冲区里,然后再写到文件里,我发现fscanf函数可以很好的利用,可以跳过我不需要读取的部分,但是我不知道如何用它来跳过头部和ID列,我还是缺少转换部分。转换文件。

30,45,44,45
29,44,43,43
28,43,42,42
27,43,41,40
26,41,40,40
25,40,39,39
24,39,37,39
23,38,37,38
22,38,36,37
21,36,36,37
20,35,35,36
19,34,34,35
18,33,34,35
17,33,33,34
16,32,32,33
15,32,31,32
14,31,30,31
13,30,30,31
12,29,29,30
11,29,28,29
10,28,28,29
9,28,27,27
8,27,26,26
7,27,26,25
6,26,25,25
5,25,24,24
4,25,23,24
3,24,23,23
2,23,22,21
1,21,21,20
0,20,21,20
0
投票

本答案的范围是地址。//This is where I don't know how to proceed.

做你所描述的,如果不需要在注册表、结果或转换中包含第一列进行存储,那就不要。虽然对人的可读性很好,但它们会使读入、使用数组的任务变得复杂。 未使用的列数据和头行需要数组索引体操来让数据正确地排入数组中.如果没有头行和编号列,仅数组索引就足以跟踪所有需要进行转换的数据。但是,如果没有头行和编号列,仅靠数组索引就足以跟踪进行转换所需的所有内容。如果你觉得第一个列行是你需要的,那么调整下面所示例程的数组索引来适应。

下面的步骤(其中一些你已经做了)是我确定的使转换步骤尽可能简单的关键步骤...

步骤

创建注册表 - 60 - 随机数(0-30),排列成20行,3d数据列。

创建 int reg[20][3] = {{0}};

将注册表读到reg数组中,使用组合 fgets sscanf 例程。

例子:(假设你的代码中已经创建了注册表)

registry = fopen(".\\registry.csv","r");
if (registry)
{
    while(fgets(buf, sizeof(buf), registry))
    {
        sscanf(buf, "%d,%d,%d", &reg[index][0],&reg[index][1],&reg[index][2] );
        index++;
    }
    fclose(registry);
}

转换表----原有文件

创建 int conv[31][3] = {{0}};

将转换表读到conv数组中,使用一个组合 fgets sscanf 例行

一旦您正确创建了这些数组,请执行转换。

创建 int res[20][3] = {{0}};

创建 char buf[80] = {0};

打开 "结果 "文件。 FILE *results = fopen(".\\results.csv","w"); 填充结果。

例如:

//File where the resulting values will be written
FILE *results = fopen(".\\results.csv","w");
if (results)
{
    for(i=0;i<sizeof(reg)/sizeof(reg[0]);i++)
    {
        for(j=0;j<sizeof(reg[0])/sizeof(reg[0][0]);j++)
        {
            res[i][j] = conv[reg[i][j]][j];
        //      |                         |  |___reg col   == conv col
        //      |                         |______value reg == conv row
        //      |________________________________row and column of conv
        }
        sprintf(buf, "%d,%d,%d\n", res[i][0],res[i][1],res[i][2]);
        fputs(buf, results);

    }
    fclose(results);    
}

此时,你可以将结果文件打印成适合你的格式。

以下是一张图片,它显示了使用上述建议的方法对随机生成的结果 registry您提供的数值 conversion 并导致 results. (每一个创建的时候都没有在你的原始代码中显示出第1列,正如我在评论中所建议的那样。) 图像显示的是每组数组数据从0-n的索引。

enter image description here