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

Dani AI

Generated

Good questions. Two quick clarifications up front: as hinted, std::getline is what you want for sentences (it reads an entire line, including spaces). And is right that reading with operator>> splits on whitespace, so words will print jammed together. Finally, fin/fout are just common variable names for std::ifstream and std::ofstream objects, not different types.

Here is a small, robust pattern that appends a sentence to the file and then prints the file exactly as written, preserving spaces and newlines:

#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::string line;

    std::cout << "Enter a sentence: ";
    std::getline(std::cin, line);         // reads spaces too

    std::ofstream out("biscuit.txt", std::ios::app);
    if (!out) { std::cerr << "Cannot open biscuit.txt for writing\n"; return 1; }
    out << line << '\n';                  // append plus newline so entries do not run together

    std::ifstream in("biscuit.txt");
    if (!in) { std::cerr << "Cannot open biscuit.txt for reading\n"; return 1; }

    std::cout << "\nCurrent file contents:\n";
    while (std::getline(in, line)) {
        std::cout << line << '\n';
    }
}

Notes you can use right away:

  • If you ever mix >> with std::getline, eat the leftover newline: std::getline(std::cin >> std::ws, line);.
  • Appending: std::ios::app always writes at the end. Different from std::ios::ate, which opens at end but lets you seek elsewhere afterward.
  • Prefer '\n' over std::endl unless you specifically need a flush.
  • No need to call close() explicitly; the destructors close the files automatically.
  • Avoid system("pause"); if you want a pause in a console, do std::cout << "Press Enter..."; std::cin.get();.

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.