ifstream::read not working?
ifstream::read not working?
我正在尝试从.csv
文件读取。下面有两个函数,一个用于写,一个用于读。
文件包含一个简单的表:
date,first,second
1 a one
2 b two
3 c three
4 c four
由于某种原因,语句while(file_stream.read(&c,1));
没有读取任何内容。它停在第一个人物,我目瞪口呆,不知道为什么。有线索吗?
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
std::string filename;
std::string line_string;
ifstream file_stream;
stringstream ss;
vector< vector<string> > vec;
char c;
void read_file()
{
filename = "test.csv";
cout << filename << endl;
file_stream.open(filename.c_str(),ios::out|ios::binary);
if(file_stream.fail())
{
cout << "File didn't open" << endl;
return;
}
if(file_stream.is_open())
cout << "file opened" << endl;
while(file_stream.read(&c,1)); // this isn't working
{
cout <<"char c is: " << c;
ss << noskipws << c;
}
file_stream.close();
cout << "string is: " << ss.str() << endl;
//get each line
int counter = 0;
vector<string> invec;
while(getline(ss,line_string,'n'))
{
string header_string;
stringstream header_stream;
header_stream << line_string;
while(getline(header_stream, header_string,','))
{
invec.push_back(header_string);
}
invec.push_back(header_string);
vec.push_back(invec);
invec.clear();
counter++;
}
}
void test_output()
{
for(int i = 0; i < vec.size();i++)
{
for(int in = 0; in < vec[0].size(); in++)
cout << vec[i][in] << " ";
cout << endl;
}
}
int main()
{
read_file();
test_output();
}
非常非常仔细地查看不正常的行:
while(file_stream.read(&c,1)); // this isn't working
{
cout <<"char c is: " << c;
ss << noskipws << c;
}
while
语句末尾的;
字符不属于!您正在运行一个无体循环,直到read()
失败才终止,然后您的代码进入括号块以输出成功读取的最后一个字符(如果有的话)。
你需要删除错误的;
字符:
while(file_stream.read(&c,1)) // this works
{
cout <<"char c is: " << c;
ss << noskipws << c;
}
现在,真正的问题是—为什么首先要将输入文件中的字符一个字符一个字符地读入std::stringstream
?您可以直接使用std::getline()
和输入std::ifstream
:
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
std::vector< std::vector<std::string> > vec;
void read_file()
{
std::string filename = "test.csv";
std::cout << filename << std::endl;
std::ifstream file_stream;
file_stream.open(filename.c_str(), ios::binary);
if (!file_stream)
{
std::cout << "File didn't open" << std::endl;
return;
}
std::cout << "file opened" << std::endl;
//get each line
std::vector<std::string> invec;
std::string line;
int counter = 0;
if (std::getline(file_stream, line))
{
std::istringstream iss(line);
while (std::getline(iss, line, ','))
invec.push_back(line);
vec.push_back(invec);
invec.clear();
++counter;
while (std::getline(file_stream, line))
{
iss.str(line);
while (iss >> line)
invec.push_back(line);
vec.push_back(invec);
invec.clear();
++counter;
}
}
}
void test_output()
{
if (!vec.empty())
{
for(int in = 0; in < vec[0].size(); ++in)
std::cout << vec[0][in] << ",";
std::cout << std::endl;
for(int i = 1; i < vec.size(); ++i)
{
for(int in = 0; in < vec[i].size(); ++in)
std::cout << vec[i][in] << " ";
std::cout << std::endl;
}
}
}
int main()
{
read_file();
test_output();
}
相关文章:
- 使用 ImageIO.read() 生成的图像是否仍然使用传递给它的相同内存?
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- write() 和 read() 中几乎没有混淆
- C++ TCP 套接字通信 - 连接按预期工作,几秒钟后失败,没有收到新数据,read() 和 recv() 块
- 为什么调试器引发"read access violation. this was nullptr"异常?
- 对同一文件使用 .read 的次数是否有限制?
- Win32 API - HWND "{unused = ???} Unable to read memory"错误
- 为什么我会"Invalid read of size 8"?(瓦尔格林德)
- QDataStream read to QVector
- asio::read() 需要很长时间,使用 asio::write 没有问题
- Read OpenCV Mat 16bit to QImage 8bit Greyscale
- 如果 I/O read() 处于阻塞阶段,如何使用 Ctrl+C 退出 C++ 程序?
- 如何将 (serverClients[i].read() 传递给 ESP8266 上的字节数组
- 将 Lora.read() 转储到数组
- GDB - strcmp not working: __strcmp_sse2_unaligned
- 无法通过 FUSE 文件系统"read"任何内容
- 指向 std::unrodered_map 中元素的指针返回'Read access violation'
- 封送指向结构异常"cannot Read or Write protected memory"的指针数组的指针数组
- C++HTTP客户端在GET请求后挂起read()调用
- ifstream::read not working?