<< 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

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
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