2016-10-25 84 views
1

我正在嘗試從.csv文件讀取。下面有兩個功能,一個用於書寫,另一個用於閱讀。ifstream :: read不工作?

該文件包含一個簡單的表:

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

什麼變化到std :: fstream的::嗎? – domonica

回答

4

非常非常仔細在該行不工作:

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

哈哈..謝謝你..這是需要幾個小時,這是一個愚蠢的錯字到最後。感謝您將這引起我的注意。我自己的愚蠢的錯誤。 – domonica

相關問題