繁体   English   中英

我如何使该文件的每一行的第一个数字递增? 那第一行不被跳过呢? 在C中

[英]How do I get this file the first number of each line to increment it? And what about the first line not being skipped? In C

我生成了此CSV:

1;0
2;0
2;0
2;0
2;1
2;2
2;2
2;2
2;2
2;3

...

我如何使该文件的每一行的第一个数字递增? 那第一行不被跳过呢?

int gerarRelatorioHash(int colisoes) {
     FILE *pont_arq;

     pont_arq = fopen("relatorio.csv", "r+");

     if(pont_arq == NULL) {
        printf("Erro na abertura do arquivo!");
        return 1;
    }

    int qtd;
    fscanf(pont_arq, "%d", &qtd);
    qtd++;

    fseek(pont_arq, 0, SEEK_END);\
    fprintf(pont_arq, "\n%d;%d", qtd,colisoes);

    fclose(pont_arq);
    printf("Dados gravados com sucesso!\n");
    return 0;
}

qtd是一个计数器,它跳过的每一行都会执行qtd ++ ,第二个参数是传递的值。

示例应为:

1;0
2;0
3;0
4;0
5;1
6;2
7;2
8;2
9;2
10;3
11;3
....

不清楚你在问什么。 但是我认为以下代码可以通过适当的更改来解决您的问题。

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

/* Define your own output format here
     Example: [line: %5d] [first: %5d] [second: %5d]
 */
void print_line(FILE *fp, int nr, int e1, int e2)
{
    fprintf(fp, "[line: %5d] [first: %5d] [second: %5d]\n", nr, e1, e2);
}

int deal(const char *input_path, const char *output_path, bool append, 
    void(*print_line)(FILE *, int, int, int))
{
    FILE *ifp;
    FILE *ofp;
    char buff[1024];
    int elems[2];
    long nr = 0;

    if (!strcmp(input_path, ""))
        ifp = stdin;
    else
        ifp = fopen(input_path, "r");
    if (!ifp)
    {
        perror("open");
        return -1;
    }

    if (!strcmp(output_path, ""))
        ofp = stdout;
    else
        ofp = fopen(output_path, append? "a" : "w");
    if (!ofp)
    {
        perror("open");
        return -1;
    }

    while(fgets(buff, 1024, ifp))
    {
        sscanf(buff, "%d;%d", elems, elems + 1);
        print_line(ofp, nr++, elems[0], elems[1]);
    }

    if (strcmp(input_path, ""))
        fclose(ifp);
    if (strcmp(output_path, "") && strcmp(output_path, input_path))
        fclose(ofp);

}

void usage(const char *prog)
{
    printf("Usage: %s [option]\n", prog);
    printf("    -i <input_path>     input file path [default: stdin]\n");
    printf("    -o <output_path>    output file path [default: stdout]\n");
    printf("    -a                  append content in output file\n");
    printf("    -h                  print help message\n");
}

int main(int argc, char *argv[])
{
    int opt;
    char *input_path = "";
    char *output_path = "";
    bool append = false;

    while ((opt = getopt(argc, argv, "i:o:ah")) != -1)
    {
        switch(opt)
        {
            case 'i':
                input_path = optarg;
                break;
            case 'o':
                output_path = optarg;
                break;
            case 'a':
                append = true;
                break;
            case 'h':
                usage(argv[0]);
                return 1;
        }
    }

    return deal(input_path, output_path, append, print_line);
}

这是产地num.csv

1;0
2;0
2;0
2;0
2;1
2;2
2;2
2;2
2;2
2;3

运行./text -i num.csv -o num.csv -a

1;0
2;0
2;0
2;0
2;1
2;2
2;2
2;2
2;2
2;3
[line:     0] [first:     1] [second:     0]
[line:     1] [first:     2] [second:     0]
[line:     2] [first:     2] [second:     0]
[line:     3] [first:     2] [second:     0]
[line:     4] [first:     2] [second:     1]
[line:     5] [first:     2] [second:     2]
[line:     6] [first:     2] [second:     2]
[line:     7] [first:     2] [second:     2]
[line:     8] [first:     2] [second:     2]
[line:     9] [first:     2] [second:     3]

您可以只定义自己的print_line()来打印所需的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM