Saturday, May 14, 2011

Writing and reading strings in files using C Language


C Language also provides fputs() and fgets() functions to handle strings with files.
fputs()
Syntax of fputs() - fputs(< string >, < file pointer >)
fgets()
Syntax of fgets() - fgets(< string >, < max_no_of_characters >, < file pointer >)

Here is a program to demonstrate the use of fputs and fgets function

#include < stdio.h >
#include < string.h >
 void main()
{
FILE *ptr;
char str[80], answer='y';
clrscr();
printf("Program to create a file containing text and then reading the text \n");
ptr=fopen("dd1.txt","w");
while((toupper(answer)) != 'N')
{
printf("Enter the text :");
gets(str);
fflush(stdin);
fputs(str,ptr); /* Writing text (string) into the file */
printf("\n Any more text to add Y/N ?");
scanf("%c", &answer);
printf("\n");
fflush(stdin);
}
printf("Writing of text into the file complete \n");
fclose(ptr);
ptr = fopen("dd1.txt", "r");
printf("The text entered in the file was as follows :\n");
while(!feof(ptr))
{
fgets(str, 80,ptr); /* Reading 80 characters at a time */
printf("%s",str);
}
fclose(ptr);
printf("\nReading of text from the file complete \n\n");
getch();
}

Different C file handling functions for moving around in a file
fseek()
ftell()
rewind()

fseek()
fseek() function in C Language can be used to move forward or backward in a file.
Syntax of fseek() - fseek(< file pointer >, < offset >, < position > );
offsetis the number of bytes we want to move from the current position in the file. position indicates the location in the file from where on wishes to move.
position can take only three values which are
0 - meaning movement from the beginning of the file
1 - meaning movement from the current position in the file
2 - meaning movement from the end of the file.

Different operations of fseek () function

fseek(ptr,0L,0) -  same as rewind () function
 fseek(ptr,m,0)- the pointer moves to the m+1 bytes from the beginning
fseek(ptr,m,1) - pointer moves by m bytes from the current position of the file
fseek(ptr,-m,1) - pointer moves m bytes backward from it's current position
fseek(ptr,-m,2) moves the pointer m bytes backward from the end

ftell()
While reading a file, ftell () function returns the current position of the file in long. The syntax is: ftell (p), while p is the pointer.

rewind()
Syntax of rewind() - rewind(<file pointer>);

Program to demonstrate the above functions

#include < stdio.h >
void main()
{
FILE *ptr;
char str[80];
long int no;
clrscr();
printf("Program to illustrate the use of rewind() function \n\n");
ptr=fopen("dd1.txt","r");
if(ptr== NULL)
{
printf("Can not open the file, terminating .... \n");
getch();
return;
}
printf("The following is the text in the file :\n");
while(!feof(ptr))
{
fgets(str, 80,ptr);
puts(str);
}
printf("The current offset in the file is %ld \n", ftell(ptr));
printf("At this stage, the end of file has been reached, now using rewind() \n");
rewind(ptr);
printf("The following is the text in the file after using rewind() :\n");
while(!feof(ptr))
{
fgets(str,80,ptr);
puts(str);
}
printf("The current offset in the file is %ld \n", ftell(ptr));
fclose(ptr);
getch();
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner