C 语言的 CSV 文件生成器

发布于 2024-10-16 21:55:50 字数 65 浏览 2 评论 0原文

是否有任何 C 程序可以在 Linux 机器上运行,并创建给定尺寸(行 x 列)的 csv 文件并将其存储在硬盘上?

Is there any C program which I can run on a Linux box, and will create a csv file of given dimensions(rows x columns) and store it on hard disk?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九局 2024-10-23 21:55:50

CSV 文件只是一个带有逗号分隔值的纯文本文件,因此您可以在纯文本编辑器中手动创建它。 RFC 4180 中有一个规范。

通常第一行用于列名称,例如:

Name, Account no, Amount
Niels, 1234, $0.99
Thomas, 8888, $10.00
Per, 3454, $9.00
Rasmus, 9412, $99.99

用于创建普通空 CSV 文件的小型 c 程序可能如下所示:

/*
 * makecsv.c 
 */

#include <stdio.h>

int main(int argc, char **argv) {
   if( argc != 3) {
      printf("Mandatory arguments: <rows> <cols>\n");
         return 1;
      }

      int row, col;
      for(row = 0; row < atoi(argv[1]); row++) {
         for(col = 0; col < atoi(argv[2]); col++) {
            if(col > 0) {
               printf(", ");
            }
            /* Default values are "row x col" */
            printf("\"%dx%d\"", row, col);
         }
         printf("\r\n");
       }
       return 0;
}

我将使用以下命令编译并运行它:

$ gcc -o makecvs makecsv.c 
$ ./makecvs 3 4
"0x0", "0x1", "0x2", "0x3"
"1x0", "1x1", "1x2", "1x3"
"2x0", "2x1", "2x2", "2x3"

$

将输出放入文件“unix way”,使用以下命令将输出通过管道传输到文件:

$ ./makecvs 3 4 > myFile.csv

A CSV file is just a plain text file with Comma Separated Values and you can therefore create it by hand in an plain text editor. There is a specification in RFC 4180.

Often the first row is used for column names such as:

Name, Account no, Amount
Niels, 1234, $0.99
Thomas, 8888, $10.00
Per, 3454, $9.00
Rasmus, 9412, $99.99

A small c program to create an plain and empty CSV file could look like:

/*
 * makecsv.c 
 */

#include <stdio.h>

int main(int argc, char **argv) {
   if( argc != 3) {
      printf("Mandatory arguments: <rows> <cols>\n");
         return 1;
      }

      int row, col;
      for(row = 0; row < atoi(argv[1]); row++) {
         for(col = 0; col < atoi(argv[2]); col++) {
            if(col > 0) {
               printf(", ");
            }
            /* Default values are "row x col" */
            printf("\"%dx%d\"", row, col);
         }
         printf("\r\n");
       }
       return 0;
}

I'd compile and run it with the following commands:

$ gcc -o makecvs makecsv.c 
$ ./makecvs 3 4
"0x0", "0x1", "0x2", "0x3"
"1x0", "1x1", "1x2", "1x3"
"2x0", "2x1", "2x2", "2x3"

$

To place the output in a file "the unix way", pipe the output to file using the following commands:

$ ./makecvs 3 4 > myFile.csv
暗藏城府 2024-10-23 21:55:50

如果您正在寻找现有的实用程序,您可能在错误的站点上,但是类似这样的内容会打印一个带有双引号单元格且没有标题行的空 CSV 文件:

for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols-1; ++j) {
        printf("\"\",");
    }
    printf("\"\"\n");
}

获取一些任意数据并将其转换为 CSV 文件是更难。首先,输入数据的格式是什么?其次,正确转义数据。

If you're looking for an existing utility you're probably on the wrong site, but something like this would print an empty CSV file with double-quoted cells and no header row:

for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols-1; ++j) {
        printf("\"\",");
    }
    printf("\"\"\n");
}

Taking some arbitrary data and turning it into a CSV file is harder. First, what's the input data format? Second, escape the data correctly.

昨迟人 2024-10-23 21:55:50

好吧,我进行了搜索并得出:
http://sourceforge.net/projects/libcs​​v/

所以这只是“库”的一个示例" 用于 csv 处理。也许值得一看 glib 和 apr,看看他们的库中是否也有一些东西。或者您可以按照此处的建议“手动”进行操作。

Well I did a search and came up with:
http://sourceforge.net/projects/libcsv/

So this is but one example of a "library" for csv handling. It might be worth a look into glib and maybe apr and see if they do have something in there libraries also. Or you can do it "by hand" as suggested here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文