简体   繁体   English

在C中使用fscanf读取csv文件

[英]Read csv file using fscanf in C

I have to read a csv file using fscanf function (I cant use any other function like strtok_s to parse the line) and im having the following problem. 我必须使用fscanf函数读取csv文件(我不能使用任何其他函数,例如strtok_s来解析行),并且im出现以下问题。

Here is the code: 这是代码:

fp1 = fopen (argv [1],"r");

var = fscanf (fp1,"%d,%d,%[^,]s,%[^,]s",&aux.points,%aux.titles,aux.name,aux.nation);

I'm trying to print each parameter in the screen. 我正在尝试在屏幕上打印每个参数。 There is no problem with the integers and even with the first string (name) but nothing is stored in the next string (nation). 整数甚至第一个字符串(名称)都没有问题,但下一个字符串(国家)中没有存储任何内容。

I assume that the first %[^,]s is stopping the execution of the whole fscanf function so the next string is never read. 我假设前一个%[^,]s正在停止执行整个fscanf函数,因此永远不会读取下一个字符串。 Any idea? 任何想法? I have tried everything but this is just not working. 我已经尝试了一切,但是这是行不通的。

Try this as the string: "%d,%d,%[^,],%[^,]" 尝试将其作为字符串:“%d,%d,%[^,],%[^,]”

I eliminated the "s" because [...] acts as the specifier. 我删除了“ s”,因为它是说明符。

Think of the [...] as a super s. 认为超级。

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

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