2.3. Opening and Reading from a File
Let's say that you want to write a program that performs the simplest possible file operation: you want to open a file, read from it, and write its contents to the screen. The easiest way to do this is to use the I/O library that ships with C or C++. Listings 2.1 and 2.2 demonstrate a simple file-reading program in straight C, and then in C++. To learn how to compile this code, see Appendix A.
Code Listing 2.1. A C program that reads from a file and writes its contents to the screen
// file1.cpp #include <windows.h> #include <stdio.h> void main() { char filename[MAX_PATH]; char c; FILE *f; // get file name printf("Enter filename: "); gets(filename); // open the file f = fopen(filename, "r"); if (f) // read ... |
Get Win32 System Services: The Heart of Windows® 98 and Windows® 2000 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.