473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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.
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
1 1519
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

1
by: rforman1 | last post by:
I have found many threads with this question asked and ostensibly answered, but none with an example of how exactly to do it. Help! I know that in order to set up the values of command line...
16
by: Kenneth McDonald | last post by:
For unfortunate reasons, I'm considering switching back to Win XP (from OS X) as my "main" system. Windows has so many annoyances that I can only compare it to driving in the Bay Area at rush hour...
5
by: JCauble | last post by:
1. We have a web project that has a support dll that uses the following command: HttpContext.Current.Application.ToString(); 2. In our environment we must use a build process that runs from the...
29
by: Tiraman | last post by:
Hi, I Build my own dll with my own namespace name and i would like to put it in one place but for the project bin folder so all of the projects will be able to use it . i tried to put the dll...
3
by: Jake Smythe | last post by:
Hello, I have some code that impersonates a user upon launching of the application. We now have the need to run some command line items. The impersonation doesn't seem to pass to the commands...
8
by: djc | last post by:
I'm new to this and was wondering what the options are for interpreting the command line using a CLI program. Specifically methods for interpreting the parameters passed to the program on the...
8
by: Andrew Robert | last post by:
Hi Everyone. I tried the following to get input into optionparser from either a file or command line. The code below detects the passed file argument and prints the file contents but the...
10
by: Jeff | last post by:
I guess that I should have explained better (I'm stil new with this, so I may not be asking the questions in the best way) Normally, one would use the simple code: if x>y then something end...
1
by: nancy | last post by:
I am new to PHP but have done other programming can someone please hold my hand and slowly talk me through some simple security issues? I have seen in PHP documents that there are 'strip...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.