Accessing files within a folder

Pages: 12
I am writing a program that will have .dat files within a folder. The user will input the folder, and there should be a few .dat files within. How do I open up the files to read them into an array?! Please respond ASAP!! Thank you in advance for your help.

*NOTE: Use the most basic C++ you can; I am a beginner! Thanks! The entire getDir() was given to us to complete the assignment, I didn't write that!*

const int MAX_SIZE = 31;
int getDir(string dir, stringVector &files);

int main()
{
ifstream in_file;
ofstream out_file;
int i=0, array[MAX_SIZE], next;
float average_temp, maximum_temp;
int start;
string dir, intro;

intro = "C://*/*/*/*/*/*/Homework 06";
cout << "Enter directory with files available for use: "; cin >> dir;
cout << endl << intro + dir << endl;
stringVector files = stringVector();

getDir (dir, files);

for (unsigned int i = 0; i < files.size(); i++)
{
cout << files[i] << endl;
in_file.open(files[i].c_str());
while (in_file >> next) {
array[i] = next;
i++;
}
for (int i = 0; i < MAX_SIZE; i {
cout << array[i] << " ";
}
}

in_file.close();
out_file.close();
cin >> start;
return 0;
}
int getDir (string dir, stringVector &files)
{
DIR *dp;
struct dirent *dirp;

if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}

while ((dirp = readdir(dp)) != NULL) {
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}
you are doing it correct, now make an object of
ifstream
and open the file one by one.
read data till not eof and close the file.
I would think intro = "C://*/*/*/*/*/*/Homework 06"; is asking for trouble.

If your filename has C:, this is probably a Windows application, but you're using Unix file/directory functions.

What platform are you using? What compiler are you using? File/Directory handing isn't part of the C++ language, it's dependent on supporting libraries; and in this case, environment specific libraries.
Last edited on
kbw, how do I set my code like the way you did in your reply? please always bash me for not doing it, but I never know how... :(
oh, but I'm using DevC++ compiler on a Windows computer.
I'm still getting the crazy large answers. Here is my loop, everything else in the code hasn't changed so just refer back up to code:

for (unsigned int i = 0; i < files.size(); i++)
{
ifstream indiv_file;
cout << files[i] << endl;
indiv_file.open(files[i].c_str());
while (indiv_file >> next)
{
array[i] = next; i++;
}
for (int i = 0; i < MAX_SIZE; i++)
{
cout << array[i] << " ";
}
indiv_file.close();
}

How should it be if it isn't correct?
this is correct. whats the problem??
When i input the folder where my data files are, it reads them, but it outputs random numbers...why would that be?
the program looks perfect. try running this:


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
#define MAX_SIZE 100
void main()
{
	ifstream indiv_file;
	indiv_file.open("test.txt");
	if(indiv_file.fail())
		return;

	int next;
	int arr[MAX_SIZE];
	int i = 0;
	
	while (indiv_file >> next)
	{
		arr[i] = next; 
		i++;
		if(i == MAX_SIZE)
			break;
	}

	for (int j = 0; j < i; j++)
	{
		cout << arr[j] << endl;
	}	

	indiv_file.close();
}
there is one problem in your program which i've corrected.. can you figure out whats that??
make a file test.txt in the current directory. which should have random numbers
Well, you added another for loop to get rid of all the number unused. However, I tried what you told me to, and this time I get very very large numbers (not what I entered into the dataset).
see the first loop will read the whole file, or to the limit of the array.
second loop will print the array till it is filled, "i" will tell till what limit it is filled.

ok, tell me whats in the file and what output you are getting then i may say something.
or better paste some part of your file/dataset here.
here is one file:

28 27 35 34 10 14 22 33 48 42 45 40 38 32 30 17 13 6 10 9 13 13 15 18 17 22 34 31 25 20 7

here is the other:

36 54 76 65 63 64 38 32 30 28 37 39 34 23 39 42 43 48 49 58 53 56 62 53 49 40 33 37
i executed the program which i posted above with the data you gave, the output is coming out nicely.
i created a file test.txt with the above data, and i got this output..
36
54
76
65
63
64
... more numbers...

which compiler you are using?
DevC++
i have no idea regarding that compiler. i compiled the program on gcc and ms vc++. both are giving correct output.

you said you are getting very big numbers. what kind of output you are getting when you run the above program which i posted?
Copy and paste the code that you used to access the data? i dont think we have the same code then. I really don't think that compiler matters for the program that I'm running...
yes you are correct.. ok im pasting it 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
#include <iostream>
#include <fstream>
using namespace std;

#define MAX_SIZE 100
int main()
{
        ifstream indiv_file;
        indiv_file.open("test.txt");
        if(indiv_file.fail())
                return 0;

        int next;
        int arr[MAX_SIZE];
        int i = 0;

        while (indiv_file >> next)
        {
                arr[i] = next;
                i++;
                if(i == MAX_SIZE)
                        break;
        }

        for (int j = 0; j < i; j++)
        {
                cout << arr[j] << endl;
        }

        indiv_file.close();
        return 0;
}


try this code with your data set in a file in the same directory in which your binary is created.
That works (as expected), but that's not the problem. I need help with the directory portion of the code...How to access a certain file, read the dataset, and then close that file, and then go to the next, read the dataset, and then close, etc.
Pages: 12