By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,382 Members | 1,567 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,382 IT Pros & Developers. It's quick & easy.

question on command line code

P: 57
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.
Expand|Select|Wrap|Line Numbers
  1. abc def ghi gk
  2.  
  3. 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.....
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string>
  5. using std::cin;
  6. using std::cout;
  7. using std::cerr;
  8. using std::ifstream;
  9. using std::ofstream;
  10. using std::setw;
  11. using std::string;
  12.  
  13.  
  14. void word_count(ifstream&, ofstream&);
  15.  
  16.  
  17. int main(int argc, char* argv[])
  18. {
  19. //if command line doesnt have 2 arguments output error message.
  20.     if(argc < 2)
  21.     cout << "error need more arguments.";
  22.  
  23.  
  24.  
  25. while(argc > 1)
  26. {
  27.  
  28. //output each command line argument.
  29. for(int idx = 0; idx < argc; ++idx)
  30. {
  31.     //put file name in a c string.
  32.     string arg(argv[idx]);
  33.  
  34.  
  35.  
  36.         // Define object for input
  37.     ifstream in(arg.c_str());
  38.     if(!in)
  39.     {
  40.             // couldn't open input file, exit
  41.         cerr << "Error: couldn't open "
  42.              << arg
  43.              << " exiting\n";
  44.         exit(1);
  45.     }
  46.  
  47.         // create name of output file
  48.     string out_file_name = arg + ".COPY";
  49.  
  50.         // Define object for output
  51.     ofstream out(out_file_name.c_str());
  52.     if(!out)
  53.     {
  54.             // couldn't open output file, exit
  55.         cerr << "Error: couldn't open "
  56.              << out_file_name
  57.              << " exiting\n";
  58.         exit(1);
  59.     }
  60.  
  61.         // Output
  62.     const int WIDTH = 4;
  63.     int count = 1;
  64.     string input_string;
  65.     while (in >> input_string)
  66.     {
  67.  
  68.  
  69.       word_count(in, out);
  70.  
  71.            // one word per line
  72.         out << setw(WIDTH) << count << ": "
  73.             << input_string << "\n";
  74.         ++count;
  75.     }
  76.  
  77.         // Close files
  78.     in.close();
  79.     out.close();
  80.     return 0;
  81. }}
  82. }
  83.  
  84. //fuction definitions
  85. void word_count(ifstream&, ofstream&)
  86. {
  87.     int counter = 0;
  88.     while (!eof)
  89.     {
  90.         string words;
  91.         cin >> words;
  92.         ++counter;
  93.      }
  94.        cout << counter << "\n";
  95. }
  96.  
this is part of the output file produced it has many more lines

Expand|Select|Wrap|Line Numbers
  1.    1: ^?ELF^A^A^A^@^@^@^@^@^@^@^@^@^B^@^C^@^A^@^@^@¼<8c>^D^H4^@^@^@^@^Z^@^@^@^@^@^@4@^
  2.    2: ^@^H^@(^@^^^@^[^@^F^@^@^@4^@^@^@4<80>^D^H4<80>^D^H^@^A^@^@^@^A^@^@^E^@^@^@^D^@^@^@^C^@^@^@4^A^@^@4<81>^D^H4<81>^D^H^S^@^@^@^S^@^@^@^D^@^@^@^A^@^@^@^A^@^@^@^@^@^@^@^@<80>^D^H^@<80>^D^Hk^V^@^@k^V^@^@^E^@^@^@^@^P^@^@^A^@^@^@l^V^@^@l¦^D^Hl¦^D^H<8c>^A^@°^B^@^@^F^@^@^@^@^P^@^@^B^@^@^@<84>^V^@^@<84>¦^D^H<84>¦^D^H ^@^@^@ ^@^@^@^F^@^@@^@^@^D^@^@^@H^A^@^@H<81>^D^HH<81>^D^H
  3.    3: ^@^@^@
  4.    4: ^@^@^@^D^@^@^@^D^@^@^@Påtd¸^T^@^@¸<94>^D^H¸<94>^D^H<^@^@^@<^@^@^@^D^@^@^@^D^@Qåtd^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^F^@^@^@^D^@^@^@/lib/ld-linux.so.2^@^@D^@^@^@^P^@^@^@^A^@^@^@GNU^@^@^@^@^@^B^@^@^@^B^@^@^@^E^@^@^@^Q^@^@^@$^@^@^@^R^@^@^@^X^@^@^@
  5.    5: ^@^@^@^_^@^@^@
  6.    6: ^@^@^@^@^@^@^@^]^@^@^@^V^@^@^@^[^@^@^@^U^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@"^@^@^@
  7.    7: ^@^@^@#^@^@^@!^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^@^@^@^@^@^@^F^@^@^@^@^@^@^@
  8.    8: ^@^@^@
  9.    9: ^@^@^@^@^@^@^@^H^@^@^@^@^@^@^@^@^@^@^@^E^@^@^@^D^@^@^@^@^@^@^@^@^@^@^@^Q^@^@^@^P^@^@^@^@^@^@^@^W^@^@^@^G^@^@^@^S^@^@^@^O^@^@^@^@^@^@^@^T^@^@^@^\^@^@^@
  10.   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^@^@^@^@^@^@
May 26 '07 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Find a better compiler. Your code won't compile using Visual Studio.NET 2005 because of:

Expand|Select|Wrap|Line Numbers
  1. void word_count(ifstream&, ofstream&)
  2. {
  3.     int counter = 0;
  4.     while (!eof)
  5.     {
  6.         string words;
  7.         cin >> words;
  8.         ++counter;
  9.      }
  10.        cout << counter << "\n";
  11. }
  12.  
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.
May 27 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.