Pulling in data from .CSV file

I'm having an issue were I'm trying pulling in data from the .CSV file into the xCol[], yCol[], etc.... Arrays and for some reason they wont be read. The number of lines works fine, so i know the file is being opened. Here is the data format for the csv file (there are 5 columns).

6195152 1897096 33 680144.3125 5.832601
6195152 1897096 32 659309.625 5.819089
6195152 1897096 31 673557.8125 5.828375
6195152 1897096 30 680511.0625 5.832835
6195152 1897096 29 612108 5.786828
6195152 1897096 28 598957.75 5.777396
6195152 1897096 27 613724.4375 5.787973
6195152 1897096 26 627397.1875 5.797543


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
#include <iostream>
#include <ostream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	double xCol[10000];
	double yCol[10000];
	double zCol[10000];
	double dataCol[10000];
	double unCol[10000];
	ifstream inFile; 
	ofstream outFile;
	ofstream tableFile;
	int num = 0;
	int num2 = 0;
	int num3 = -1;
	int num4 = 0;
	int num5 = -1;
	int injPoint = 0;
	int injPoint2 = 0;
	double totalCord = 0;
	double topBoring;
	double detValue;
	double dataColMax = 0;

	
	
	cout << "What is the top of the boring: ";
	cin >> topBoring; 

	cout << "What is the detection value: ";
	cin >> detValue;
	inFile.open("C:\\Users\\Frank\\Desktop\\Data To Injection\\ecd data.csv");
	if(inFile.fail())
	{
		cout << "Infile Failed";
	}
	

	int number_of_lines = 0;
    std::string line;
    while (std::getline(inFile, line))
        ++number_of_lines;
    std::cout << "Number of lines in text file: " << number_of_lines;
	
	for(int f = 0; f<number_of_lines; f++)
	{
		
		inFile >> xCol[f];
		inFile >> yCol[f];
		inFile >> zCol[f];
		inFile >> dataCol[f];
		inFile >> unCol[f];
		
	}
	inFile.close();
	
	outFile.open("C:\\Users\\Frank\\Desktop\\Data To Injection\\ECD Output.txt");
	if(outFile.fail())
	{
		cout << "the file failed to open";
	}

	outFile << "x\t";
	outFile << "y\t";
	outFile << "Elevation\t";
	outFile << "@@Detector_Response\t";
	outFile << "Boring\t";
	outFile << "Top\n";
	outFile << "Elevation\t";
	outFile << "feet\n";
	outFile << "=count(A4:A10000)\t";
	outFile << "1\t";
	outFile << "Screen\n";
	for (int i=0; num>i;++i)
	{
		if(dataCol[num2]>=detValue)
		{
			outFile << fixed << setprecision(2) << xCol[num2] << "\t" << yCol[num2] << "\t" << zCol[num2] <<"\t" << dataCol[num2] << "\t";
		
			if((xCol[num3] + yCol[num3]) == (xCol[num2] + yCol[num2]))
			{
				outFile << "Inj-" << injPoint;
			}
			else
			{
				outFile << "Inj-" << injPoint+1;
				injPoint++;
			}
			outFile << "\t" << topBoring;
			totalCord = xCol[num2] + yCol[num2];
			outFile << endl;
			
		}
	num2++;
	num3++;
	
	}
	outFile.close();
	
	return 1;
}
Is that the actual contents of your .CSV file? CSV stands for comma seperated values. I do not see any commas in what you said is the contents. I also do not see comma handling anywhere in your code.
I tried this also, and it didnt work.

6195152.000000, 1897096.000000, 33.000000, 680144.312500, 5.832601,
6195152.000000, 1897096.000000, 32.000000, 659309.625000, 5.819089,
6195152.000000, 1897096.000000, 31.000000, 673557.812500, 5.828375,
6195152.000000, 1897096.000000, 30.000000, 680511.062500, 5.832835,
6195152.000000, 1897096.000000, 29.000000, 612108.000000, 5.786828,
6195152.000000, 1897096.000000, 28.000000, 598957.750000, 5.777396,
Loop on line 48 most likely ends when the end of file (EOF) is reached. There is nothing to read after the EOF. The ifstream has seekg to move the current position on the stream, for example back to beginning.

However, you don't really need that because you don't use the number_of_lines much.
I would rather write something like:
1
2
3
4
5
6
7
8
9
10
struct Record { int x; int y; int z; double data; double un; };
istream & operator>> ( istream &, Record & );

// use
vector<Record> data;
data.reserve(10000);
Record record;
while ( inFile >> record ) {
  data.push_back( record );
}
I got you. I actually tried commenting the part of reading the data and put in the code below and it only reads the first set of numbers "6195152.0000000000".

1
2
3
4
5
6
7
8
9
10
11
12

while(!inFile.eof())
	{
		
		inFile >> xCol[num];
		inFile >> yCol[num];
		inFile >> zCol[num];
		inFile >> dataCol[num];
		inFile >> unCol[num];
		num++;
		
	}
Am I missing something were I need to step over the comma?
std::getline(inFile, xCol[num], ','); ect... http://www.cplusplus.com/reference/string/string/getline/?kw=getline

Also you can simply do while(inFile) http://www.cplusplus.com/reference/ios/ios/operator_bool/
Last edited on
CSV does stand for "comma separated values", but it doesn't have to be commas that separate. Whitespace is fine too.

If you do have commas in the file, then you do need to discard them.
1
2
3
4
5
6
7
8
9
istream & operator>> ( istream & lhs, Record & rhs )
{
  lhs >> rhs.x; lhs.ignore( 1 );
  lhs >> rhs.y; lhs.ignore( 1 );
  lhs >> rhs.z; lhs.ignore( 1 );
  lhs >> rhs.data; lhs.ignore( 1 );
  lhs >> rhs.un; lhs.ignore( 1 );
  return lhs;
}
May I ask why you use ignore(1) instead of ignore()? Since the default size is 1 and default delimiter is EOF.
keskiverto that worked. Thank you very much.
I was torn between "lets use the default, because user of ignore() must know what the function does" and "explicitly show to the quick reader that we ignore 1, whatever that means".
Topic archived. No new replies allowed.