question on command line code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

    question on command line code

    okay I had a code that asked user to input a file name and instead i changed it to take the input from the command line.

    I now want to output results of some functions to an output file. I have one function that is suppose to count the words in the file but it isnt working

    I have a test file with two lines in it.
    Code:
    abc def ghi gk
    
    ades
    and my program will output the file with the correct counter values but I have a lot of other junk in it can anyone help?

    this is my program code.....
    Code:
    #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(ifstream&, 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 = 0; 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_name.c_str());
        if(!out)
        {
                // couldn't open output file, exit
            cerr << "Error: couldn't open "
                 << out_file_name
                 << " exiting\n";
            exit(1);
        }
    
            // Output
        const int WIDTH = 4;
        int count = 1;
        string input_string;
        while (in >> input_string)
        {
    
    
          word_count(in, out);
    
               // one word per line
            out << setw(WIDTH) << count << ": "
                << input_string << "\n";
            ++count;
        }
    
            // Close files
        in.close();
        out.close();
        return 0;
    }}
    }
    
    //fuction definitions
    void word_count(ifstream&, ofstream&)
    {
        int counter = 0;
        while (!eof)
        {
            string words;
            cin >> words;
            ++counter;
         }
           cout << counter << "\n";
    }
    this is part of the output file produced it has many more lines

    Code:
       1: ^?ELF^A^A^A^@^@^@^@^@^@^@^@^@^B^@^C^@^A^@^@^@¼<8c>^D^H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^T^@^@^@^\^@^@^@
      10: ^@^@^@^Y^@^@^@^N^@^@^@^^^@^@^@^Z^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^^B^@^@^@^@^@^@¼^E^@^@^R^@^@^@e^C^@^@^@^@^@^@Ã^A^@^@^R^@^@^@H^@^@^@^@^@^@^@^U^@^@^@^R^@^@^@^NC^@^@ø§^D^H<8c>^@^@^@^Q^@^Y^@[^A^@^@^@^@^@^@^T^@^@^@^R^@^@^@^R^B^@^@^@^@^@^@R^@^@^@^@^@^@^X^C^@^@^@^@^@^@
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Find a better compiler. Your code won't compile using Visual Studio.NET 2005 because of:

    [code=cpp]
    void word_count(ifst ream&, ofstream&)
    {
    int counter = 0;
    while (!eof)
    {
    string words;
    cin >> words;
    ++counter;
    }
    cout << counter << "\n";
    }
    [/code]

    In the loop above, eof is not defined. Worse, the function has arguments but no names.

    Try fixing this and decide if you are using cin or your inout file.

    Comment

    Working...