<< Chapter < Page Chapter >> Page >

Output:

File opened successfully. Contents: String: 111 222 3String: 33String: 444 555 6String: 66 String: 777 888 9String: 99Now closing file...

The main area of focus is the while loop - notice how I performed the check for the return of a NULL pointer. Remember that passing in char * variable, c as the first argument assigns the line read into c, which is printed off by printf. We specified a maximum number of characters to be 10 - we knew the number of characters per line in our text file is more than this, but we wanted to show that fgets reads 10 characters at a time in this case.

Notice how fgets returns when the newline character is reached - this would explain why 444 and 777 follow the word "String". Also, the tab character, \t, is treated as one character.

Other function:

Fseek() function

int fseek (FILE *fp, long int offset, int origin);

In the above prototype, there are two arguments:

  • fp: Pointer to a FILE object that identifies the stream.
  • offset: Number of bytes to offset from origin.
  • If offset>= 0: set the position indicator toward to the end of file,
  • If offset<0: set the position indicator toward to the beginning of file.
  • origin: Position from where offset is added. It is specified by one of the following constants defined in<cstdio>:
Constant Value Meaning
SEEK_SET 0 Beginning of file
SEEK_CUR 1 Current position of the file pointer
SEEK_END 2 End of file

This function sets the position indicator associated with the fp to a new position defined by adding offset to a reference position specified by origin. The End-of-File internal indicator of the file is cleared after a call to this function.

Return Value: If successful, the function returns a zero value. Otherwise, it returns nonzero value.

#include<stdio.h>int main () {FILE * fp; fp = fopen ( "myfile.txt" , "w" );fputs ( "This is an apple." , fp ); fseek ( fp , -8 , SEEK_END );fputs ( " sam" , fp ); fclose ( fp );return 0; }
Got questions? Get instant answers now!

After this code is successfully executed, the file myfile.txt contains:

This is a sample.
#include<stdio.h>int main () {FILE * fp; fp = fopen ( "myfile.txt" , "w" );fputs ( "This is an apple." , fp ); fseek ( fp , 9 , SEEK_SET );fputs ( " sam" , fp ); fclose ( fp );return 0; }
Got questions? Get instant answers now!

After this code is successfully executed, the file myfile.txt contains:

This is a sample.

Rewind() function

void rewind (FILE *fp);

This function sets the current position indicator associated with fp to the beginning of the file. A call to rewind is equivalent to:

fseek (fp, 0, SEEK_SET);

except that, unlike fseek, rewind clears the error indicator.

On streams open for update (read+write), a call to rewind allows to switch between reading and writing.

#include<stdio.h>#include<conio.h>int main () {char str [80];int n; FILE * fp;fp = fopen ("myfile.txt","w+"); for ( n='A' ; n<='Z' ; n++) fputc ( n, fp);rewind (fp); n=0;while (!feof(fp)) {str[n]= fgetc(fp);n++; }fclose (fp); printf ("I have read: %s \n",str);getch(); return 0;}
Got questions? Get instant answers now!

A file called myfile.txt is created for reading and writing and filled with the alphabet. The file is then rewinded, read and its content is stored in a buffer, that then is written to the standard output:

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to computer science. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10776/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?

Ask