Practical 9 20T28005 Krunal Pandya

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

PRACTICAL SET 9

1 Write a program to illustrate reading from Files .

Solution:#include <stdio.h>
#include <stdlib.h>

void main()
{
FILE *fptr;
char filename[15];
char ch;

printf("Enter the filename to be opened \n");


scanf("%s", filename);
/* open the file for reading */
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
ch = fgetc(fptr);
while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(fptr);
}
fclose(fptr);
}

Enrollment No.:
b. Write a program to illustrate writing in Files.

Solution:#include <stdio.h>
int main()
{
char ch;
FILE *fpw;
fpw = fopen("C:\\newfile.txt","w");

if(fpw == NULL)
{
printf("Error");
exit(1);
}

printf("Enter any character: ");


scanf("%c",&ch);

/* You can also use fputc(ch, fpw);*/


fprintf(fpw,"%c",ch);
fclose(fpw);

return 0;
}

Enrollment No.:
3. C program to create memory for int, char and float variable at run time.

Solution:

#include <stdio.h>
#include <stdlib.h>

int main()
{

int *ptr_1;

char *ptr_2;

float *ptr_3;

ptr_1 = (int*)malloc(1*sizeof(int));
ptr_2 = (char*)malloc(1*sizeof(char)*1);
ptr_3 = (float*)malloc(1*sizeof(float));

printf("\nEnter the value for integer pointer : ");


scanf("%d",ptr_1);

printf("\nEnter the value for char pointer : ");


scanf(" %c",ptr_2);

printf("\nEnter the value for float pointer : ");


scanf("%f",ptr_3);

printf("\nThe value stored in integer pointer is : %d",*ptr_1);

Enrollment No.:
printf("\nThe value stored in char pointer is : %c",*ptr_2);
printf("\nThe value stored in float pointer is : %f",*ptr_3);

free(ptr_1);
free(ptr_2);
free(ptr_3);

return 0;
}

4. Write a program to store an integer in block of memory space created by malloc and then modify
the same to store a large integer

Solution:

Enrollment No.:
 
*******

Enrollment No.:

You might also like