如何使用c++将CSV文件的内容显示到数组中
How display content of CSV file into an array using c++
我想以行和列的形式显示csv数据,例如
名称uid类
nnn 1 A
bbb 2 B
ccc 3 A
这就是我迄今为止所尝试的:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream ifile;
char s[100], fname[20];
cout<<"Enter file name to read and display its content" ;
cin>>fname;
ifile.open(fname);
if(!ifile)
{
cout<<"Error in opening file..!!";
getch();
exit(0);
}
while(ifile.eof()==0)
{
ifile>>s;
cout<<s<<" ";
}
cout<<"n";
ifile.close();
getch();
}
这是我显示csv数据的代码,但它实际上在一行中显示了所有数据。
您可以使用getline成员函数来获取完整的行。然后更改这一行:
cout<<s<<" ";
至
cout<<s<<"n";
此外,我认为你的while循环条件是不正确的。
使用std::getline
读取一行(包括逗号)。从这行生成std::stringstream
。
然后使用std::getline
根据逗号分隔行。您将不得不使用嵌套循环。
代码草图:
std::string line, word;
while (std::getline(ifile, line))
{
std::stringstream stream(line);
while (std::getline(stream, word, ','))
{
... (whatever you want to do with each word)
}
std::cout << 'n'; // whatever you want to do at end of line
}
.csv文件的一行(行)中的单元格用";"检查
例如,当您从.csv文件中读取一行时,该行的每一列都用";"进行检查符号,您可以将其用于一行中的单独列!
相关文章:
- 显示数组中的学生 ID 和最高分
- 我编写了一个程序来显示数组的内容,但它显示的是它的地址
- 为什么断点显示数组的第二个值是一个大数字?额外学分工作
- 使用指针显示数组的值
- 我无法显示包含整数最大值的索引. 但我可以显示数组中整数的最高值
- 数组:如何在指定索引之前显示数组的第一个和最后一个元素以及数组元素的差异?
- 使用增量/减少操作员在C 中显示数组的奇怪结果
- 将25个甚至整数存储到一个名为intlist的整数阵列中,然后在屏幕上显示数组,并在屏幕上显示名为fivintegers
- 显示数组的问题.一切都放在一个单独的行中,但不放入单独的列中
- strlen 显示数组元素的数量错误
- 我的函数不会用" "替换".",也不会按预期显示数组
- 以C++为单位乘以并显示数组的值
- 找不到标识符,尝试在 C++ 中显示数组值
- 如何显示数组名称中的名称
- 在列表小部件 Qt C++中显示数组内容
- c++将.txt文件中的字符串添加到字符串数组中,并显示数组的值
- C++如何将数组传递给函数,然后调用它来显示数组的值
- 从函数更新(覆盖)和显示数组
- 在单独的数据中显示数组数据
- 仅显示数组文本文件中的第一个字母