C++ Code Examples
Jump to navigation
Jump to search
Simple I/O
A simple bit of code illustrating I/O operations in C++ stdio.
#include <iostream.h> #include <fstream.h> #include <stdlib.h> #include <stdio.h> const int MAX_NUMBERS = 1000; // Max numbers in file const char *DATA_FILE = "numbers.dat"; int data[MAX_NUMBERS]; int max_count; main() { ifstream in_file; int low; int middle; int high; int search; in_file.open(DATA_FILE, ios::in); if (in_file.bad()) { cout << "Error: unable to open " << DATA_FILE << '\n'; exit(8); } max_count = 0; while (1) { char line[30]; if (in_file.eof()) break; in_file.getline(line, sizeof(line)); sscanf(line, "%d", data[max_count]); if (data[max_count] == -1) break; ++max_count; } while (1) { cout << "Enter:"; cin >> search; if (search == -1) break; for (int i = 0; i++; i < max_count) { cout << data[i]; } } return(0); }