写入用户输入的文件名 [英] Writing to a file name input by user
问题描述
我创建了一个Windows窗体应用程序,其中一部分包括从文本框中获取名称并将其附加到目录中,以便写入该目录中的文件.
最高级别始终是C:\ Programs \ PROGRAMS \
这是我现在要为构造函数提供的内容(这不是我真正想要的方式,只是想提供一些我正在查看的内容):
createFile::createFile(string name) { string path = "C:\\Programs\\PROGRAMS\\" + name + "\\" + name + ".txt"; }
例如,我想写入的文件可能是:
C:\ Programs \ PROGRAMS \ ATP-8878B \ ATP-8878B.txt
我希望能够使用
ofstream outFile;
outFile.open(path);
我得到的错误是:试图从受保护的内存中读取或写入.
在第一个strcat()调用中.
可以将参数(const char *名称)更改为其他类型,因为无论如何它都需要从String ^进行转换,如果您认为这样做会有所帮助的话.根据我的发现,路径最终必须是const char *才能调用outFile.open(path);
有什么更好的方法可以做到这一点吗?
在此先感谢您的帮助.
修改后的原始代码.
在将构造函数中的代码更改为现在我最高职位中的代码之后,解决方案很简单:
ofstream outFile;
outFile.open(path.c_str());
您其他代码无效的原因是您拥有:
createFile::createFile(const char* name) { char* path = "C:\\Programs\\PROGRAMS\\"; path = strcat(path, name); path = strcat(path, (const char*)''\\''); path = strcat(path, name); path = strcat(path, (const char*)''.txt''); }
什么时候应该有这样的事情:
createFile::createFile(const char*name) { char path[] = "C:\\Programs\\PROGRAMS\\"; strcat(path, name); strcat(path, (const char*)"\\"); strcat(path, name); strcat(path, (const char*)".txt"); }
I have created a Windows form app and part of it includes getting a name out of a text box and appending it to a directory in order to write to a file in that directory.
The top level will always be C:\Programs\PROGRAMS\
Here is what I have right now for my constructor (This is not how I actually want to do it, just wanted to provide some idea of what I''m looking at):
createFile::createFile(string name) { string path = "C:\\Programs\\PROGRAMS\\" + name + "\\" + name + ".txt"; }
So for example a file I would like to write to could be:
C:\Programs\PROGRAMS\ATP-8878B\ATP-8878B.txt
and I would like to be able to use
ofstream outFile;
outFile.open(path);
The error I get is: Attempting to read or write from protected memory.
at the first strcat() call.
The parameter (const char* name) can be changed to another type, as it needs to be converted from a String^ anyway, if you think that could be helpful. From what I''ve found path needs to end up being a const char* in order to call outFile.open(path);
Any ideas on a better way I can do this?
Thanks in advance for any help.
EDIT:
Modified original code.
Sorry to waste anyone''s time who read this... apparently I just wasn''t thinking clearly.
After changing the code in the constructor to what it now in my top post, the solution was simply:
ofstream outFile;
outFile.open(path.c_str());
The reason your other code wasn''t working was that you had:
createFile::createFile(const char* name) { char* path = "C:\\Programs\\PROGRAMS\\"; path = strcat(path, name); path = strcat(path, (const char*)''\\''); path = strcat(path, name); path = strcat(path, (const char*)''.txt''); }
when you should''ve probably had something like this:
createFile::createFile(const char*name) { char path[] = "C:\\Programs\\PROGRAMS\\"; strcat(path, name); strcat(path, (const char*)"\\"); strcat(path, name); strcat(path, (const char*)".txt"); }
这篇关于写入用户输入的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!