read in .csv data and array manipulation

Hello,

I tried to read a 3001 x 90 .csv data file into a two dimension array and then divide the array into three 3001 x 30 arrays. Then I tried to build a loop to repeatedly save each 3001 x 30 array into a .txt file, LIN_4.txt, (expecting to overwrite by the old file). My code did not work. Any helps will be appreciated.

Bo

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

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

	int main(int argc, char* const argv[]) {


		static double data[3001][90];
		std::ifstream file("C:\\Users\\Bo\\Desktop\\Data\\datal.csv");

		for (int row = 0; row < 3001; ++row){
			std::string line;
			std::getline(file, line);
			if (!file.good())
				break;

			std::stringstream iss(line);

			for (int col = 0; col < 90; ++col){
				std::string val;
				std::getline(iss, val, ',');
				if (!iss.good())
					break;
				std::stringstream convertor(val);
				convertor >> data[row][col];
			}
		}
	
		    for (int k = 0; k < 3; ++k) {
				ofstream LIN_4("C:\\Users\\Bo\\Desktop\\C++\\LIN_4.txt");
				double dat[3001][30];
				for (int i = 0; i< 3001; ++i) {
					for (int j = (k - 1) * 30; j < (k - 1) * 30 + 30; ++j){
					dat[i][j] = data[i][j];					
				}
			}
				LIN_4 << dat[3001][30];
				LIN_4.close();	
		}
	}
Last edited on
This code does not look like it will compile because your braces are mismatched. Could you check that and your indentation, then describe what you mean by "does not work"? How does it not work? Do you get an error message? Strange output?
The data saved into LIN_4.txt were just a couple of unmeaningful numbers. This code could compile, but could not produce the data set I need. Thanks.
Without seeing the input file, verifying what is wrong is extremely difficult. I suspect that you're probably not reading the input file correctly.

Also with the sizes of your arrays I would recommend you consider using vectors instead.

Opening and closing your output file inside the loop is probably not the best thing to do. Why not open the file prior to the loop?

Lastly this:
LIN_4 << dat[3001][30];
Is accessing your array out of bounds. By the way this is not how you print an array, this is trying to print a single element.

Thanks, jlb. The input file contain 3001 rows and 90 columns with the first row containing the variable names: x1, x2....x30....x1,x2,....,x30....x1,x2,....x30. All are data are binary: 0s and 1s.

I see what you meant with dat[3001][30]. I will fix it and try again. Also, will try to fix the structure of the loop based on you suggestion.
I tried to fix it as possible as I can, but I got lost again....


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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main(int argc, char* const argv[]) {


	static double data[3001][90];
	std::ifstream file("C:\\Users\\Bo\\Desktop\\Data\\datal.csv");

	for (int row = 0; row < 3001; ++row) {
		std::string line;
		std::getline(file, line);
		if (!file.good())
			break;

		std::stringstream iss(line);

		for (int col = 0; col < 90; ++col) {
			std::string val;
			std::getline(iss, val, ',');
			if (!iss.good())
				break;
			std::stringstream convertor(val);
			convertor >> data[row][col];
		}
	}

	static double dat[3001][30];
	for (int k = 0; k < 3; ++k) {
		ofstream LIN_4("C:\\Users\\Bo\\Desktop\\C++\\test\\LIN_4.txt");
		for (int i = 0; i< 3001; ++i) {
			for (int j = (k - 1) * 30; j < (k - 1) * 30 + 30; ++j){
				dat[i][j] = data[i][j];
			}
		}
		LIN_4 << dat;
		LIN_4.close();
	}
}

Last edited on
Topic archived. No new replies allowed.