c - 如何获取该文件每行的第一个数字以递增它?那么第一行不被跳过呢?在C中

标签 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() 来打印您想要的内容。

关于c - 如何获取该文件每行的第一个数字以递增它?那么第一行不被跳过呢?在C中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51182450/

相关文章:

将 GPIO_TypeDef 从 STM32L1xx 转换为 STM32F10x

c - 如何隐藏在多个文件中可见的全局变量?

c++ - 结构一字节对齐与体系结构的对齐要求冲突?

C 非标准库

c - 获取被调用函数C的行号

c - 虚拟机内部的CLOCKS_PER_SEC值是 "wrong"

c - linux 中的闹钟和 sleep

C 温度转换程序保持输出 0 华氏度到摄氏度

C 指针算术

c - 终止C语言程序