Skip to Content

correcting a code

mborn's picture

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

mborn's picture

My format was completely

My format was completely destroyed. How may one attach files here?

Thanks

ikhnaton2's picture

You can change your c++

for i in `ls`
do
calc_max $i >> output_file.txt
done

calc_max is my assuption name to your c++ prgram, replace it with ur program name.


mborn's picture

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

ikhnaton2's picture

Please copy your c++ code


mborn's picture

testing

{{{ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include 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:"<
mborn's picture

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

ikhnaton2's picture

got it

I got it. But in furthur post, put the code in a code tag


mborn's picture

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

ikhnaton2's picture

Replace the following

Replace the following lines:

cout << "Hello, world!" << endl;
char file_name [30];
cout << "Enter file name:";
cin >> file_name;

with:

char *file_name;
file_name=argv[1];

After compiling lets say for a file named "calc_max", run the for loop mentioned above.


ikhnaton2's picture

Here's the full code

Here's the full code:

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[]) {
  char *file_name;
  unsigned int col1;
  double col2;
  unsigned int max_col1;
  double max_col2;

  file_name=argv[1];
  ifstream in;
  in.open (file_name);
  if(!in)
    cout<<"Cannot open file\n";
  else {
    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;
}

mborn's picture

run results

Dear: Ikhnaton, thanks for the help. I did the following: 1- I made a script file with the for loop you mentioned, but I used for i in `ls /home/mborn/gplot/` do calc_max $i >> input_file done where calc_max is the name of the C++ code you kindly provided. the resulting file contained lines telling me it could not open the file, and gave me numbers for both f(x) and x which are the limits of the typr of variables we used in our code. If we can make everything in the code, so I can run the code from inside the folder which contains these files, that will be OK! This case I shall copy the code in each folder I have and get the results. What do you think? Max PS. In case it works, I am obliged to mention your name in the published work, in my aknowlegment, and so, I shall need your full name and occupation! :)
ikhnaton2's picture

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 ;)


ikhnaton2's picture

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:

for i in `ls`
do
calc_max $i >> results.out
done

2. Or use find command (instead of ls) as it'll provide the full path as follows:

for i in `find ./home/mborn/gplot/ -type f`
do
calc_max $i >> results.out
done

This will create one results.out file for all files (even if it's under subdirectories) with the maximum values in each file.


mborn's picture

Hi, Thanks for the reply. I

Hi, Thanks for the reply. I am on a windows machine now, so I shall ask this question only for reference, if I add the excutive code in each older, then "ls" will show it in its output, and this will cause it to go to itself as a possible input (correct me if I am wrong) and this will cause the whole thing to collapse.....am I right? Also, in the previous for loop, as I said, I have used `ls /home/mborn/gplot/` so it should have read the files correctly...why was the problem? Thanks yours, Max
ikhnaton2's picture

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.


mborn's picture

Thank you for the help,

Thank you for the help, I could manage with some help from a freind to have teh following code, which I shall put between #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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.