2016-02-11 34 views
-2

我需要編寫一個讀取txt文件的代碼(包含2列,第1列爲int型,第2列爲雙精度型)從txt文件(C++)中讀取數字後無法正確顯示數字

13 2.7

42 3.1

78 1.6

37 7.8

17 2.7

然後打印出如下所示的表格:

1 | 13 | 2.7

2 | 42 | 3.1

3 | 78 | 1.6

4 | 37 | 7.8

5 | 17 | 2.7

但在運行代碼後,出放像:

click

我不明白爲什麼這會發生。有人可以告訴我我錯了嗎?謝謝。以下是代碼:

#include <iostream> 
#include <fstream> 
#include <string> 
#include <iomanip> 

using namespace std; 

int main() 
{ 
ifstream in_file; 
string filename; 
const int NUM_LINES = 10; 
const int NUM_COLUMNS = 2; 
const int COLUMN_WIDTH = 10; 
cout << "In order to process, please enter the file name: "; 
cin >> filename; 
in_file.open(filename.c_str()); 
if (!in_file.is_open()) 
{ 
    cout << "Cannot open file" << endl; 
} 
else 
{ 
    for (int k = 0; k < NUM_LINES; ++k) 
    { 
     int intValue; 
     double doubleValue; 
     in_file >> intValue >> doubleValue; 
     cout << setw(COLUMN_WIDTH) << (k + 1) << " | "; 
     cout << setw(COLUMN_WIDTH) << intValue << " | "; 
     cout << setw(COLUMN_WIDTH) << doubleValue << endl; 
    } 
} 

system("pause"); 
return 0; 
} 
+1

爲什麼不可讀截圖? –

+0

你爲什麼不讀兩個整數和分隔符? (同樣缺少閱讀行尾) –

+0

http://imgur.com/qvirDWF這裏是鏈接:D – Onion

回答

1

該代碼按照預期在我的機器上運行。我相信你的文件中有字符,可能是某些格式或標題的數據,你沒有考慮。

+1

輸入文件可能是UTF-16格式,頂部有一個字節順序標記。 –

+0

;; ___ ;;所以我該怎麼做。該文件的Unicode格式@Erich – Onion

0

此代碼應與簡單的輸入重定向

#include <iostream> 
using std::cout; 
using std::cin; 
using std::cerr; 
using std::endl; 
#include <vector> 
using std::vector; 
#include <string> 
using std::string; 
#include <cstdlib> 
#include <utility> 
using std::pair; 

int main() { 

    int integer; 
    double decimal_value; 

    vector<pair<int, double>> pair_values; 
    while (cin >> integer >> decimal_value) { 
     pair_values.push_back({integer, decimal_value}); 
    } 

    // print out the values in a table format 
    for (auto val : pair_values) { 
     cout << val.first << '\t' << '|' << '\t' 
      << val.second << endl; 
    } 

    return 0; 
} 

的工作,這應該與文件流

#include <iostream> 
using std::cout; 
using std::cin; 
using std::cerr; 
using std::endl; 
#include <fstream> 
using std::ifstream; 
#include <vector> 
using std::vector; 
#include <string> 
using std::string; 
#include <cstdlib> 
#include <utility> 
using std::pair; 

int main() { 

    // get the input file stream 
    string filename; 
    ifstream fin; 
    cout << "Please enter the input filename : "; 
    cin >> filename; 
    fin.open(filename.c_str()); 
    if (!fin) { 
     cerr << "Could not open file with filename " << filename << endl; 
     exit(1); 
    } 

    int integer; 
    double decimal_value; 

    vector<pair<int, double>> pair_values; 
    while (fin >> integer >> decimal_value) { 
     pair_values.push_back({integer, decimal_value}); 
    } 

    // print out the values in a table format 
    for (auto val : pair_values) { 
     cout << val.first << '\t' << '|' << '\t' 
      << val.second << endl; 
    } 

    return 0; 
} 
+0

爲什麼不'vector >'? –

+0

你是對的!我應該明確地使用過它。這是更高效:)不知道爲什麼我沒有 – Curious

+1

好奇,這是你第二次幫助我,謝謝你T__T我現在正在嘗試代碼,讓我看... – Onion