A question I encoutered

Hi,

Is there anyone knows that how to store a series of strings? For example, there are different files names: "FileA.txt", "FileB.txt", etc. How to store these strings in order so that I can quote them later?
closed account (zb0S216C)
In C:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main( )
{
  char const * const FileA = "FileA.txt";
  char const * const FileB = "FileB.txt";

  // Alternatively:
  
  char const * const File[2] = 
  {
    "FileA.txt",
    "FileB.txt"
  };
}

In C++:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>

int main( )
{
  std::string const FileA( "FileA.txt" );
  std::string const FileB( "FileB.txt" );

  // Alternatively:

  std::string const File[2] =
  {
    "FileA.txt",
    "FileB.txt"
  };
};

Wazzak
Last edited on
Thanks for your reply!

But the thing is I don't know how many files there are. Actually the whole program is about asking how many files firstly, and then combine all of them line by line. Therefore I think there need to be an array which each element can store a string, which is the name of each file.
Last edited on
You should probably use one of the dynamic containers for this. A Vector or List of std::strings would be the easiest to use if you have never done this before, but anyone of them should do given the parameters you've listed.

EDIT: Sorry, I almost forgot

- LIST: http://www.cplusplus.com/reference/list/list/

- VECTOR: http://www.cplusplus.com/reference/vector/vector/
Last edited on
Topic archived. No new replies allowed.