【发布时间】:2013-06-20 00:23:26
【问题描述】:
在尝试打开“test.dat”文件时很难弄清楚我做错了什么。它似乎正在打开它,但没有读取它以提供输出。该程序应该读取数字的频率。
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fileName;
int aTest;
cout << "Enter a File Name: ";
cin >> fileName;
ifstream inFile (fileName.c_str());
if (! inFile)
{
cout << "!!Error in opening file 'test.dat'"<< endl;
}
vector<int> test(101, 0);
while(inFile >> aTest) {
test[aTest]++;
}
system("pause");
return 0;
}
test.dat 文件
75 85 90 100
60 90 100 85
75 35 60 90
100 90 90 90
60 50 70 85
75 90 90 70
这就是我的输出现在的样子
Enter a File Name: test.dat
Press any key to continue . . .
它的样子
Enter file name: test.dat
100: 3
90: 8
85: 3
75: 3
70: 2
60: 3
50: 1
35: 1
【问题讨论】:
-
您的程序看起来不错,但您没有打印出任何内容,那么您为什么期望输出呢?
-
Ummmm,你读取数组中的值后如何显示...
-
@newbie,这有什么关系?
-
@CarlNorum,我搞错了。
标签: c++ file-io vector output ifstream