简单的用户请求用于输出和输入的文件名

发布于 2024-09-18 17:21:37 字数 834 浏览 12 评论 0原文

如何请求用户输入我的程序需要读取的文件名并让它输出带有 .out 扩展名的名称?

示例:

char fileName[256];
cout << "What is the file name that should be processed?";
cin >> fileName;

inFile.open(fileName);
outFile.open(fileName);

但我需要它将文件保存为 filename.out 而不是原始文档类型(IE:.txt)

我已经尝试过:

char fileName[256];
cout << "What is the file name that should be processed?";
cin >> fileName;

inFile.open(fileName.txt);
outFile.open(fileName.out);

但我收到以下错误:

c:\users\matt\documents\visual studio 2008\projects\dspi\dspi\dspi.cpp(41):错误 C2228:“.txt”的左侧必须具有类/结构/联合 1>类型是“char [256]”

c:\users\matt\documents\visual studio 2008\projects\dspi\dspi\dspi.cpp(42):错误 C2228:“.out”左侧必须具有类/结构/联合 1>类型是“char [256]”

How can I request the user to input the filename that my program needs to read from and have it output the name with .out extension instead?

Example:

char fileName[256];
cout << "What is the file name that should be processed?";
cin >> fileName;

inFile.open(fileName);
outFile.open(fileName);

But I need it to save the file as a filename.out instead of the original document type (IE:.txt)

I've tried this:

char fileName[256];
cout << "What is the file name that should be processed?";
cin >> fileName;

inFile.open(fileName.txt);
outFile.open(fileName.out);

But I get these errors:

c:\users\matt\documents\visual studio 2008\projects\dspi\dspi\dspi.cpp(41) : error C2228: left of '.txt' must have class/struct/union
1> type is 'char [256]'

c:\users\matt\documents\visual studio 2008\projects\dspi\dspi\dspi.cpp(42) : error C2228: left of '.out' must have class/struct/union
1> type is 'char [256]'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长发绾君心 2024-09-25 17:21:37

要更改文件扩展名:

string fileName;
cin >> fileName;
string newFileName = fileName.substr(0, fileName.find_last_of('.')) + ".out";

To change fileName extension:

string fileName;
cin >> fileName;
string newFileName = fileName.substr(0, fileName.find_last_of('.')) + ".out";
过期情话 2024-09-25 17:21:37

您正在使用 iostream,这意味着使用 C++。这反过来意味着您可能应该使用 std::string,它具有用于字符串连接的重载运算符 - 以及自动内存管理和增加安全性的良好副作用。

#include <string>
// ...
// ...
std::string input_filename;
std::cout << "What is the file name that should be processed?\n";
std::cin >> input_filename;
// ...
infile.open(input_filename + ".txt");

You are using iostreams, implying the use of C++. That in turn means you should probably be using std::string, which has overloaded operators for string concatenation - and the nice side effect of automatic memory management and added security.

#include <string>
// ...
// ...
std::string input_filename;
std::cout << "What is the file name that should be processed?\n";
std::cin >> input_filename;
// ...
infile.open(input_filename + ".txt");
别念他 2024-09-25 17:21:37

编写 filename.txt 意味着 fileName 是一个对象,并且您想要访问它的数据成员 .txt。 (类似的论点适用于fileName.out)。相反,使用

inFile.open(fileName + ".txt");
outFile.open(fileName + ".out");

Writing filename.txt implies that fileName is an object and you want to access it's data member .txt. (Similar argument applies to fileName.out). Instead, use

inFile.open(fileName + ".txt");
outFile.open(fileName + ".out");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文