After all what is the use of the program if you can't save output to a disk.
Inorder to do this we have to use a pointer. The pointer in this case is FILE. The syntax to declare a file is :FILE*file_ponter;
The link between your program, the file and the computer is established with the fopen() function using the syntax shown below:
pointer=fopen("FILENAME","mode");
For example to create a file by the name cd.dat we do the following:
FILE*cdfile;
cdfile=fopen("CD>DAT","w");
If you will be reading from the file above use "r" instead "w" in the
second sentence.
In order to rpint information use the following command:
FILE*cdfile;
cdfile=fopen("PRN","w");
A file is closed by using the fclose() command.Next we will look at an exam ple of reading from a file.
example-13
#include "stdio.h"
main()
{
FILE*fp;
int letter;
if((fp=fopen("MYFILE","r"))==NULL)
{
puts("Cannot oepn the file");
exit();
}
while((letter=fgetc(fp)) !=eof)
printf("%c",letter);
fclose(fp);
}
The eof statement means end of file and this is included in the stdio.h header file which was declared at the start of the example. The stdio.h header file is one of many that comes with your compiler. So check your compiler specifics for other header files which will help perform other functions.
Inorder to do this we have to use a pointer. The pointer in this case is FILE. The syntax to declare a file is :FILE*file_ponter;
The link between your program, the file and the computer is established with the fopen() function using the syntax shown below:
pointer=fopen("FILENAME","mode");
For example to create a file by the name cd.dat we do the following:
FILE*cdfile;
cdfile=fopen("CD>DAT","w");
If you will be reading from the file above use "r" instead "w" in the
second sentence.
In order to rpint information use the following command:
FILE*cdfile;
cdfile=fopen("PRN","w");
A file is closed by using the fclose() command.Next we will look at an exam ple of reading from a file.
example-13
#include "stdio.h"
main()
{
FILE*fp;
int letter;
if((fp=fopen("MYFILE","r"))==NULL)
{
puts("Cannot oepn the file");
exit();
}
while((letter=fgetc(fp)) !=eof)
printf("%c",letter);
fclose(fp);
}
The eof statement means end of file and this is included in the stdio.h header file which was declared at the start of the example. The stdio.h header file is one of many that comes with your compiler. So check your compiler specifics for other header files which will help perform other functions.
No comments:
Post a Comment