Searching Directory for .CSV file and Searching file

Pages: 12
if they are all the same format, as you said, just make a struct of that format.

struct data
{
string first, second;
int third, fourth;
};


vector<data> filedata(1000); //whatever initial size makes sense for your stuff.
//you can typedef the above and treat it like an object if that appeals to you.

loop over file
file >> filedata[index].first >> filedata[index].second … etc //you can make your own >> operator to do this, but is it worth the trouble here?

if you don't know a max size (1000 above) you may need to use push back (ugg) and let it grow. Sometimes you can't avoid that.
Last edited on
here is what I have now. It runs but However the data is not outputting.

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
#include <stdio.h>
#include <dirent.h>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>


using namespace std;

int main(void)
{

	
	struct datas
	{
		string firstname;
		string lastname;
		int weight;
		int age;
	};

	vector<datas> filedata(1000);

	int counter = 0;

	// Pointer for directory entry
	struct dirent *de;

	// opendir() returns a pointer of DIR type. 
	DIR *dr = opendir(".");

	// opendir returns NULL if couldn't open directory
	if (dr == NULL)
	{
		cout << "Could not open current directory" << endl;
		cin.get();
		return 0;
	}

	while ((de = readdir(dr)) != NULL)
	{
		string name(de->d_name);

		// Search directory for all files ending in .csv
		if (name.size() > 4 && name.substr(name.size() - 4) == ".csv")
		{
			cout << name << ":\n";
			ifstream fin(name); //bring in file
			string line;;


			while (getline(fin, line)) { // To get the number of lines in the file
				counter++;
			}
			datas* data = new datas[counter]; // Add number to structured array

			for (int i = 0; i < counter; i++) 
			{

				fin >> data[i].firstname >> data[i].lastname >> data[i].weight >> data[i].age;

				cout << data[1].firstname << data[1].lastname << endl;
			}
			cout << data[1].firstname << data[1].lastname << endl;

			 
			

		}

	}
	cin.get();
	closedir(dr);

	return 0;
}
Topic archived. No new replies allowed.
Pages: 12