data reading from a text file question

Hi all,

I am trying to read data into a file, but I get very unexpected results. I need to play around with the data and do a polynomial interpolation later, so I include a bunch of extra libraries. My code:

#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <fstream>
#include <limits>
#include <sstream>
#include <iomanip>

//using the standard namespace
using namespace std;
int main()
{
//variable definitions
ofstream outClientFile;
ifstream inClientFile;
string fileName;
string inputname;
string stuff;
string stuff1;

int kpt=0;

//prompt input for finding file name
cout << "Enter the file name to be interpolated, without file extension, extensions are handled internally : ";
cin >>inputname;
cout <<endl;

stuff=string(inputname +".energy");
inClientFile.open(stuff.c_str());
//check if the file opened
if(inClientFile.is_open())
{
cout <<"input file opened sucessfully"<<endl;
cout <<endl;
}
else
{
cout <<"Error opening input file"<<endl;
cout <<endl;
finaloutput();
return 0;
}
cout << "Enter the file name to be outputted, just the name without the extension, file extensions are handled internally: ";
cin >> fileName;
cout <<endl;
stuff1 =string(fileName + ".energy");
outClientFile.open(stuff1.c_str());

//error check if files opended sucessfully
if(outClientFile.is_open())
{
cout<<"output file opened sucessfully"<<endl;
cout<<endl;
}
else
{
cout<<"output file could not be created"<<endl;
finaloutput();
return 0;
}

//varible definitions for data input
int kptcount=0;
int ecount =0;
int linecount=0;
string stuff2;
int line=0;
int totalline=0;

//reading the file here, the problem is probably after this line

getline(inClientFile,stuff2);
inClientFile>>kptcount;
cout<<"Reading "<<kptcount<<" k-points from the input file and a total of ";
getline(inClientFile,stuff2);

double x1read[kptcount];
double x2read[kptcount];
double x3read[kptcount];

inClientFile>>x1read[1]>>x2read[1]>>x3read[1]>>line;
getline(inClientFile,stuff2);
for(int i=2;i<kptcount;i++)
{
x1read[i]=0;
x2read[i]=0;
x3read[i]=0;
}
linecount=line*kptcount+kptcount;
totalline=line*kptcount+kptcount+2;
cout<<linecount+2<<" lines of data"<<endl<<endl;
double energydata[kptcount][line];
for(int n=1;n<line+1;n++)
{
inClientFile>>energydata[1][n];
}
for(int l=2;l<kptcount+1;l++)
{
inClientFile>>x1read[l];
inClientFile>>x2read[l];
inClientFile>>x3read[l];
getline(inClientFile,stuff2);
for(int n=1;n<line+1;n++)
{
inClientFile>>energydata[l][n];
}
}

//testing output
for(int i=1;i<kptcount+1;i++)
{
cout<<i<<" "<<x1read[i]<<" "<<x2read[i]<<" "<<x3read[i]<<" ";
cout<<energydata[i][1]<<" "<<energydata[i][2]<<endl;
}
return 0;
}

and the sample input file is:
unimportant stuff
7 unimportant stuff
0 0 0 2 unimportant stuff1
-0.1
0.1
0 0 0.005 2 unimportant stuff2
-0.099
0.099
0 0 0.01 2 unimportant stuff3
-0.098
0.098
0 0 0.015 2 unimportant stuff4
-0.097
0.097
0 0 0.02 2 unimportant stuff
-0.096
0.096
0 0 0.025 2 unimportant stuff
-0.095
0.095
0 0 0.03 2 unimportant stuff
-0.094
0.094

I get an output of the form using this code:
1 0 0 -.094 -0.1 0.1
2 0 0 0.094 -0.099 0.099
3 0 0 0.01 -0.098 0.098
4 0 0 0.015 -0.097 0.097
5 0 0 0.02 -0.096 0.096
6 0 0 0.025 -0.095 0.095
7 0 0 0.03 -0.094 0.094

as opposed to what I actually want to see:
1 0 0 0 -0.1 0.1
2 0 0 0.005 -0.099 0.099
3 0 0 0.01 -0.098 0.098
4 0 0 0.015 -0.097 0.097
5 0 0 0.02 -0.096 0.096
6 0 0 0.025 -0.095 0.095
7 0 0 0.03 -0.094 0.094

I am a little confused, I probably wrote the logic conditions for reading the lines incorrectly, but I do not see the solution to the problem. Any help would be greatly appreciated
Chris

Last edited on
Found the error, there is a memory spacing fault with the g++ compiler working on my laptop. the new routine that works is:
double x1read[kptcount];
double x2read[kptcount];
double x3read[kptcount];
double memoryspacer[kptcount]; //fault with g++ compiler requiring memory spacing
//on my computer

inClientFile>>x1read[1]>>x2read[1]>>x3read[1]>>line;
getline(inClientFile,stuff2);
linecount=line*kptcount+kptcount;
totalline=line*kptcount+kptcount+2;
cout<<linecount+2<<" lines of data"<<endl<<endl;
double energydata[kptcount][line];

for(int a=1;a<kptcount;a++)
{
for(int n=1;n<line+1;n++)
{
inClientFile>>energydata[a][n];
}
inClientFile>>x1read[a+1];
inClientFile>>x2read[a+1];
inClientFile>>x3read[a+1];
getline(inClientFile,stuff2);
}
for(int f=1;f<line+1;f++)
{
inClientFile>>energydata[kptcount][f];
}
Please post a small complete sample of your input file.

And note this program doesn't compile with my compiler:

main.cpp||In function ‘int main()’:|
main.cpp|41|error: ‘finaloutput’ was not declared in this scope|
main.cpp|59|error: ‘finaloutput’ was not declared in this scope|
main.cpp|78|error: ISO C++ forbids variable length array ‘x1read’ [-Wvla]|
main.cpp|79|error: ISO C++ forbids variable length array ‘x2read’ [-Wvla]|
main.cpp|80|error: ISO C++ forbids variable length array ‘x3read’ [-Wvla]|
main.cpp|93|error: ISO C++ forbids variable length array ‘energydata’ [-Wvla]|
main.cpp|93|error: ISO C++ forbids variable length array ‘energydata’ [-Wvla]|
main.cpp|22|warning: unused variable ‘kpt’ [-Wunused-variable]|
main.cpp|65|warning: unused variable ‘ecount’ [-Wunused-variable]|
||=== Build finished: 7 errors, 2 warnings (0 minutes, 1 seconds) ===|



Hi, here is the program again edited to remove the errors you found, it compiled without errors with g++ on my windows 8 laptop, is that strange it would not work on your computer?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <fstream>
#include <limits>
#include <sstream>
#include <iomanip>

//using the standard namespace
using namespace std;
int main()
{
    //variable definitions
    ofstream outClientFile;
    ifstream inClientFile;
    string fileName;
    string inputname;
    string stuff;
    string stuff1;
    
    //prompt input for finding file name
    cout << "Enter the file name to be interpolated";
    cin >>inputname;
    cout <<endl;
    
    stuff=string(inputname +".energy");
    inClientFile.open(stuff.c_str());
    //check if the file opened
    if(inClientFile.is_open())
    {
	cout <<"input file opened sucessfully"<<endl;
	cout <<endl;
    }
    else
    {
	cout <<"Error opening input file"<<endl;
	cout <<endl;
	return 0;
    }
    cout << "Enter the file name to be outputted: ";
    cin >> fileName;
    cout <<endl;
    stuff1 =string(fileName + ".energy");
    outClientFile.open(stuff1.c_str());

    //error check if files opended sucessfully
    if(outClientFile.is_open())
    {
	cout<<"output file opened sucessfully"<<endl;
	cout<<endl;
    }
    else
    {
	cout<<"output file could not be created"<<endl;
	return 0;
    }

    //varible definitions for data input
    int kptcount=0;
    int linecount=0;
    string stuff2;
    int line=0;
    int totalline=0;

    getline(inClientFile,stuff2);
    inClientFile>>kptcount;
    cout<<"Reading "<<kptcount<<" k-points from the input file and a total of ";
    getline(inClientFile,stuff2);

    double x1read[500];
    double x2read[500];
    double x3read[500];
    double memoryspacer[500]; //fault with g++ compiler requiring memory spacing
                                   //on my computer

    inClientFile>>x1read[1]>>x2read[1]>>x3read[1]>>line;
    getline(inClientFile,stuff2);
    linecount=line*kptcount+kptcount;
    totalline=line*kptcount+kptcount+2;
    cout<<linecount+2<<" lines of data"<<endl<<endl;
    double energydata[500][200];
    
    for(int a=1;a<kptcount;a++)
    {
	for(int n=1;n<line+1;n++)
	{
	    inClientFile>>energydata[a][n];
	}
	inClientFile>>x1read[a+1];
	inClientFile>>x2read[a+1];
	inClientFile>>x3read[a+1];
	getline(inClientFile,stuff2);
    }

    for(int f=1;f<line+1;f++)
    {
	inClientFile>>energydata[kptcount][f];
    }

    for(int i=1;i<kptcount+1;i++)
    {
	cout<<i<<" "<<x1read[i]<<" "<<x2read[i]<<" "<<x3read[i]<<" ";
	cout<<energydata[i][1]<<" "<<energydata[i][2]<<endl;
    }

      inClientFile.close();
    outClientFile.close();
    return 0;
}

Last edited on
The input file looks something like:

Eigen-energies file
    3     1    0.0000000000E+00     ! nk, nspin, Fermi level(Ry) : energies below in Ry
    0.0000000000E+00    0.0000000000E+00    0.0000000000E+00    10    ! kpt nband
   -0.1026061174E+01
   -0.9734346823E+00
   -0.9734310100E+00
   -0.9287116551E+00
   -0.9274085175E+00
   -0.9159473161E+00
   -0.9159463609E+00
   -0.8982511599E+00
   -0.8810543372E+00
   -0.8810505815E+00
     0.0000000000E+00    0.0000000000E+00    0.2083333333E-02    192    ! kpt nband
   -0.1026061078E+01
   -0.9734349450E+00
   -0.9734311113E+00
   -0.9288079137E+00
   -0.9273119227E+00
   -0.9159473546E+00
   -0.9159468212E+00
   -0.8982514410E+00
   -0.8812643036E+00
   -0.8812609619E+00
      0.0000000000E+00    0.0000000000E+00    0.4166666667E-02    192    ! kpt nband
   -0.1026059817E+01
   -0.9734339102E+00
   -0.9734305469E+00
   -0.9290419907E+00
   -0.9270778117E+00
   -0.9159474681E+00
   -0.9159472513E+00
   -0.8982519312E+00
   -0.8815769464E+00
   -0.8815744072E+00
With the above input, what do you expect for your output? Is "Eigen-energies file" par of the actual input file?

The "Eigeg-energies file" is included in the first line of the input file. I get the expected output from my file reader:

1 0 0 0 -0.1026E+01 -0.9734E+00
2 0 0 0.2083E-02 -0.1026E+01 -0.9734E+00
3 0 0 0.4167E-02 -0.1026E+01 -0.9734E+00


I think I have also figured out the trickiest part my program as well, dealing with degenerate crossing points in the interpolation. Is there a faster or more efficient way to do what I want?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
	double energytemp1=0;
	double energytemp2=0;
	double energytemp3=0;
	double deriv2temp1=0;
	double deriv2temp2=0;
	double deriv2temp3=0;
	double deriv2temp4=0;
	double deriv2temp5=0;
	double deruv2temp6=0;
	double highderiv=0;
	int storedindex=0;
	int crossingcount1=0;
	int crossingcount2=0;
	int crossingcount3=0;
	int crossingcount4=0;
	int crossingcount5=0;
	int corssingcount6=0;
	for(int z=1;z<(kptcount-2);z++)
	{
	    for(int a=1;a<line+1;a++)
	    {
		energytemp1=energydata[z+2][a]+(energydataderiv1[z+1][a]+energydataderiv1[z][a])/2;
		for(int b=a+1;b<(line+1);b++)
		{
		    energytemp2=energydata[z+2][b]+(energydataderiv1[z+1][b]+energydataderiv1[z][b])/2;
		    //condition 1
		    if(energytemp1>energytemp2)
		    {
			cout<<"Crossing condition 1 met between k points "<<z+2<<" and "<<z+3<<" for lines "<<a<<" and "<<b<<endl;
		  	crossingcount1++;
		  	deriv2temp1=abs(energydataderiv2[z+1][a]);
		  	deriv2temp2=abs(((energydataderiv2[z][a]+energydataderiv2[z-1][a])/2)*5);
			//condition 2
		    	if(deriv2temp1>deriv2temp2)
		    	{
		   	    cout<<"Crossing condition 2 met betweem k points "<<z+2<<" and "<<z+3<<" for lines "<<a<<" and "<<b<<endl;
		   	    crossingcount2++;
			    //checking to see if there is a change in the second derivative at that point
			    
			    //checking to see if there are any other crossings at the same point
			    for(int c=b+1;c<(line+1);c++)
			    {
				energytemp3=energydata[z+2][c]+(energydataderiv1[z+1][c]+energydataderiv1[z][c])/2;
				//condition 4
				if(energytemp1>energytemp3)
				{
				    crossingcount4++;
				    cout<<"Crossing condition 3 met between k points "<<z+2<<" and "<<z+3<<"for lines "<<a<<" "<<b<<" "<<c<<endl;
				    //checking for higher order crossings
				    for(int d=c+1;d<(line+1);d++)
				    {
					energytemp4=energydata[z+2][d]+(energydataderiv1[z+1][d]+energydataderiv1[z][d])/2;
					if(energytemp1>energytemp4)
                                       {
                                               crossingcount5++;
                                               //sort out the degeracy here.....etc

				    }
				}
			    }
			    
		    	}
		     }
		}
	    }
	}
Topic archived. No new replies allowed.