f



How can I creat a file if the file doesn't exit?

int main( )
{
	string str;
	string filename;
	fstream outfile;

	cout<<"Input the file name you will save in:"<<endl;
	getline(cin,filename);
	outfile.open(filename.c_str() );
	//cin.clear(); //清空流
	cout<<"Please input your text....."<<endl;
	getline(cin,str);
	outfile<<str;
	outfile.close();
	return 0;

}

when it request me to input the filename, if the filename I input is
exit, the program work normally. But if the file  doesn't exit, it
can't work .

how can i creat a new file when it doesn't exit?

Thanks~ ^_^#


{ Please post with ISO Latin-1 or UTF-8 character encoding. TIA., -mod }

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
somebodykiss
3/16/2009 6:51:09 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

4 Replies
881 Views

Similar Articles

[PageSpeed] 6

somebodykiss@gmail.com wrote:
> int main( )
> {
> string str;
> string filename;
> fstream outfile;

Note: declaring all variables at the beginning of a function was necessary
in C, but not in C++. Even more, it is considered bad style. The reason is
that restricting the scope of a variable as much as possible makes it
easier to see where it is used and how.

> cout<<"Input the file name you will save in:"<<endl;
> getline(cin,filename);
> outfile.open(filename.c_str() );

Two things here:
1. You need to check if opening succeeds. It would have told you that it
didn't here. Suggestion:

   if(!outfile)
     throw std::runtime_error("failed to open file '"+filename+"'");

2. The reason it fails is that you didn't provide any parameters concerning
the 'mode'. For writing, it must contain 'ios_base::out'. Note that it is
clearer if you use an 'ofstream' instead of an 'fstream', where you can
also omit that parameter because it is implicit.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Ulrich
3/16/2009 12:55:32 PM
On Mar 16, 5:51 am, "somebodyk...@gmail.com" <somebodyk...@gmail.com>
wrote:
> int main( )
> {
>         string str;
>         string filename;
>         fstream outfile;
>
>         cout<<"Input the file name you will save in:"<<endl;
>         getline(cin,filename);
>         outfile.open(filename.c_str() );
outfile.open(filename.c_str(), ios_base::out);
>         //cin.clear(); //清空流
>         cout<<"Please input your text....."<<endl;
>         getline(cin,str);
>         outfile<<str;
>         outfile.close();
>         return 0;
>
> }
>

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
red
3/16/2009 12:57:37 PM
> Note: declaring all variables at the beginning of a function was necessary
> in C, 
In C, variables have to be declared at the beginning of the block, not
the function.

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Matthias
3/20/2009 6:51:28 PM
Matthias Berndt wrote:
>> Note: declaring all variables at the beginning of a function was 
>> necessary
>> in C, 
> In C, variables have to be declared at the beginning of the block, not
> the function.

That used to be the case in C90, but not any more in C99, the current C;
declarations and statements can be freely mixed in a block in C99.

-- 
Seungbeom Kim

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Seungbeom
3/24/2009 4:13:35 PM
Reply: