简体   繁体   English

逐部分读取逗号分隔的文件

[英]Reading a comma delimited file part by part

I have an assignment from school and have to read a comma delimited file and put each value in to a char*. 我有一个学校的作业,必须读取逗号分隔的文件,并将每个值放入char *中。

Ex) 例)

File contains: 文件包含:

5,Justin,19,123-4567,etc..

char * firstValue = 5;

char * secondValue = Justin;

char * thirdValue = 123-4567; etc..

I cannot use std::string since we haven't learned it yet. 我还不能使用std :: string,因为我们还没有学到它。 I am supposed to do it with ifstream or other file streams. 我应该用ifstream或其他文件流来做到这一点。 I have no idea how to do this 我不知道该怎么做

For homework assignments iterating over the char array in a for loop and every time a comma is encountered splitting the string will work (I won't give the code because it is a good exercise to try to work out yourself). 对于作业分配,它会在for循环中遍历char数组,并且每次遇到逗号时都可以拆分字符串(我不会给出代码,因为这是尝试锻炼自己的好方法)。 But for real world use you should be aware that the CSV format is considerably more complex. 但是对于现实世界而言,您应该知道CSV格式要复杂得多。 Consider multiline entries or entries with an escaped comma and how they break the above solution. 考虑多行条目或带转义逗号的条目,以及它们如何破坏上述解决方案。 I strongly recommend using libcsv for any real world CSV parsing. 我强烈建议将libcs​​v用于任何实际的CSV解析。

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

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