Reputation: 1
Good evening everyone. I should start by saying that I'm completely new to programming and the C++ language.
I'm experiencing trouble opening a file to be able to use it in my program. I've read similar post and followed recommendations. I'm using ifstream to declare my input file and include the complete path to the file that I want to open. I've tried moving the file to other folders including within the workspace files, I have tried opening it with just the tittle, I have included the command "ios::in" after declaring my input file; no matter the changes that I have made I keep getting the same error message which I created:
Failed to open.
I've tried 3 different compilers, at the moment using CodeRunner. I do get the initial messages printed into the output file:
This program reads and computes the statistics for the words on a input file and creates an output file with the results. It will compute the total number of words, amount of words with different number of letters and the average quantity of letters per word.
Is there something I'm missing on my variable declarations? Am I missing any directives? Is it the command that I am using to open it? Thank you for your time and would appreciate any ideas or solutions.
// 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.
Upvotes: 0
Views: 622
Reputation: 77
You have wrong function call, it is used for function declaration:
void words_statistics(ifstream & fin, ofstream & fout);
But you have to call function like this:
words_statistics(fin, fout);
Upvotes: 0
Reputation: 206667
// Function call
void words_statistics(ifstream & fin, ofstream & fout);
That is not a function call. It is yet another declaration of the function. Use:
words_statistics(fin, fout);
Upvotes: 2