You are on page 1of 7

JUNE 10, 2021

NAME: -AYUSH ARVINDKUMAR RANA

ROLL NO.: -122

SEMESTER: -2nd

DIVISION: -B

SUBJECT: - FUNDAMENTALS OF
PROGRAMMING USING C-2

PROBLEMSHEET-4

AYUSH RANA
1. Write a program to display content of file.

#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
printf("Enter name of a file you wish to see\n");
gets(file_name);
fp = fopen(file_name, "r");
if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(0);
}
printf("The contents of %s file are:\n", file_name);
while((ch = fgetc(fp)) != EOF)
{
printf("%c", ch);
}
fclose(fp);
return 0;
}

2. Write a program to copy content of one file into another file.

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1, *fp2;
char c, filename[100];
printf("Enter name of file to copy\n");
gets(filename);
fp1= fopen(filename, "r");
if( fp1 == NULL )
{
printf("Cannot open file %s\n",filename);
exit(0);
}
printf("Enter name of target file\n");
gets(filename);

fp2 = fopen(filename, "w");

if( fp2 == NULL )


{
printf("Cannot open file %s\n",filename);
exit(0);
}
c=fgetc(fp1);
while(c != EOF )
{
fputc(c, fp2);
c=fgetc(fp1);
}
printf("File copied successfully.\n");
fclose(fp1);
fclose(fp2);
return 0;
}

3. Write a program to write 5 students records (rollno, name,


email) into the file “student.dat”.

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct student
{
int rollno;
char name[20];
char email[50];
};
int main()
{
FILE *fp,*fp1;
int i;
fp=fopen("student.dat","w");
if(fp==NULL)
{
fprintf(stderr,"\n Error oprned file");
exit(1);
}
struct student a[5];
for(i=0;i<5;i++)
{
printf("enter rollno of student %d:",i);
scanf("%d",&a[i].rollno);
printf("enter name of student %d:",i);
scanf("%s",a[i].name);
printf("enter Email of student %d:",i);
scanf("%s",a[i].email);
}
for(i=0;i<5;i++)
{
fwrite(&a[i],sizeof(struct student),1,fp);
}
fclose(fp);
return 0;
}

4. Write a program to read above “student.dat “ file and display all


records.

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct student
{
int rollno;
char name[20];
char email[50];
};
int main()
{
FILE *fp,*fp1;
int i;
fp=fopen("student.dat","w");
if(fp==NULL)
{
fprintf(stderr,"\n Error oprned file");
exit(1);
}
struct student a[5];
for(i=0;i<5;i++)
{
printf("enter rollno of student %d:",i);
scanf("%d",&a[i].rollno);
printf("enter name of student %d:",i);
scanf("%s",a[i].name);
printf("enter Email of student %d:",i);
scanf("%s",a[i].email);
}
for(i=0;i<5;i++)
{
fwrite(&a[i],sizeof(struct student),1,fp);
}
fclose(fp);
fp1=fopen("student.dat","r");
if(fp1==NULL)
{
fprintf(stderr,"\n Error oprned file");
exit(1);
}
while(fread(&a[0],sizeof(struct student),1,fp))
{
printf("rollno=%d name=%s
email=%d\n",a[0].rollno,a[0].name,a[0].email);
}
fclose(fp1);
return 0;
}
5. Write a program to find maximum of integers entered in a
command line.

#include<stdio.h>
int main(int argc, char *argv[])
{
int a, b, c;
if (argc < 4 || argc > 5)
{
printf("enter 4 arguments only eg.\"filename arg1 arg2 arg3!!\"");
return 0;
}

a = atoi(argv[1]);
b = atoi(argv[2]);
c = atoi(argv[3]);

if (a < 0 || b < 0 || c < 0)


{
printf("enter only positive values in arguments !!");
return 1;
}

if (!(a != b && b != c && a != c))


{
printf("please enter three different value ");
return 1;
}
else
{

if (a > b && a > c)


printf("%d is largest", a);

else if (b > c && b > a)


printf ("%d is largest", b);
else if (c > a && c > b)
printf("%d is largest ",c);
}
return 0;
}

You might also like