大家晚上好。 我首先要说的是,我对编程和C ++语言都是全新的。
我在打开文件时遇到问题,无法在我的程序中使用它。我已经阅读了类似的帖子并遵循了建议。我使用ifstream来声明我的输入文件,并包含我想要打开的文件的完整路径。我已经尝试将文件移动到其他文件夹,包括工作区文件中,我尝试只用tittle打开它,我已经包含了命令" ios :: in"声明我的输入文件后;无论我做了哪些更改,我都会收到与我创建的相同的错误消息:
无法打开。
我目前使用CodeRunner尝试了3种不同的编译器。我确实将初始消息打印到输出文件中:
该程序读取并计算输入文件中单词的统计信息 并创建一个包含结果的输出文件。 它将计算单词总数,不同数字的单词数量 字母和每个单词的平均字母数量。
我的变量声明中是否缺少某些内容? 我错过了任何指令吗? 这是我用来打开它的命令吗? 感谢您的时间,并感谢任何想法或解决方案。
// Directives
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <fstream>
#include <string>
using namespace std;
void words_statistics(ifstream & fin, ofstream & fout);
// Opens a file, reads it, computes statistics of for the words on the file.
int main (){
// Variables declaration
ifstream fin; //Variable type for Input files.
ofstream fout; // Variable type for output files.
string inFile, outFile;
fout << "This program reads and computes the statistics for the words on a input file \n";
fout << "and creates an output file with the results.\n";
fout << "It will compute the total number of words, amount of words with different number \n";
fout << "of letters and the average quantity of letters per word.\n";
// Open input file to read-in
fin.open("/Macintosh HD/Users/antonydelacruz/Downloads/words.txt");
if(fin.fail()) // Generate Error message input file.
{
cout << inFile << " Failed to open."<< endl;
exit (1);
}
fout.open("/Macintosh HD/Users/antonydelacruz/Downloads/words_statistics.txt");
if(fout.fail()) // Error message in case that the program can't access output file.
{
cout << inFile << " Failed to open file Words Statistics."<< endl;
exit (1);
}
// Function call
words_statistics(fin, fout);
fin.close(); // Close input File.
fout.close(); // Close output file.
return 0;
}
// Function Definition
void words_statistics(ifstream & fin, ofstream & fout)
{
// Variable Declaration
std::string inFile, outFile;
int lettersQuantity=0; //Variable to accumulate the amount of letters per word.
int totalWords=0; // Variable to accumulate the total amount of Words.
double avg=0;
int un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix, onze, douze, treize, otre; // Variables to accummulate words depending on the amount of letters that they have.
un = deux = trois = quatre = cinq = six = sept = huit = neuf = dix = onze = douze = treize = otre=0;
while (!fin.eof()) { //Specifies to noly use the switch feature while there is data to read.
(fin >> inFile); // Extracts data from file.
lettersQuantity++; // Adds the amount of letters per word.
totalWords++; // Adds the total amount of words
switch (lettersQuantity){ //Evaluates each word and adds it to a category depending on how many letters the word has.
答案 0 :(得分:2)
// Function call
void words_statistics(ifstream & fin, ofstream & fout);
这不是函数调用。这是该功能的另一个宣言。使用:
words_statistics(fin, fout);
答案 1 :(得分:0)
您的函数调用错误,它用于函数声明:
void words_statistics(ifstream & fin, ofstream & fout);
但是你必须调用这样的函数:
words_statistics(fin, fout);