0% found this document useful (0 votes)
37 views

C Language

The document contains code snippets demonstrating the use of various file input/output functions in C including fputc(), putw(), fprintf(), fputs(), fwrite(), and fputc() for writing data to files. Each code block opens a file, writes data to the file using the specified function, and then closes the file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

C Language

The document contains code snippets demonstrating the use of various file input/output functions in C including fputc(), putw(), fprintf(), fputs(), fwrite(), and fputc() for writing data to files. Each code block opens a file, writes data to the file using the specified function, and then closes the file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

fputc();

#include <stdio.h>
int main()
{
int ch = 0;
FILE *fp = NULL;
//create a file
fp = fopen("aticleworld.txt", "w");
if(fp == NULL)
{
printf("Error in creating the file\n");
exit(1);
}
//Write A to Z in file
for(ch =65 ; ch <= 90 ; ++ch)
{
fputc(ch, fp);
}
//close the file
fclose(fp);
printf("A t0 Z written to the created file\n");
return 0;
}
putw()
#include <stdio.h>

int main() {
FILE *fp;
int num;

fp = fopen("file.txt", "w");
printf("Enter any number:\n");
scanf("%d", &num);
putw(num, fp);
fclose(fp);
printf("%d\n", num);

return 0;
}
fprintf():
#include <stdio.h>
int main()
{
//file pointer
FILE *fp = NULL;
int i = 0;
char name[40] = {0};
//create a text file
fp = fopen ("world.txt", "w");
if(fp == NULL)
{
printf("File is not created\n");
exit(1);
}
//three times asking for
//student name
for (i=1 ; i<4 ; i++)
{
puts ("Enter the student name: ");
//Get input from the user
gets (name);
//Write formated data into the file
fprintf (fp, "%d. Name = [%s]\n",i,name);
}
//close the file
fclose(fp);
return 0;
}

fputs();
#include <stdio.h>
int main()
{
//file pointer
FILE *fp = NULL;
fp = fopen("world.txt", "w");
if(fp == NULL)
{
printf("Error in creating the file\n");
exit(1);
}
fputs("Hello There, I hope this example will help!",fp);
//close the file
fclose(fp);
printf("File has been created successfully\n");
return 0;
}
fwrite():
#include <stdio.h>
#include <string.h>

int main()
{

// Creating a filepointer
FILE *file;

// Saving the text message


char greeting[] = "Hello Ninjas! Welcome to Coding Ninjas";

// Creating a file with name codingninjas


file = fopen("codingninjas.txt", "w");

// If file is not created


if (file == NULL)
{
printf("Some Error occurred while opening file.");
}

else
{
fwrite(greeting, sizeof(char), strlen(greeting), file);
}

// Closing the file


fclose(file);

// Printing the message for user


printf(" Your File has been created.\n");

return 0;
}
#include <stdio.h>
int main()
{
//Ascii value of 1
int ch = 49;
FILE *fp = NULL;
//Open file in append mode
fp = fopen("aticleworld.txt", "a");
if(fp == NULL)
{
printf("Error in creating the file\n");
exit(1);
}
//Write 1 in file
fputc(ch, fp);
//close the file
fclose(fp);
printf("Append 1 to the created file\n");
return 0;
}

You might also like