#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
void getFileName(ifstream& input, ofstream& output) //gets filename
{
string fileName;
cout << "Enter the file name: ";
cin >> fileName;
input.open(fileName.c_str());
if(!input)
{
cout << "Incorrect File Path" << endl;
exit (0);
}
output.open("c:\\users\\jacob\\desktop\\thomannProj3Results.txt");
}
void countWords(ifstream& input) //counts words
{
bool notTrue = false;
string words;
int i = 0;
while(notTrue == false)
{
if(input >> words)
{
i++;
}
else if(!(input >> words))
notTrue = true;
}
cout << "There are " << i << " words in the file." << endl;
}
void countChars(ifstream& input, char storeCharacters[], ofstream& output) // counts characters
{
int i = 0;
while(input.good() && !input.eof())
{
input.get(storeCharacters[i]);
i++;
}
output << storeCharacters[0];
}
void sortChars() //sorts characters
{
}
void printCount() //prints characters
{
}
int main()
{
ifstream input;
ofstream output;
char storeCharacters[1000] = {0};
getFileName(input, output);
countWords(input);
countChars(input, storeCharacters, output);
return 0;
}
你如何分配空間對於這些參數? – jrok 2013-04-24 18:34:59
您可以通過使用'input.read'方法來消除該函數。 – 2013-04-24 19:50:17
嘗試使用while(input.good()&&!input.eof())作爲eof不僅是指示流不可讀的屬性。然而,「假設所有變量都被聲明...」:-) – 2013-04-24 20:55:27