关于流的问题 [英] Question on ofstream

查看:94
本文介绍了关于流的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于c ++ ofstream的快速提问。在我的主要我把它声明为int main(int arg,char * argv [])。所以当我运行以下命令./program.cpp test.txt时,它将运行我的程序并将test.txt文件传递给它。我的问题是如何保存我的输出文件作为我传入的文件名。我知道使用ofstream但我不知道如何传入我传入我的程序测试的文件。我有以下代码。任何帮助都会很棒。谢谢


ofstream myfile("",ios :: out);

Quick question on c++ ofstream. in my main i have it declared as int main(int arg, char*argv[]). so that when i run the following command ./program.cpp test.txt it will run my program and pass the test.txt file into it. My question is how do i save my output file as the file name i passed in. i know to use ofstream but im not sure how to pass in the file i passed into my program to test. I have the following code.any help would be great. thanks

ofstream myfile(" ", ios::out);

推荐答案

文件名将保存在String argv [1] ,所以只需将它作为你给你的ofstream的文件名。


问候,

Nepomuk
The filename will be saved in the String argv[1], so just use that as the filename you give to your ofstream.

Greetings,
Nepomuk


好吧我想我理解这个概念。我只是不确定我编码是否正确..这是我试图实现它...但它不起作用..


int main(int arg ,char * argv []){

string fname = argv [1];

ofstream myfile(fname,ios :: out);
ok i think i understand that concept. Im just not sure if im coding it right.. here is how i have tried to implement it..but it doesn not work..


int main(int arg, char *argv[]){
string fname= argv[1];
ofstream myfile( fname, ios::out);


你不能使用字符串对象作为参数。


要么这样做:
You cannot use a string object as the argument.

Either do this:
展开 | 选择 | 换行 | 行号


这篇关于关于流的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆