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

Name: - Ayush Arvindkumar Rana ROLL NO.: - 122 Semester: - 2 Division: - B Subject: - Fundamentals of Programming Using C-2

This document contains 5 programming problems and their solutions in C language. The problems include: 1) Writing a program to display the contents of a file. 2) Writing a program to copy the contents of one file to another. 3) Writing a program to write records of 5 students (roll number, name, email) to a data file. 4) Writing a program to read the student data file and display the records. 5) Writing a program to find the maximum of integers entered through command line arguments. For each problem, the full code solution in C is provided.

Uploaded by

122AYUSH RANA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Name: - Ayush Arvindkumar Rana ROLL NO.: - 122 Semester: - 2 Division: - B Subject: - Fundamentals of Programming Using C-2

This document contains 5 programming problems and their solutions in C language. The problems include: 1) Writing a program to display the contents of a file. 2) Writing a program to copy the contents of one file to another. 3) Writing a program to write records of 5 students (roll number, name, email) to a data file. 4) Writing a program to read the student data file and display the records. 5) Writing a program to find the maximum of integers entered through command line arguments. For each problem, the full code solution in C is provided.

Uploaded by

122AYUSH RANA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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