5

所以基本上当我尝试这样做时

char* inputFileName;
cout<<  "Filename: "; cin>>*inputFileName;

它让我输入文件名,但是当我按下回车键时,我得到一个未处理的异常错误。有任何想法吗?

如果我尝试也编辑

char* inputFileName;
cout<<  "Filename: "; cin>>inputFileName;

当我尝试运行它时,我得到一个调试断言失败。

4

2 回答 2

10

您需要传入指针,而不是取消引用的指针,并为 char * 分配内存

#include <iomanip>
#include <iostream>
using namespace std;

int main(){
    const size_t BUFFER_SIZE = 1024;
    char inputFileName[BUFFER_SIZE];
    cout << "Filename: ";
    cin >> setw(BUFFER_SIZE) >> inputFileName;
    cout << inputFileName << endl;
}

除此之外的另一个选择char inputFileName[BUFFER_SIZE];

char *inputFileName = new char[BUFFER_SIZE];

然后

delete [] inputFileName;
于 2013-05-02T01:53:18.227 回答
4

尝试使用这个:

#include <iostream>
using namespace std;

int main()
{
    char *inputFileName = new char[50];
    cout << "Filename: ";
    cin >> inputFileName;
    cout << inputFileName << endl;
    /* .... */
    delete [] inputFileName;
}
于 2015-10-13T20:47:37.603 回答