2

Hey so this is probably a dumb beginner question.

I want to write the filename of all .txt files from a folder inside a const char* array[].

So I tried to do it like this:

const char* locations[] = {"1"};
bool x = true;
int i = 0;    

LPCSTR file = "C:/Folder/*.txt";
WIN32_FIND_DATA FindFileData;

HANDLE hFind;
hFind = FindFirstFile(file, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
    do 
    {
        locations.append(FindFileData.cFileName); //Gives an error
        i++;
    }
    while (FindNextFile(hFind, &FindFileData));
    FindClose(hFind);
}
cout << "number of files " << i << endl;

Basically the code should add the Filename of the .txt file to the const char* locations array but it doesn't work using append as it gives me the error: "C++ expression must have class type but it has type ''const char *''"

So how do I do it right?

3
  • Short answer: use std::vector<std::string>. Commented Dec 4, 2021 at 21:19
  • hey @Brian you want me to use std::vector<std::string> instead? I ll need the const char* locations[] format for an array listbox. how would I convert that? tried it with std::vector<std::string>locations; and locations.push_back(FindFileData.cFileName); now. How can i make that to an array? Commented Dec 4, 2021 at 21:29
  • Use a vector. Then locations[ i ].data() will return the char* pointer to the underlying buffer of std::string. Commented Dec 4, 2021 at 21:31

1 Answer 1

3

Problem:

  1. You can't append items to a C array -T n[]- because the length of the array is determined at compile time.
  2. An array is a pointer(which is a scalar type), which isn't an object and doesn't have methods.

Solution:

The easiest solution is to use an std::vector which is a dynamic array:

#include <vector>
// ...
std::vector<T> name;

// or if you have initial values...
std::vector<T> name = {
    // bla bla bla..
};

In your case an array called location of strings is std::vector<std::string> locations.
For some reason, C++ containers don't like the classical append(), and provide the following methods:

container.push_back();  // append.
container.push_front(); // prepend.
container.pop_back();   // remove last
container.pop_front();  // remove first

Note that std::vector only provides push_back and pop_back

See:

Sign up to request clarification or add additional context in comments.

3 Comments

An array is not a pointer.
@Peter An array is a pointer to elements × 1 element size length chunk of memory. Also int a[5]; std::cout << a << std::endl; prints an address.
An array can be converted to a pointer (to its first element) but is something different. There are contexts when assuming an array is a pointer works, and others where it fails. If what you say is true, the discussion in this link would be moot. stackoverflow.com/questions/1461432/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.