Connecting Tech Pros Worldwide Forums | Help | Site Map

output to a file

Gaijinco
Guest
 
Posts: n/a
#1: Nov 22 '05
If I want to get input from a file I use:

#include <fstream>

int main(){

ifstream input("file.in",ios::in);

int x;
input >> x;

}

What about if I want to output to a file?


John Harrison
Guest
 
Posts: n/a
#2: Nov 22 '05

re: output to a file


Gaijinco wrote:[color=blue]
> If I want to get input from a file I use:
>
> #include <fstream>
>
> int main(){
>
> ifstream input("file.in",ios::in);[/color]

ios::in is not necessary.
[color=blue]
>
> int x;
> input >> x;
>
> }
>
> What about if I want to output to a file?
>[/color]

Very similar

int main()
{
ofstream output("file.out");
int x = 42;
outupt << x;
}

Basically use ofstream instead of ifstream and << instead of >>.

john
Greg Comeau
Guest
 
Posts: n/a
#3: Nov 22 '05

re: output to a file


In article <3HHdf.9457$8R6.871@newsfe1-gui.ntli.net>,
John Harrison <john_andronicus@hotmail.com> wrote:[color=blue]
>Gaijinco wrote:[color=green]
>> If I want to get input from a file I use:
>>
>> #include <fstream>
>>
>> int main(){
>>
>> ifstream input("file.in",ios::in);[/color]
>
>ios::in is not necessary.
>[color=green]
>>
>> int x;
>> input >> x;
>>
>> }
>>
>> What about if I want to output to a file?
>>[/color]
>
>Very similar
>
>int main()
>{
> ofstream output("file.out");
> int x = 42;
> outupt << x;
>}
>
>Basically use ofstream instead of ifstream and << instead of >>.[/color]

Gaijinco, PLEASE get yourself a good reference.
See http://www.comeaucomputing.com/booklist for some suggestions.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
John Harrison
Guest
 
Posts: n/a
#4: Nov 22 '05

re: output to a file


[color=blue]
>
> Gaijinco, PLEASE get yourself a good reference.
> See http://www.comeaucomputing.com/booklist for some suggestions.[/color]

Also there are good C++ standard library references on the internet. I
like http://www.dinkumware.com/refxcpp.html.

john
Closed Thread