Input and output to and from files is identical to that at the command line, except the fprintf and fscanf functions are used and they require another argument. This additional argument is called a file pointer. In order to write two floating point numbers to a file, you first need to declar the file pointer with the FILE type, and you need to open it, as in float x=1, y=2; FILE *file; file = fopen(‘‘file.txt’’,’’w’’); fprintf(file,’’%f %f\n’’,x,y); fclose(file); The function fprintf is identical to the printf function, except now we see it has another argument file, which is a pointer to the file. Before you use the file variable, you need to open the file with file = fopen(‘‘file.txt’’,’’w’’); This opens up the file ‘‘file.txt’’ and the ‘‘w’’ which is the mode and indicates how the file will be used. The following three modes are allowed: Mode String Open for reading “r” Open for writing “w” Open and append “a” When you are done with the file, you close it ...
Learn C++ Programming fast and easy, find C++ Program Codes