使用C 从.TXT文件读取
Reading from a .txt file using class C++
你能帮我吗?我是Truna,可以创建一个支持cmd
-> int main(int argc, char *argv[])
的程序。使用课程,我想创建一种写下我的不适的方法。.txt文件的名称是第三个参数。
我的问题是我需要open()
我的文件,但是可以更改文件的名称。我尝试打开指定的文件,它的是1.txt
。但是,我应该如何这样做才能在 cmd
中给出文件的名称,因为我的方法(在 Header
中,请参见该名称?
我的方法:
void recordData()
{
wRecord.open("1.txt", std::ios_base::app);//the hard way to create a file in this method.
int i=0;
wRecord << "Name of your city: " << Name << std::endl;
......................................................
wRecord << "Quanity of schools in your city: " << Schools << std::endl;
wRecord << std::endl;
}
,我认为应该是wRecord.open(argv[3], std::ios_base::app);
。但是如何正确提供argv[3]
?
我的代码,当我致电void recordData()
:
if (argv[1] == create)
{
wRecord.open(argv[3], std::ios_base::app);// here is I put any name of file
//But I can't use any name because in method there is only `1.txt`
if (!wRecord)
{
std::cerr << "Error: canNOT oprn a text file" << std::endl;
exit(-1);
}
}
您应该在recorddata()函数中添加一个char*参数,以将其从main()argv []传递给wrecord.open()。
即:
void recordData(char *filename)
{
wRecord.open(filename, std::ios_base::app);
...
}
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量