2016-12-13 100 views
1

我很感興趣的是爲什麼輸出後跟輸出的數字的輸出不在同一行上,除了第一次出現以外,任何解決方案或推理都將非常讚賞。 有沒有辦法阻止/ n getline讀取? 如果是這樣怎麼樣?試圖打印輸出數組,但在打印前輸入新行

#include<iostream> 
#include <fstream> 
#include <string> 
#include <vector> 
#include<sstream> 
using namespace std; 

const int NCOLS = 4; 
const int NROWS = 10; 

//void description_and_options(string data[][NCOLS], int count[NCOLS]); 

int main() 
{ 
    ifstream file_name;//create the new file 
    string user_input_file;//the files name inputed by the user 
    string starting_array[8]; 
    int stringlength; 
    string read_in_array[NROWS][NCOLS]; 
    string line; 
    int counter = 1; 

    cout << "Enter the name of the input file: "; 

    cin >> user_input_file; 



    if (user_input_file.length() > 4)// check to see if its more than 4 in length 
    { 
     stringlength = user_input_file.length(); //saves length 
     if (user_input_file.substr(stringlength - 4, 4) == ".txt")//checks to see if its .dat 
     { 
      file_name.open(user_input_file.c_str()); 
      if (file_name.fail()) 
      { 
       cerr << "The file " << user_input_file << " failed to open.\n";//tells user if it fails 
       exit(1); 
      } 
     } 
    } 
    else 
    { 
     user_input_file += ".txt";//adds .dat to non .dat 
     file_name.open(user_input_file.c_str()); 
    } 

    if (file_name.fail()) 
    { 
     cout << "File failed to open" << endl; 
     system("PAUSE"); 
     exit(1); 
    } 


    //for (int row = 0; row <=9; row++) 
    for (int row = 0; row <= 9; row++) 
    { 
     for (int col = 0; col < 4; col++) 
     { 
      if (getline(file_name, line, ';')) 
      { 
       read_in_array[row][col] = line; 
       // cout << read_in_array[row][col]; 
      } 
     } 
     //cout << counter[row]<<read_in_array[row][0]<<endl; 

    } 
    //[updown][leftright] 
    file_name.close(); 
    for (int lcv = 0; lcv < 9; lcv++) 
    { 
     cout << counter<< " "<< read_in_array[lcv][0] << endl; 
     counter++; 
    } 
    cout << endl; 
    // description_and_options(read_in_array, counter); 
    system("PAUSE");//pause 
} 

輸出應該是沿着

1 Google (on one line) 
2 Deviantart(on the next line) 
3 Dragcave.net(and so on) 
etc... 

和數據文件中的行是

Google;[email protected];Kyleman27;security question:White rabbit with a watch; 
Deviantart;Dragonmaster27;Gandalfthegrey; NULL; 
Dragcave.net;Dragonmaster27; DragonM27; Notes: username shortend; 
Youtube.com;DragonMaster207; DragonM207; Notes: 207 not 27; 
Facebook.com; KyleSmith27; KsmithFB; NULL; 
Twitter.com; KyleSmith207; KsmithT; NULL; 
Blogger; Kylesmith207; KyleSmith27; Notes: password 27 not 207; 
Yahoo.com; [email protected]; kSmith08; yahoo has a . before 01; 
Jibjab.com;Kyle.207; KyleSmit.2.7; .2.7; 
Dragonworld.com;KyleDragonMaster;DragonMkyle;; 
+1

http://stackoverflow.com/questions/18725522/getline-keeps - 不斷變換 - 新特性 - 如何避免 - 這個getline保持換行。 – stark

回答

0

getline看到:...rabbit with a watch;\nDeviantart;...,那麼接下來的讀取變得\nDeviantart

您可以使用兩個getline循環;一個讀線,並且內的第一的第二讀取每個值高達;

#include <iostream> 
#include <string> 
#include <vector> 
#include <sstream> 
#include <iomanip> 

extern const char* file_data; 

int main() 
{ 
    std::istringstream is{file_data}; //could replace with ifstream 
    std::vector<std::vector<std::string>> read_in_array{}; 
    for(std::string line{}; std::getline(is, line);) { 
     std::istringstream line_is{line}; 
     std::vector<std::string> row{}; 
     for(std::string value{}; std::getline(line_is, value, ';');) { 
      row.push_back(value); 
     } 
     read_in_array.push_back(row); 
    } 

    size_t counter{0}; 
    for(const auto& row : read_in_array) { 
     std::cout << counter++ << " "; 
      for(const auto& val : row) { 
       std::cout << std::setw(25) << val; 
     } 
      std::cout << '\n'; 
    } 
} 

const char* file_data = 
R"(Google;[email protected];Kyleman27;security question:White rabbit with a watch; 
Deviantart;Dragonmaster27;Gandalfthegrey; NULL; 
Dragcave.net;Dragonmaster27; DragonM27; Notes: username shortend; 
Youtube.com;DragonMaster207; DragonM207; Notes: 207 not 27; 
Facebook.com; KyleSmith27; KsmithFB; NULL; 
Twitter.com; KyleSmith207; KsmithT; NULL; 
Blogger; Kylesmith207; KyleSmith27; Notes: password 27 not 207; 
Yahoo.com; [email protected]; kSmith08; yahoo has a . before 01; 
Jibjab.com;Kyle.207; KyleSmit.2.7; .2.7; 
Dragonworld.com;KyleDragonMaster;DragonMkyle;;)"; 

產地:

0      Google [email protected]    Kyleman27security question:White rabbit with a watch 
1     Deviantart   Dragonmaster27   Gandalfthegrey      NULL 
2    Dragcave.net   Dragonmaster27    DragonM27 Notes: username shortend 
3     Youtube.com   DragonMaster207    DragonM207  Notes: 207 not 27 
4    Facebook.com    KyleSmith27     KsmithFB      NULL 
5     Twitter.com    KyleSmith207     KsmithT      NULL 
6      Blogger    Kylesmith207    KyleSmith27 Notes: password 27 not 207 
7     Yahoo.com [email protected]     kSmith08 yahoo has a . before 01 
8     Jibjab.com     Kyle.207    KyleSmit.2.7      .2.7 
9    Dragonworld.com   KyleDragonMaster    DragonMkyle