-1
我正在嘗試編寫一個程序,該程序需要3個輸入.txt文件,使用模板對文件進行排序,然後將排序後的數據寫入3個輸出文件。我想要排序的3個輸入.txt文件是IntFile.txt,FloatFile.txt和QUOTES.txt。存儲在文件中的數據類型分別是整數,雙精度和字符串。現在,我正試圖讓IntFile.txt進行排序,並且無法讀取.txt文件並將其保存到數組以發送到我的模板進行排序。我無法找到一種方法來從文件中讀取最後一個數據後停止for循環。迄今爲止,我已經寫程序是C++使用模板排序.txt文件
`#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include "sorting.h"
using namespace std;
int main()
{
ifstream inNumbers("IntFile.txt");
ifstream inFloaters("FloatFile.txt");
ifstream inWords("QUOTES.txt");
ofstream outNumbers("SortedInt.txt");
ofstream outFloaters("SortedFloat.txt");
ofstream outWords("SortedQuotes.txt");
int i, length = 0;
int data[100];
if (!inNumbers)
{
cerr << "IntFile.txt file could not be opened" << endl;
exit(1);
}
if (!inFloaters)
{
cerr << "FloatFile.txt file could not be opened" << endl;
exit(1);
}
if (!inWords)
{
cerr << "QUOTES.txt file could not be opened" << endl;
exit(1);
}
for (i = 0; data[i] ; ++i)
{
inNumbers >> data[i];
length += 1;
}
sorting(data[100], length);
}`
這是數字是如何出現在IntFile.txt:
55 67 458 23 81 33
782 375 528
405 324 950 46
14 864 551 38 167 518 630
是在'IntFile.txt'號碼在不同的行? – 2015-02-05 19:36:33
我編輯了問題以顯示示例輸入 – 2015-02-05 19:46:03
@StephenOsborne'sorting(data [100],length);'這不符合您的想法。這不會將數組發送到「排序」。它將(無效)條目'data [100]'發送到排序函數。 – PaulMcKenzie 2015-02-05 19:47:15