ifstream :: read不工作?(ifstream::read not working?)

编程入门 行业动态 更新时间:2024-10-12 03:16:05
ifstream :: read不工作?(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(); }

I am trying to read from a .csv file. There are two functions below, one for writing and one for reading.

The file contains a simple table:

date,first,second 1 a one 2 b two 3 c three 4 c four

For some reason, the statement while(file_stream.read(&c,1)); does not read anything. It stops at the first character and I'm dumbfounded as to why. Any clues?

#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(); }

Look very very carefully at the line that is not working:

while(file_stream.read(&c,1)); // this isn't working { cout <<"char c is: " << c; ss << noskipws << c; }

The ; character at the end of the while statement does NOT belong! You are running a no-body loop that does not terminate until read() fails, and THEN your code enters the bracketed block to output the last character that was successfully read (if any).

You need to remove that erroneous ; character:

while(file_stream.read(&c,1)) // this works { cout <<"char c is: " << c; ss << noskipws << c; }

Now, the real question is - why are you reading the input file character-by-character into a std::stringstream in the first place? You can use std::getline() with the input std::ifstream directly:

#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(); }

更多推荐

本文发布于:2023-07-27 02:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284060.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   ifstream   read   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!