C++ Help with file handle and simple statistics problem asking...


 
Thread Tools Search this Thread
Top Forums Programming C++ Help with file handle and simple statistics problem asking...
# 1  
Old 06-20-2011
C++ Help with file handle and simple statistics problem asking...

Input_file:
Code:
>header_1
ASDFFDASADASF
>header_2
ASDSAFASDAAFFFAFA

Desired output file:
Code:
30

Source Code try:
Code:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("Input_file");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file";

  return 0;
}

I only have idea to read the file.
Unfortunately, I don't know how to use the content inside input file to calculate its total number of content besides ">" Smilie
Apart from that, how should I do in order to let the program analysis any type of file instead of specific input file name at the source code?
eg.
Code:
program_name any_input_file_name output_file_name

program_name refers to the program that help to calculate the content in any type of input_file except ">";
any_input_file_name refer to the any input file name as long as its content is contain ">" and its respectively content;
output_file_name refer to the total character in the input_file exclude ">". In my case, output_file_name should shown "30"
Thanks for any advice.

Last edited by perl_beginner; 06-20-2011 at 06:36 AM..
# 2  
Old 06-20-2011
Code:
 
#include <iostream> 
using namespace std; 
int main(int argc, char* argv[]) { 
   cout << "argc = " << argc << endl; 
   for(int i = 0; i < argc; i++) 
      cout << "argv[" << i << "] = " << argv[i] << endl; 
   return 0; 
}

program_name any_input_file_name output_file_name
any_input_file_name = argv[0]
output_file_name = argv[1]


To count the characters, you can use find function to figure out the ">" and get the index and count it.

string::find - C++ Reference
# 3  
Old 06-20-2011
Hi itkamaraj,
Thanks for reply.
Sorry that if my question misleading you Smilie
Actually the following command:
Code:
program_name any_input_file_name output_file_name

program_name refers to the program that help to calculate the content in any type of input_file except ">";
any_input_file_name refer to the any input file name as long as its content is contain ">" and its respectively content;
output_file_name refer to the total character in the input_file exclude ">". In my case, output_file_name should shown "30"
Thanks again for your advice.

---------- Post updated at 08:33 PM ---------- Previous update was at 04:36 AM ----------

Does anybody have any idea about my problem ?
Thanks first.
# 4  
Old 07-06-2011
You might try to use fin, fout and argv..
Good luck..
All the best Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need optimized awk/perl/shell to give the statistics for the Large delimited file

I have a file size is around 24 G with 14 columns, delimiter with "|" My requirement- can anyone provide me the fastest and best to get the below results Number of records of the file First column and second Column- Unique counts Thanks for your time Karti ------ Post updated at... (3 Replies)
Discussion started by: kartikirans
3 Replies

2. Red Hat

CPU Usage statistics Dump in a text file over a period of time

I am facing issue related to performance of one customized application running on RHEL 5.9. The application stalls for some unknown reason that I need to track. For that I require some tool or shell scripts that can monitor the CPU usage statistics (what we get in TOP or in more detail by other... (6 Replies)
Discussion started by: Anjan Ganguly
6 Replies

3. Shell Programming and Scripting

Writing only timing statistics output of Timer to File

I'm running long integrations on a remote server, and I'm working in terminal in a tcsh shell. I'm looking to write ONLY the timing statistics to a file. For example: $time ls >timer.out writes both the files in my current directory & the timer statistics to the file timer.out. I only... (2 Replies)
Discussion started by: elemonier
2 Replies

4. Shell Programming and Scripting

problem in using cgi_script to handle form data

Hi, I have an HTML form through which I get some text as input. i need to run a shell script say script.sh inside a perl-cgi script named main_cgi.sh on the form input. I want to write the contents of the form in a file and then perform some command line operations like grep, cat on the text... (0 Replies)
Discussion started by: piyush_priya
0 Replies

5. Shell Programming and Scripting

Awk getting statistics of a grid file,

Hi , I have the following file which is basically a grid (has more than 100000 rows) LLL1 PPP1 LLL1 PPP2 LLL1 PPP3 ............... LLL1 5500 ..... LLL2 PPP1 LLL2 PPP2 LLL2 PPP3 ............... LLL1 5500 ..... L100 PPP1 L100 PPP2 L100 PPP3 ............... 2100 5500... (6 Replies)
Discussion started by: alex2005
6 Replies

6. Solaris

Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

I know how to check if any file has a unix process using a file by looking at 'lsof <fullpath/filename>' command. I think using lsof is very expensive. Also to make it accurate we need to inlcude fullpath of the file. Is there another command that can tell if a file has a truely active... (12 Replies)
Discussion started by: kchinnam
12 Replies

7. Shell Programming and Scripting

handle file by month

Hi, I want to write a shell script which handle an oracle alert log so that when it changes month, the script generates a new file with month in the filename. Thanks in advance. Leim (4 Replies)
Discussion started by: leim
4 Replies

8. UNIX for Dummies Questions & Answers

file statistics??

Is there any command in Unix (HP-UX) which will give me the file statistics .. e.g I have a file called r001 if I created that file on 2nd of aug 2005 and till now I changed that file contents 10 times. So how will I get the history statistic(time & date) of file modification. (1 Reply)
Discussion started by: zing_foru
1 Replies

9. Programming

deleting a file name by its handle

All, I am having three function 1.open 2.process 3.close. Open will open a file and process will process a file and close will close the file .. I can able to close the file by its filehandler.Is there is anyway that i can get the file name by filehandle and remove it after... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies
Login or Register to Ask a Question