Hi all,
I am new to this forum, and pretty new to C++. This is what I use:
WinXP Home SP2
mingw32: g++ (GCC) 3.4.2 (mingw-special)
Dev-C++ 4.9.9.2
My problem:
***********
*** 1 ***
I read 12 files of a directoy into a char *array. As control, I output each array item after having read each file. It looks ok, like this:
Array 0: .
Array 1: ..
Array 2: IMAG0004.JPG
Array 3: IMAG0005.JPG
Array 4: IMAG0006.JPG
Array 5: IMAG0007.JPG
Array 6: IMAG0010.JPG
Array 7: IMAG0011.JPG
Array 8: IMAG0012.JPG
Array 9: MPG_0001.MPG
Array 10: MPG_0002.MPG
Array 11: MPG_0003.MPG
Array 12: MPG_0008.MPG
Array 13: MPG_0009.MPG
*** 2 ***
If I iterate later in the program through the same array from i=0 to 12, the output is very strange, i.e., only the last directory entry is listed:
i: 0 Array 0: MPG_0009.MPG
i: 1 Array 1: MPG_0009.MPG
i: 2 Array 2: MPG_0009.MPG
i: 3 Array 3: MPG_0009.MPG
i: 4 Array 4: MPG_0009.MPG
i: 5 Array 5: MPG_0009.MPG
i: 6 Array 6: MPG_0009.MPG
i: 7 Array 7: MPG_0009.MPG
i: 8 Array 8: MPG_0009.MPG
i: 9 Array 9: MPG_0009.MPG
i: 10 Array 10: MPG_0009.MPG
i: 11 Array 11: MPG_0009.MPG
i: 12 Array 12: MPG_0009.MPG
i: 13 Array 13: MPG_0009.MPG
Can anybody help, please, what is wrong? Please find the program main.cpp at the end of my message.
Thank you,
cppcub
P.S. Now comes the listing. Note that this is only the beginning of larger program I am going to develop, to help me rename files from my digital camera. An note that I know that I should not use too many global variables and constants. Trust me, after this program will get to work, local variables and functions will folow suite.
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
************************
main.cpp
************************
// reads files from subdirectory ./BB into array alleFiles[]
//
// modified from :
// http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=245
// for details see end of program
// 2008-feb-20
#include <iostream>
#include <errno.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
const int directorySize = 20; // arbitrary size of array; currently less than 20 entries
char *alleFiles[directorySize];
int i = 0;
int main()
{
for (i = 0; i < directorySize; i++)
{
alleFiles[i] = "string"; //to be on the safe side
}
DIR *pdir;
struct dirent *pent;
pdir=opendir("./BB"); // open directory
if (!pdir){
printf ("opendir() failure; terminating");
exit(1);
}
errno=0;
i = 0;
//read directory ./BB and put entries into alleFiles[i]
while ((pent=readdir(pdir)))
{
//alleFiles[i] gets directory entries
alleFiles[i] = pent->d_name;
std::cout << " Array " << i << ": " << alleFiles[i] << std::endl; //just to be sure it works
i = i + 1;
//alleFiles[5] = "fuenf"; // control: if this is outcommented, alleFiles[5] keeps the string "fuenf"
}
if (errno)
{
printf ("readdir() failure; terminating");
exit(1);
}
closedir(pdir);
//check alleFiles[i] again
for (i = 0; i < directorySize; i++)
{
std::cout << "i: " << i;
std::cout << " Array " << i << ": " << alleFiles[i]; // alleFiles[i] with i from 14, returns only last enty from ./BB! Why?
std::cout << std::endl; // print alleFiles[i]
}
std::cout << std::endl;
system("PAUSE");
return EXIT_SUCCESS;
}
/*
This is the source I modified main.cpp from. My major mdification was to
add an array to hold all directory entries.
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=245
Manipulating Directories
Last updated Jul 29, 2005.
Standard C++ doesn't have a library for manipulating directories. The closest
thing you can get is Boost's filesystem library. However, most implementations
nowadays support the quasi-standard <dirent.h> and <dir.h> libraries. These
libraries, originally written in C, enable you to list the files in a directory,
change the current directory, create a new directory and delete an existing
directory.
Traversing a Directory
The <dirent.h> header declares functions for opening, reading, rewinding and
closing a directory. To view the files in a directory, you have to open it first
using the opendir() function:
DIR * opendir(const char * path);
opendir() returns a pointer to DIR. DIR is a data structure that represents a
directory. A NULL return value indicates an error. path must be a name of an
existing directory.
To traverse a successfully opened directory, use readdir():
struct dirent * readdir (DIR * pdir);
pdir is the pointer obtained from a previous opendir() call. readdir() returns
a pointer to a dirent structure whose data member, d_name, contains the name of
the current file (the rest of dirent's members depend on the specific file
system installed on your machine so I won't discuss them here). Each successive
call to readdir() advances to the next file in the directory.
readdir() returns NULL either in the event of an error or once you have
traversed all the files in the directory. To distinguish between these two
conditions, check errno after every readdir() call. Since readdir() changes
errno only if an error has occurred, you need to reset it explicitly before each
readdir() call.
The rewinddir() function resets a directory stream to the first entry:
void rewinddir(DIR *pdir);
rewinddir() also ensures that the directory accurately reflects any changes to
the directory (file deletion, renaming etc.) since the last opendir() or
rewinddir() calls.
Use closedir() to close the directory once you are done with it:
int closedir (DIR * pdir);
pdir is the pointer obtained from a previous opendir() call.
*/
| |