Next: Pointers
Up: C Primer CS 1005
Previous: Functions
Simple I/O:
char no[7] = "number";
cout << 3 << " is a " << no;
int i;
char ch;
char str[20];
cin >> i >> ch >> str;
File I/O:
#include <iostream.h>
#include <fstream.h>
char (database[80])[30];
ifstream infile;
infile.open("input.txt");
int i = 0 ;
while (infile.peek() != EOF) {
infile >> database[i];
i++;
}
infile.close();
ofstream outfile;
outfile.open("outfile.txt");
int size = i; // copy variable i from above into something meaningful
for (i = 0; i < size; i++)
outfile << database[i] << endl;
outfile.close();
- never forget to close a file after you are done reading/writing
- other useful functions of I/O-stream are in the book in Appendix
F
- reading strings with
>>
does not work if the string has
whitespaces. In that case, use
infile.getline(charVariable, maxLength)
.
This will also prevent
buffer overflow.
Andreas Koeller
2000-06-04