[英]How to read a csv file in C?
I am trying to read a CSV file in C.我正在尝试读取 C 中的 CSV 文件。 The CSV file contains one column and 1024 rows.
CSV 文件包含一列和 1024 行。 The CSV file contains only decimal numbers.
CSV 文件仅包含十进制数字。 So far I have this code, but the numbers from the output and the CSV file do not match.
到目前为止,我有这个代码,但是 output 和 CSV 文件中的数字不匹配。 The CSV file is called file.csv
CSV 文件称为 file.csv
enter image description here在此处输入图像描述
enter image description here在此处输入图像描述
int main(void)
{
FILE *stream;
errno_t err;
err = fopen_s(&stream, "file.csv", "r");
if (stream == NULL) {
printf("\n file opening failed ");
return -1;
}
double values[1024];
int count;
for (count = 0; count < 1024; count++) {
double u = fscanf_s(stream, "%d", &values[count]);
printf("%d\n", u);
}
return(0);
}
Scan into and print double
objects using %lg
, not %d
.使用
%lg
而不是%d
扫描并打印double
对象。
The return type of fscanf_s
is an int
, not a double
. fscanf_s
的返回类型是int
,而不是double
。 Test it to see if the desired number of items were assigned (one, in your case), and, if not, print an error message and exit the program.测试它以查看是否分配了所需数量的项目(在您的情况下为一个),如果没有,则打印错误消息并退出程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.