C/C++, KDEVELOP

Basics

Program Flow

Functions

Classes and Objects

  1. Declaration of a Class
  2. Constructing Objects
  3. I/O Streams

X Window

KDevelop


-----------------
© Alfred Nussbaumer
Updated:
09 December 2021
-----------------

Input/Output-Streams

Opening Textfiles - Read

We use the textfile "names.txt" which contains some names:

micky mouse
morty mouse
minnie mouse
maxi mouse
monti mouse

The following program reads all data from the textfile and writes it to the standard output:

#include <iostream.h>
#include <fstream.h>

int main() {
  char linebuffer[80];
  ifstream ifs("names.txt", ios::in);
  while (!ifs.eof()) {
    ifs.getline(linebuffer, 80, '\n');
    cout << linebuffer << endl;
  }
  return 0;
}

So we get the following output:

micky mouse
morty mouse
minnie mouse
maxi mouse
monti mouse

Creating Textfiles - Write, Append

The following example demostrates how to append new lines to an existing textfile.


© Alfred Nussbaumer, Weblog "MiniNuss"