简体   繁体   English

如何使用 c 编程计算 csv 文件中的行数和列数

[英]How to count no of rows and column in csv file using c programming

#include <stdio.h>
 
int main()
{
    FILE *fileptr;
    int count_lines = 0;
    char filechar[40], chr;
 
    printf("Enter file name: ");
    scanf("%s", filechar);
    fileptr = fopen(filechar, "r");
    chr = getc(fileptr);
    while (chr != EOF)
    {
        if (chr == 'n')
        {
            count_lines = count_lines + 1;
        }
            chr = getc(fileptr);
    }
    fclose(fileptr); //close file.
    printf("There are %d lines in %s  in a file\n", count_lines, filechar);
    return 0;
}

here i am able to count the no of row but i dont know how to count the no of col please help me out the main is i not am able to count no of comma in csv file so if i am able to count no of comma then it easy to know how much column in csv file在这里我可以计算行数,但我不知道如何计算 col 的数量,请帮助我主要是我无法在 csv 文件中计算逗号的数量,所以如果我能够计算逗号的数量那么很容易知道 csv 文件中有多少列

#include<stdio.h>
#include<conio.h>

int main(){

    FILE *fptr;
    int countline = 0, comma = 0;
    char file_char[40], chr;

    printf("Enter the file name");
    scanf("%s", file_char);

    fptr = fopen(file_char, "r");
    chr = getc(fptr);

    while(chr != EOF){
        if((chr == ",") && (countline == 0)){
            comma++;
        }
        if(chr == "\n"){
            countline++;
        }
        chr = getc(fptr);
    }
    fclose(fptr);
    printf("THERE %d line and %d rows", countline, comma + 1);
    return 0;
}

now i do the changes but still it show me problem现在我做了改变,但它仍然显示我的问题

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

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