2016-12-13 65 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