Simple string arrays

Mar 6, 2012 at 11:58am
Hi, I'm a newbie in programming and I'm currently writing a simple tool in C.
This program scans all the directories in %USERPROFILE%\Documents for *.doc and *.docx files using FindFirstFile and FindNext. If the name of the file ends in *.doc or *.docx, the file name is inserted into an array of strings.
At the end of the scanning the program will use this string array to begin the copy of the files in an usb key.

In order to create a string array I use "char * * filenames;". The problem is: I don't know how many files will be present in the folder. How can I know the number of files to initialize the array? For example, if the array has 1000 strings, and the files are 10-20, I'm wasting memory; if the files are more than 1000, these won't be included. How can this be solved?
Mar 6, 2012 at 12:01pm
Use a std::vector<std::string> and use puch_back to add a filename to the vector. No need to know in advance how many files there is.
Mar 6, 2012 at 12:12pm
Thank you.
Isn't it C++? If yes, how can I do that in standard C?
Topic archived. No new replies allowed.