C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C File Write

The following functions are used to write data into a file.

  • fprintf()
  • fputc()
  • fputs()

Files - fprintf()

The fprintf() function is used to write sequence of characters, integers, floats, etc. by using appropriate format specifier specified data type.

C program - fprintf()

Lets try to code using fprintf() and have some fun.

fprintf.c
#include <stdio.h>
int main()
{
FILE *fptr;     //File pointer declaration
char name[40];
int age;
fptr = fopen("record.txt", "w ");     //the function fopen opens the record text file
printf("\nEnter your name : ");
scanf("%s", name);
printf("\nEnter your age : ");
scanf("%d",&age);
fprintf(fptr,"%s", name);    //the entered name is written in to the file
fprintf(fptr,"%d ", age);    //the entered age is written to the file
fclose(fptr);     //the function closes the opened file
return 0;
}
  • Enter your name : siva
  • Enter your age : 21

Note:

The program shows that the fprintf function is used to store the name and age into the file record.txt.

Files - fputc()

The fputc() function also used to write into an opened file. But it writes a single character at a time.

C program - fputc()

Lets try to code using fputc() and have some fun.

fputc.c
#include <stdio.h>
int main()
{
FILE *fptr;     //File pointer declaration
char ch;
fptr = fopen("record.txt", "a ");     //the function fopen opens the record text file
printf("\nEnter characters with a space to write into a file press 'y 'to stop writing : ");
scanf("%c",&ch);
while(ch != 'y')
{
fputc(ch, fptr);
scanf("%c",&ch);
}
fclose(fptr);     //the function closes the opened file
return 0;
}
  • Enter characters with a space to write into a file press 'y 'to stop writing : f u n y

Note:

The program shows that the fputc function is used to store entered character through variable ch into the file record.txt.

Files - fputs()

The fputs() function used to write sequence of characters into file without the opened file. The fputs() function is used write a string into a file.

C program - fputs()

Lets try to code using fputs() and have some fun.

fputs.c
#include <stdio.h>
int main()
{
FILE *fptr;     //File pointer declaration
char name[40], city[40];
fptr = fopen("record.txt", "w ");     //the function fopen opens the record text file
printf("\nEnter your name : ");
scanf("%s", name);
printf("\nEnter your city : ");
scanf("%s", city);
fputs(name, fptr);    //the entered name is written in to the file
fputs(city, fptr);    //the entered age is written to the file
fclose(fptr);     //the function closes the opened file
return 0;
}
  • Enter your name : Sundar
  • Enter your city : Delhi

Note:

The program shows that the fputs function is used to store the name and age into the file record.txt.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report