如何在C++中的字符串数组中找到下一个空闲元素,然后将数据附加到它

How would I find the next free element in a string array in C++ then append data to it?

本文关键字:然后 元素 数据 下一个 C++ 字符串 数组      更新时间:2023-10-16

Exzample代码。。。

const int SIZEOFFILENAMES = 100;
string fileNames[SIZEOFFILENAMES];

int main () {

    string inFilePath;
    cout << "Specify the file path:" << endl;
    cin >> inFilePath;  
    ifstream inFile;
    // open in binary
    inFile.open(inFilePath.c_str(), ios::binary);
    inFile.seekg(0, ios_base::end);
    int fileLen = inFile.tellg();
    inFile.seekg(0, ios_base::beg);
        char charArr[10000];
    inFile.read(charArr, fileLen);
    inFile.close();
    inFile.clear(ios_base::goodbit);

比索代码应该是

  1. 检查filename数组元素大小。

  2. 将inFilePath附加到下一个自由元素。

}

下面是一个将输入的文件路径添加到文件名向量的简单示例:

// ...
#include <vector>
#include <string>
std::vector<std::string> fileNames;

int main ()
{
  std::string inFilePath;
  std::cout << "Specify the file path:" << std::endl;
  std::cin >> inFilePath;
  filenames.push_back(inFilePath);
  // do other stuff
  // ...
}
相关文章: