Accessing files within a folder

Pages: 12
correct.. actually i thought first create a program which can read one file.. now we can go to reading a directry.
infact your previous program which was reading a directry is correct..
lets say:

1
2
3
4
5
6
7
8
9
10
string dir = "/home/satchmo05";
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);


now read each file from files variable and read it as above.. is it fine?
Ok, thats what I expected, but this is what I have:

for (unsigned int i = 0; i < files.size(); i++)
{
ifstream indiv_file;
int arr[MAX_SIZE];
indiv_file.open("intro+dir");
indiv_file.open((dir + files[i]).c_str());
cout<< dir+files[i]<<endl;

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

indiv_file.close();
}

*'intro' is everything above that file, and 'dir' has two files below it that I need to access.*
you cant do this:

indiv_file.open("intro+dir");
see your files variable has all the files, correct.

so do this:

indiv_file.open(files[i].c_str());
this will open the file for you.

i hope file is of this type:
std::vector<std::string> file; //ok?

now for each filename in file you will open it and read it. is this possible?

what are you trying to do here?
1
2
indiv_file.open("intro+dir");
indiv_file.open((dir + files[i]).c_str());


trying to open two files with only one stream object!!!
with line 2 there, i was told that is how I open the files. can you just write out my main() function with the directory search added? then I can ask questions why things are a certain way rather than what I have... thanks!!
see it will something like 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//collect all the files first in a some directory.

vector<string> file_list;
string dir = "/home/satchmo05";
if((dp = opendir(dir.c_str())) == NULL) 
{
	cout << "Error(" << errno << ") opening " << dir << endl;
	return errno;
}

while ((dirp = readdir(dp)) != NULL) 
{
	file_list.push_back(string(dirp->d_name));
}
closedir(dp);

//you  have collected all the file names in the directory, open each file,read it and  then next.
int fsize = file_list.size();
for(int i = 0; i < fsize; i++)
{
	ifstream indiv_file;
        indiv_file.open(file_list[i].c_str());
        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(); 
}


understand this, fine tune this program and thats it.
no, unfortunately, it still isn't working...

Now i'm getting:

InputFiles/ February.dat
1096
6436272

InputFiles/ January.dat
1096
6426272
0
so now you must have changed your code, what's the final code, post it.
then we find problem in that.

Topic archived. No new replies allowed.
Pages: 12