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;;
http://stackoverflow.com/questions/18725522/getline-keeps - 不斷變換 - 新特性 - 如何避免 - 這個getline保持換行。 – stark