file input and output in c program
#include <stdio.h>
typedef struct employees
{
int salary;
char name[20];
} emp;
int main()
{
FILE *ptr;
ptr = fopen("a.text", "w");
emp e1, e2;
printf("enter the name of e1:");
scanf("%s", e1.name);
printf("enter the salary of e1:");
scanf("%d", &e1.salary);
printf("enter the name of e2:");
scanf("%s", e2.name);
printf("enter the salary of e2:");
scanf("%d", &e2.salary);
fprintf(ptr, "%s\t,", e1.name);
fprintf(ptr, "%d\n", e1.salary);
fprintf(ptr, "%s\t,", e2.name);
fprintf(ptr, "%d", e2.salary);
fclose(ptr);
return 0;
}
#include <stdio.h>
int main()
{
FILE *ptr;
char c;
ptr = fopen("getdemo.text", "r");
c = fgetc(ptr);
while (c != EOF)
{
printf("%c", c);
c = fgetc(ptr);
}
fclose(ptr);
return 0;
}
#include <stdio.h>
int main()
{
FILE *ptr1;
FILE *ptr2;
ptr1 = fopen("single.text", "r");
ptr2 = fopen("double.text", "w");
int num, num2;
fscanf(ptr1, "%d", &num);
num2 = 2 * num;
fprintf(ptr2, "%d", num2);
return 0;
}
# include<stdio.h>
int main(){
FILE *ptr;
ptr=fopen("upen1.text","w");
int num=25;
fprintf(ptr,"the number is %d \n",num);
fprintf(ptr,"thanks for using it ");
fclose(ptr);
return 0;
}
#include <stdio.h>
int main()
{
int num, num1;
FILE *ptr;
ptr = fopen("upen.text", "r");
if (ptr == NULL)
{
printf("this file doesn't exist");
}
else
{
fscanf(ptr, "%d%d", &num, &num1);
printf("The number is %d \n", num);
printf("The number is %d \n", num1);
fclose(ptr);
}
return 0;
}
#include <stdio.h>
int main()
{
FILE *ptr;
char c;
ptr = fopen("fputdemo.text", "w");
c = fputc('M', ptr);
c = fputc('O', ptr);
c = fputc('B', ptr);
c = fputc('I', ptr);
c = fputc('L', ptr);
c = fputc('E', ptr);
fclose(ptr);
return 0;
}
#include <stdio.h>
int main()
{
int mul[10];
int num;
FILE *ptr;
ptr = fopen("Mul.text", "w");
printf("Enter the number of which table U want:");
scanf("%d", &num);
for (int i = 0; i < 10; i++)
{
mul[i] = num * (i + 1);
}
for (int i = 0; i < 10; i++)
{
fprintf(ptr, "%d*%d=%d\n", num, (i + 1), mul[i]);
}
return 0;
}
#include <stdio.h>
int main()
{
FILE *ptr;
ptr = fopen("a.text", "r");
int num[2];
fscanf(ptr, "%d%d%d", &num[0], &num[1], &num[2]);
printf("%d \n", num[0]);
printf("%d \n", num[1]);
printf("%d \n", num[2]);
return 0;
}
#include <stdio.h>
int main()
{
FILE *ptr;
ptr = fopen("Table.text", "w");
int num;
printf("Enter the number:");
scanf("%d", &num);
for (int i = 0; i < 10; i++)
{
fprintf(ptr, "%d*%d=%d\n", num, (i + 1), num * (i + 1));
}
fclose(ptr);
fprintf("sucessfuly generated table of %d", num);
return 0;
}
#include <stdio.h>
int main()
{
FILE *ptr1;
FILE *ptr2;
ptr1 = fopen("file.text", "r");
ptr2 = fopen("file1.text", "w");
char c;
c = fgetc(ptr1);
while (c != EOF)
{
fputc(c, ptr2);
fputc(c, ptr2);
c = fgetc(ptr1);
}
fclose(ptr1);
fclose(ptr2);
return 0;
}