I am writting a code that opens a file and gets the file name from a command line argument. I then open an output stream to print results of functions in a separate file. I have written a function and it isn't doing what I want it to. It is suppose to count the words in the file that was opened.
the word_count function only incruments once giving a word count of one and I can figure out why. Can anyone help?
this is my code.
[CODE=cpp]#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using std::cin;
using std::cout;
using std::cerr;
using std::ifstream;
using std::ofstream;
using std::setw;
using std::string;
void word_count(ifst ream&, ofstream&);
//void line_count(ifst ream&, ofstream&);
int main(int argc, char* argv[])
{
//if command line doesnt have 2 arguments output error message.
if(argc < 2)
cout << "error need more arguments.";
while(argc > 1)
{
//output each command line argument.
for(int idx = 1; idx < argc; ++idx)
{
//put file name in a c string.
string arg(argv[idx]);
// Define object for input
ifstream in(arg.c_str()) ;
if(!in)
{
// couldn't open input file, exit
cerr << "Error: couldn't open "
<< arg
<< " exiting\n";
exit(1);
}
// create name of output file
string out_file_name = arg + ".COPY";
// Define object for output
ofstream out(out_file_na me.c_str());
if(!out)
{
// couldn't open output file, exit
cerr << "Error: couldn't open "
<< out_file_name
<< " exiting\n";
exit(1);
}
const int WIDTH = 4;
int count = 1;
string input_string;
while (in >> input_string)
{
// one word per line
out << setw(WIDTH) << count << ": "
<< input_string << "\n";
++count;
}
word_count(in,o ut);
//line_count(in,o ut);
// Close files
in.close();
out.close();
return 0;
}}
}
//fuction definitions
void word_count(ifst ream& in, ofstream& out)
{
int count = 0;
while (in >> !eof())
{
++count;
}
out << "the word count is: " << count << "\n";
}
//void line_count(ifst ream& in, ofstream& out)
//{
// int count = 1;
// string input_string;
// while(in >> getline(input_s tring))
// {
//
// ++count;
// }
//
// out << "the line count is: " << count << "\n";
//
//}[/CODE]
the word_count function only incruments once giving a word count of one and I can figure out why. Can anyone help?
this is my code.
[CODE=cpp]#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using std::cin;
using std::cout;
using std::cerr;
using std::ifstream;
using std::ofstream;
using std::setw;
using std::string;
void word_count(ifst ream&, ofstream&);
//void line_count(ifst ream&, ofstream&);
int main(int argc, char* argv[])
{
//if command line doesnt have 2 arguments output error message.
if(argc < 2)
cout << "error need more arguments.";
while(argc > 1)
{
//output each command line argument.
for(int idx = 1; idx < argc; ++idx)
{
//put file name in a c string.
string arg(argv[idx]);
// Define object for input
ifstream in(arg.c_str()) ;
if(!in)
{
// couldn't open input file, exit
cerr << "Error: couldn't open "
<< arg
<< " exiting\n";
exit(1);
}
// create name of output file
string out_file_name = arg + ".COPY";
// Define object for output
ofstream out(out_file_na me.c_str());
if(!out)
{
// couldn't open output file, exit
cerr << "Error: couldn't open "
<< out_file_name
<< " exiting\n";
exit(1);
}
const int WIDTH = 4;
int count = 1;
string input_string;
while (in >> input_string)
{
// one word per line
out << setw(WIDTH) << count << ": "
<< input_string << "\n";
++count;
}
word_count(in,o ut);
//line_count(in,o ut);
// Close files
in.close();
out.close();
return 0;
}}
}
//fuction definitions
void word_count(ifst ream& in, ofstream& out)
{
int count = 0;
while (in >> !eof())
{
++count;
}
out << "the word count is: " << count << "\n";
}
//void line_count(ifst ream& in, ofstream& out)
//{
// int count = 1;
// string input_string;
// while(in >> getline(input_s tring))
// {
//
// ++count;
// }
//
// out << "the line count is: " << count << "\n";
//
//}[/CODE]
Comment