0

处理一些将(输出/输入)5个不同文件放到我的桌面上的代码。最后,将其归结为一条错误消息,即“错误 <23>:C2109:需要下标数组或指针类型”。它与 myfile.open; 我试过 -> 运算符。不完全是如何把它变成一个数组,如果这是我想做的,因为我已经尝试将字符串变成 char 并出现警告。谁能让我知道如何修改我的代码来纠正这个问题?我对 C++ 和编程比较陌生,只有几个月。

#include <iostream>
#include <fstream>

using namespace std;

struct pizza{
string FILENAMES[9];
};

int main ()
{

int i;
char FILENAMES;

pizza greg = {"file1.doc", "file2.doc", "file3.doc", "file4.doc", "file5.doc"};

  cout << "Input is invalid.  Program will end. " << "\n" ;
for (i = 0; i < 5; i++)
{
const char *path="/Desktop/Libraries/Documents" ;
    ofstream myfile(path);
    myfile.open (FILENAMES[i]) ;
    myfile << "How you like math?\n" ;
    myfile.close();
};

return 0;

}

您的建议很有帮助,我的程序现已启动并运行。(没有双关语,哈哈。)

4

2 回答 2

2

循环应该看起来像这样:

const char *path="/Desktop/Libraries/Documents";
for (int i = 0; i < 5; ++i)
{
    std::string name(path + greg.FILENAMES[i]);
    std::ofstream myfile(name.c_str());
    if (myfile) {
        myfile << "How you like math?\n" ;
    }
    else {
        std::cerr << "ERROR: failed to open '" << name << "' for writing\n";
    }
}
于 2013-09-05T23:39:05.707 回答
0
char FILENAMES;

FILENAMES不是数组。即使是这样,您也必须将其设为字符串数组或二维字符数组才能执行您想要的操作。

您可能打算做的是访问里面的字段greg

myfile.open (greg.FILENAMES[i]);
于 2013-09-05T23:31:46.227 回答