After opening a file, check if it succeeded with if(file) or if(file.is_open()). Both return false if opening failed. Without this check, reading from a failed stream gives undefined behavior.
Common failures: file doesn't exist (for ifstream), no write permissions (for ofstream), or disk full. You'll get no error message unless you check manually with perror() or strerror(errno).
Always validate before proceeding. A simple pattern: if(!file) { cerr << "Failed to open\n"; return 1; }. Programs that skip this check crash mysteriously or produce corrupt output files.