BoardGuess.com

MCS-011 Problem Solving and Programming

Q1. Write a program to create a file in file handling. Solution: #include

#include

void main()

{

FILE *fp;

char say[80];

clrscr();

fp=fopen("test.txt","w");

if(fp==NULL)

{

printf("Cannot open file");

exit(0);

}

printf("Write the short assay:");

while(strlen(gets(say))>0)

{

fputs(say,fp);

}

fclose(fp);

getch();

}

Q2. Write a program to read the contents of one file and copy it into another file and count the number of character in that file. Solution: #include

#include

void main()

{

FILE *fp, *ft;

int noc=0;

char ch, ch1;

clrscr();

fp=fopen("ejaz/test.txt","r");

if(fp==NULL)

{

printf("Cannot open file");

exit(0);

}

ft=fopen("ejaz/test2.txt","ar");

if(ft==NULL)

{

printf("Cannot open file");

exit(0);

}

while(1)

{

ch=fgetc(fp);

if(ch==EOF)

break;

else

fputc(ch,ft);

}

fclose(ft);

ft=fopen("ejaz/test2.txt","a+");

if(ft==NULL)

{

printf("Cannot open file");

exit(0);

}

ch1=fgetc(ft);

while(ch1!=EOF)

{

noc++;

ch1=fgetc(ft);

}

printf("nThe number of character in the file is :%d",noc);

fflush(ft);

fclose(ft);

fclose(fp);

getch();

}

Q3. Write a program to open a file and read its content. Solution: #include

#include

void main()

{

FILE *fp;

char ch;

clrscr();

fp=fopen("ejaz/test2.txt","r");

if(fp==NULL)

{

printf("Cannot open file");

exit(0);

}

else

do{

putchar(ch=fgetc(fp));

}while(ch!=EOF);

fclose(fp);

getch();

}



Start Discussion!
*
* (Will not be published)
* (First time user can put any password, and use same password onwards)
*
Start a new topic: (If you have any question related to this post/category then you can start a new topic and people can participate by answering your question in a separate thread)
Title/Question: (55 Chars. Maximum)
Comment/Detailed description:* (No HTML / URL Allowed)

Characters left

Verification code:*



(If you cannot see the verification code, then refresh here)




CBSE Board, UP Board, IGNOU, JNU, MBA MCA, BBA and other educational boards of India

Disclaimer: For documents and information available on BoardGuess.com, we do not warrant or assume any legal liability or responsibility for the accuracy, completeness, or usefulness of any information. Papers, Results, Syllabus, Logo and other educational contents are owned by Indian Education Board and BoardGuess does not hold any copyright on it. The format of materials, being displayed on this website, comes under the copyright act. BoardGuess.com, All Rights Reserved ©