hi, have a little problem using fstream. first here is the code...

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    string word;
 
 
    ofstream outl("biscuit.txt", ios::out);
    cin>>word;
    outl<<word;
    system("pause");
    return 0;
}

using this, i can type a word into the input screen and it saves whatever word i typed into the biscuit.txt file. now my problem is... how do i make it save a whole sentence (as many words as i wish... i think it has to do with the string ). secondly, how do i write a program that can read from the file and print it on the screen? and also how do i add data into the file without overwriting the previous data...( the book i'm using isnt too clear on that)
thanks

p.s.
is using ifstream and ofstream the same as using fin and fout ? cuz i cant find enough info about fin or fout anywhere

Recommended Answers

All 4 Replies

use getline()

use getline()

ok thanks. but where do i place the get line?

how do i make it save a whole sentence

replace

cin>>word;

with

getline( cin, word );

how do i write a program that can read from the file and print it on the screen?

ifstream fin("biscuit.txt", ios::in);
while( fin>>word )
{
    cout<<word;
}

how do i add data into the file without overwriting the previous data

ofstream outl("biscuit.txt", ios::app);

will open the file in append mode.

is using ifstream and ofstream the same as using fin and fout? cuz i cant find enough info about fin or fout anywhere

What is fin and fout? Are you sure that you are not confusing two fstream variables?

replace

cin>>word;

with

getline( cin, word );
ifstream fin("biscuit.txt", ios::in);
while( fin>>word )
{
    cout<<word;
}
ofstream outl("biscuit.txt", ios::app);

will open the file in append mode.

What is fin and fout? Are you sure that you are not confusing two fstream variables?

hi i figure it out thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.