Pages

Saturday, December 4, 2010

Writing a file - String by String

Writing char by char to a file will take a lot of time. So it will be much better if we can write a whole string to a file in one go. To write a complete string to file we will use the function "fputs()". This function will take two arguments, first the string to be written and second the file pointer.

Syntax
fputs(char [],filepointer);

Example

1. fputs("MyName",fp);


2. char str[]="MyName";


fputs(str,fp);


3. char str[10];

printf("\nEnter any string ");
scanf("%s",&str);
fputs(str,fp);

In the first example, we are writing a fixed string "MyName" to the file pointed by the pointer fp. The second example is also doing the same the difference being that instead of writing the string directly, we have first saved the string "MyName" to a variable "str" and then have written the value of "str" to the file. In the third example, we are writing the string in the file, by asking it from the user.

Example 1 : Writing a string to a file using fputs()

#include
#include
void main()
{
FILE *fp; // Line 1
fp=fopen("names","w"); // Line 2
char name[15]; // Line 3
printf("\nEnter name "); // Line 4
scanf("%s",&name); // Line 5
fputs(name,fp); // Line 6
printf("\nName saved"); // Line 7
fclose(fp); // Line 8
}

Description

This is the simplest use of "fputs()" , after asking the name of the user in line no 4 and 5, we have written it in the file using the "fputs()" function in the line no. 6. The first argument is the string that we want to write and second is the file pointer. Line 7 displays the confirmation that the data has been saved.

What if you want to write names of several persons in the same file? fputs() can write one string at a time. So we will use the same method again - put the "fputs()" inside a loop.

Example 2 : Writing multiple strings to a file using fputs()


#include
#include
void main()
{
FILE *fp; // Line 1
fp=fopen("list","w"); // Line 2
char name[15]; // Line 3
char choice; // Line 4
do // Line 5
{
printf("\nEnter name "); // Line 6
scanf("%s",&name); // Line 7
fputs(name,fp); // Line 8
printf("\nName saved"); // Line 9
printf("\nWould you like to add another name (y/n) ? : "); // Line 10
scanf("%c",&choice); // Line 11
}while(choice=='y' || choice=='Y'); // Line 12
fclose(fp); // Line 13
}

Description
Line 4
Declares an extra variable "choice" which will accept the user's choice, "y" or "n".
Line 5
Line 5 contains a "do" loop inside which the user is prompted to enter name of a person at line no. 6 and 7
Line 8
The function "fputs()" will put(write) the name in the file pointer "fp".
Line 9
A confirmation message "Name saved" is shown to the user.
Line 10 and Line 11
Once the first name has been saved in the file, we are asking the user to confirm whether the user wants to continue adding the names or not. The user will enter either 'y' or 'n' for "yes" or "no".
Line 12
If the user enters 'y' for yes the condition inside the while becomes "true" and the loop runs again from line no. 5. Line no. 6 to 11 are repeated, the user enters another name which is again saved in the file. The user will keep entering the names in the file and the program will ask the user to continue or not. This process will continue till the user enters 'n' for "No".


To read the files created in example 1 or 2, please follow the instructions given in the post "How to read the file".

0 comments:

Post a Comment