writing an a file with columns in an array

I've a file like this
C 1.009 0.987 8.987 89.098
H 2.898 3.087 6.876 5.87
H 3.456 2.345 2.67 8.876
S 2.734 3.45 3.123 5.67

I want to write each column into an array, I got a code from the article 'Inputing from file to parallel arrays'tried to modify it but cannot use, cannot read the output. I have it in a function so I can re-use it. The length of the column is mostly known, but varies from different files. Please can somebody help me.
My code is below:

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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;

double buildArrays(char ar1[1], double ar2[1], double ar3[1], double ar4[1] )
{

  ifstream infile;
  //Open the input file. If it fails to open, display an error message and
  //end the program
  infile.open( "data.txt" );
  if( infile.fail() )
     {
         cout << "data.txt failed to open";
         exit( -1 );
     }
  int numElements = 0;
while (!infile.eof()){
        infile >> ar1[numElements];
        infile >> ar2[numElements];
        infile >> ar3[numElements];
        infile >> ar4[numElements];

        numElements++;
    }

  infile.close();
        return char ar1, double  ar2, double ar3, double ar4;
}


int main(){
     double buildArrays(char Element, double x, double y, double z);

     for(int i=0;i<numElements;i++){ 
     cout<<Element[i]<<'\t'<< x[i] <<'\t'<< y[i] <<'\t'<<z[i]<<endl;
};
     return 0;
}


The code does not run, it doesn't do what I want it to do

Last edited on
i dont think you can return those many elements ... i thought a function always returns one value ... and that is supposed to match with the return type mentioned before the function name (ex. double in line 8).

you might want to pass pointers as your arguments so you wont have to return anything
Acpaluri, Can u please help modify the code as my knowledge of c++ is limited. Am messing it all trying to use pointers.
Honestly if you can't use pointers yet I think this problem is a bit beyond you KingJosh. I suggest reading the tutorial on this site. Also why don't you post what you have so far.

You are right acpaluri, functions can return either zero values (void), or one value.
this is what I have so far

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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;

double buildArrays(char *ar1[1], double *ar2[1], double *ar3[1], double *ar4[1] )
{

  ifstream infile;
  //Open the input file. If it fails to open, display an error message and
  //end the program
  infile.open( "data.txt" );
  if( infile.fail() )
     {
         cout << "data.txt failed to open";
         exit( -1 );
     }
  int numElements = 0;
while (!infile.eof()){
        infile >> ar1[numElements];
        infile >> ar2[numElements];
        infile >> ar3[numElements];
        infile >> ar4[numElements];

        numElements++;
    }

  infile.close();
}


int main(){
         double buildArrays(char Element, double x, double y, double z); 
         double buildArrays(char Element, double x, double y, double z);
         for(int i=0;i<numElements;i++){
         cout<<Element[i]<<'\t'<< x[i] <<'\t'<< y[i] <<'\t'<<z[i]<<endl;
         };
}



and the error message is
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/istream:207: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = 
char, _Traits = std::char_traits<char>]
Last edited on
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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;

void buildArrays(char *ar1, double *ar2, double *ar3, double *ar4, double *ar5)	{
	ifstream infile;

	//Open the input file. If it fails to open, display an error message and
	//end the program
	infile.open("data.txt");

	if( infile.fail())	{
		cout << "data.txt failed to open";
		exit(0);
	}

	int numElements = 0;

	while (!infile.eof()) {
		infile >> ar1[numElements];
		infile >> ar2[numElements];
		infile >> ar3[numElements];
		infile >> ar4[numElements];
		infile >> ar5[numElements];
		numElements++;
	}

	infile.close();
}


int main() {

int num_elements = 4;
	// you can also read num of elements from the file

	char array1[num_elements];
	double array2[num_elements];
	double array3[num_elements];
	double array4[num_elements];
	double array5[num_elements];

	buildArrays (array1, array2, array3, array4, array5); 

	for(int i = 0; i < num_elements; i++)  {
		cout << array1[i] << '\t' << array2[i] << '\t' << array3[i] << '\t' << array4[i] << '\t' << array5[i] << endl;
	}
	
	return 0;
}


i hope its not home work !! :)
Thanks for the great lessons. It's not a home work. I want to do this so, I can use it to write some code to help me with my research. I use matlab but think I should change my codes to c++ so i can share them for free. I will like to do it myself so i can learn in the process. This part seems to be the most difficult, have been battling with it for weeks but felt I should share the problem. Thanks to all who have and are still contributing to it. Special thanks to Acapaluri.

1
2
3
The code compiled but does not display output when I run.

Also, the num_elements I will like to read from the file.
Last edited on
please note that in the code that you posted you are declaring 4 arrays while there are five columns in the data file ... and the use of eof in a while loop (from what i know) is not a good practice ... i have modified the code above ... also you need to take care that there are no extra lines or white spaces at the end in your input files.

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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>

using namespace std;

ifstream infile;

void buildArrays(char *ar1, double *ar2, double *ar3, double *ar4, double *ar5)	{

	int numElements = 0;

	while ( infile >> ar1[numElements] >> ar2[numElements] >> ar3[numElements] >> ar4[numElements] >> ar5[numElements] ) {
		numElements++;
	}

	infile.close();
}


int main() {

	//Open the input file. If it fails to open, display an error message and
	//end the program
	infile.open("data.txt");

	if( infile.fail())	{
		cout << "data.txt failed to open";
		exit(0);
	}
	
	int num_elements = 0;
	// you can also read num of elements from the file
	
	string temp;
	while (getline(infile,temp)) {
		num_elements++;
	}
	
	// remove the eof error flag and set the file pointer to the beginning of the file
	infile.clear();
	infile.seekg(0, ios::beg);
	
	char array1[num_elements];
	double array2[num_elements];
	double array3[num_elements];
	double array4[num_elements];
	double array5[num_elements];

	buildArrays (array1, array2, array3, array4, array5); 

	for(int i = 0; i < num_elements; i++)  {
		cout << array1[i] << '\t' << array2[i] << '\t' << array3[i] << '\t' << array4[i] << '\t' << array5[i] << endl;
	}
	
	return 0;
}


the code seems to compile correctly and produce the correct output

cheers


Thanks a million!!! It works well!!!
glad to help ... please mark the post solved if done :)
Topic archived. No new replies allowed.