Peace How are you all?
Well, here is the problem: I have a file with two columns of numbers (attached one for reference). These two columns can be considered as x and f(x). I would like to have a whatever possible utility to tell me "what is the greatest value in the column f(x) and what is the corresponding value of x". A freind of mine gave me the attached c++ file. It is ok and works fine, but it only deals with one file and exits. I have about 3467 files of these given distributed over some directories and I would like this program to deal with ALL files in a folder and produces me a list of three columns as follows: file: f(x)max x at which f(x) is max.
I hope I could make it clear.
Max
PS. I could not attach files so here they are
first part of my file
480 5.4208E-08 500 1.1701E-07 520 1.9899E-07 540 2.9281E-07 560 3.9469E-07 580 4.9915E-07 600 6.0204E-07 620 7.0419E-07 640 8.0019E-07 660 8.9130E-07 680 9.7178E-07 700 1.0543E-06 720 1.1233E-06 740 1.1866E-06 . . . .
the code I have:
#ifdef HAVE_CONFIG_H #include <config.h> #endif #include <iostream> #include <cstdlib> #include <fstream.h> #include <stdlib.h> using namespace std; int main(int argc, char *argv[]) { cout << "Hello, world!" << endl; char file_name [30]; cout << "Enter file name:"; cin >> file_name; ifstream in; in.open (file_name); if(!in) cout<<"Cannot open file\n"; else { unsigned int col1;double col2; unsigned int max_col1;double max_col2; if(!in.eof()) { in>>col1>>col2; max_col1=col1; max_col2=col2; } else { cout<<"Empty file";exit(0); } while (!in.eof()) { in >> col1 >> col2; if (col2 > max_col2) {max_col1 = col1; max_col2 = col2;} } cout<<"\nMaximum value is:"<<max_col2<<" which corresponds to: "<<max_col1<<endl; } return EXIT_SUCCESS; }
Thanks
My format was completely
My format was completely destroyed. How may one attach files here?
Thanks
You can change your c++
calc_max is my assuption name to your c++ prgram, replace it with ur program name.
Peace, Thank you for the
Peace,
Thank you for the quick reply. I am afraid I am not as good as you. Please, in case it is ok with you, adjust your answer so I can use the code directly. Thanks again for your time.
Max
Please copy your c++ code
testing
How may I send it to
How may I send it to you?
the include statement did not appear it is: include iostream include cstdlib include fstream.h include stdlib.h
with the usual brackes used
thanks
got it
I got it. But in furthur post, put the code in a code tag
I hope it is clear to
I hope it is clear to you by the way, the list of bumber above can be made understood if you take 480, 500, 520 in a column, and those with E- in them as the f(x) values corresponding.
Thanks for yout time and help
Replace the following
Replace the following lines:
with:
After compiling lets say for a file named "calc_max", run the for loop mentioned above.
Here's the full code
Here's the full code:
run results
You are right, I'll see
You are right, I'll see where the problem.
Thanks for being willing to mention my name, this's an indication of your high ethics and open sourced mind :) but you don't have to do, it's a small effort. Consider it a present from me to you ;)
Okay, it's a small problem
Okay, it's a small problem in the for loop. It provides the calc_max program with the filename but not the full path, so it search for the filename in the current directory and didn't find it of course because it's locate some where else ( /home/mborn/gplot/ ).
1. Copy the calc_max program in each folder and just use:
2. Or use find command (instead of ls) as it'll provide the full path as follows:
This will create one results.out file for all files (even if it's under subdirectories) with the maximum values in each file.
Hi, Thanks for the reply. I
if I add the excutive code
ls don't show the current directory in its output but if the folder contain subdirectories, it'll list them and this will make the calc_max program not work in this situation only.
That may be a reason for u to use find over using ls.
Lets assume you're in /bin, you do ls /home/mborn/gplot/. This return file list in that directory, for example file1 file2 file3. Then calc_max run for each one, i.e. calc_max file1 but you r in /bin and it doesn't contain any file named file1. file1 is in /home/mborn/gplot/. so u need to pass the full path+the file name to calc_max program, i.e. calc_max /home/mborn/gplot/file1 and that's what find do, providing the full path+filename.
I wish it's clear now.
Thank you for the help,
#ifdef HAVE_CONFIG_H #include
#endif
#include
#include
#include
using namespace std;
struct DATA
{
float cross;
int energy;
};
DATA get_max(char file_name[])
{
ifstream in;
in.open (file_name);
if(!in)
cout<<"Cannot open file\n";
else
{
unsigned int col1;double col2;
unsigned int max_col1;double max_col2;
if(!in.eof())
{
in>>col1>>col2;
max_col1=col1;
max_col2=col2;
}
else
{
cout<<"Empty file";exit(0);
}
while (!in.eof())
{
in >> col1 >> col2;
if (col2 > max_col2)
{max_col1 = col1; max_col2 = col2;}
}
cout<> file_name;
ifstream in;
ofstream out;
cout << "Enter the list of out file name:";
cin >> out_file;
out.open(out_file);
in.open (file_name);
if(!in)
cout<<"Cannot open file\n";
else
{
char name[30];
DATA d1;
cout<<"File Name \t\tCross Section \tEnergy\n";
out<<"File Name \t\tCross Section \tEnergy\n";
while(!in.eof())
{
in.getline(name,29);
d1=get_max(name);
cout<>yes;
}while(yes=='y');
return EXIT_SUCCESS;
}
I hope it will appear correctly Max