<< 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 biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
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