Beruflich Dokumente
Kultur Dokumente
File Open
Create
for
File?
"a"
Appending
Yes
Notes
Appending
Yes
and reading
"r"
Reading
Information is added
to the end of the file.
No
"r+"
Reading
No
and writing
"w"
Writing
Yes
"w+"
Writing and
reading
Yes
}
Line 6 creates the file handle, fh. Its a pointer. The pointer
stores the result of the fopen() function from Line 8. The file
hello.txt is created using the "w" (write) mode. If the file
exists, its overwritten.
The if statement checks to confirm that the file was properly
opened. If it wasnt, the value of fh is NULL, and
appropriate action is taken.
The fprintf() function writes text to the file at Line 14. The
format is the same as for printf(), although the file handle
must be included as an argument.
Finally, Line 15 closes the file by using the fclose() function.
This statement is a required step for any file access
programming.
Exercise 1: Copy the source code from Write That File into
your editor. Build and run the program.
The programs output goes to a file, so you dont see
anything displayed when its run. Use your computers file
browser to locate the file and open it. Or you can write a
program that reads the text from that same file.
Line 9 in Read That File opens the file hello.txt for reading
only. The file must exist or else an error occurs.
The while loop at Line 15 displays the files contents one
character at a time. The fgetc() function reads one
character from the file identified by handle fh. That
character is stored in variable ch. The result is compared
with the End of File constant. When theres a match, the file
has been completely read and the while loop stops.
Otherwise, the character thats read is displayed on Line
16.
Exercise 2: Create a new program by using the source
code shown in Read That File. Build and run.
The program displays the contents of the file created by
Exercise 1; otherwise, you see the error message.
Exercise 3: Modify your source code from Exercise 1 to
write a second string to the file. Add the following statement
after Line 14:
fputs("MyCprogramwrotethisfile.\n",fh);
Unlike using the puts() statement, you need to specify a
newline character for fputs() output. Further, the file handle
argument appears after the string, which is unusual for a C
language file function.
}
The fgets() function appears at Line 15 as the while loops
condition. Thats because fgets() returns a pointer to the
string thats read, and when no string is read, it returns a
NULL. That value stops the loop. Otherwise, the printf()
function at Line 16 displays the input.
The buffer size and the size of characters read in Gulping
Strings of Text are identical. Thats because the \0 at the
end of the string is read from the file and isnt interpreted as
an end-of-file marker.
Exercise 4: Type the source code from Gulping Strings of
Text into your editor. Build and run.
This lesson is about using random access files in C and the
following lesson will look at working with text files. Apart
from the simplest of applications, most programs have to
read or write files. Maybe it's just for reading a config file, or
a text parser or something more sophisticated. The basic
file operations are
file.
argv[]) {
const char *
filename="test.txt";
const char *
int byteswritten=0;
fopen(filename, "wb") ;
FILE * ft=
if (ft) {
fwrite(mytext,sizeof(char),strlen(mytext
), ft) ;
fclose( ft ) ;
printf("len of mytext = %i
",strlen(mytext)) ;
return 0; }
1.
Sequentially
2.
Randomly
if we want to access the forty fourth record then first forty three record read sequentially
to reach forty four record. In random access data can be accessed and processed
directly .There is no need to read each record sequentially. If we want to access a
particular record random access takes less time than the sequential access.
C supports these function for random access file.
1.
fseek( ) Function
2.
ftell ( ) Function
offset is a long integer which specifies the number of bytes from origin where the
position indicator should be placed;
/*
* Program to understand the use of fseek
function
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct emprecord
{
char name[30];
int age;
float sal;
}emp;
void main()
{
int n;
FILE *fp;
fp=fopen("employee.dat","rb");
if (fp==NULL)
{
printf("/n error in opening file");
exit(1);
}
printf("enter the record no to be read");
scanf("%d",&n);
fseek(fp,(n-1)*sizeof(emp),0);
freed(&emp,sizeof(emp),1,fp);
printf("%s\t,emp.name);
printf("%d\t",emp.age);
printf("%f\n",emp.sal);
fclose(fp);
getch();
};