100% found this document useful (1 vote)
371 views

C++ File Handling Question

The document contains code snippets for performing various file operations in C like reading and writing to files, counting lines and words, deleting lines, and copying files. It includes the code, sample input/output, and descriptions of what each code snippet does.

Uploaded by

Rizi X
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
371 views

C++ File Handling Question

The document contains code snippets for performing various file operations in C like reading and writing to files, counting lines and words, deleting lines, and copying files. It includes the code, sample input/output, and descriptions of what each code snippet does.

Uploaded by

Rizi X
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Question :

Write a program in C to read an existing file.


Test Data :
Input the file name to be opened : test.txt
Expected Output :
The content of the file test.txt is :
This is the content of the file test.txt.

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

void main()
{
FILE *fptr;
char fname[20];
char str;
printf("\n\n Read an existing file :\n");
printf("------------------------------\n");
printf(" Input the filename to be opened : ");
scanf("%s",fname);
fptr = fopen (fname, "r");
if (fptr == NULL)
{
printf(" File does not exist or cannot be opened.\n");
exit(0);
}
printf("\n The content of the file %s is :\n",fname);
str = fgetc(fptr);
while (str != EOF)
{
printf ("%c", str);
str = fgetc(fptr);
}
fclose(fptr);
printf("\n\n");
}

Question :
Write a program in C to write multiple lines in a text file.
Test Data :
Input the number of lines to be written : 4
:: The lines are ::
test line 1
test line 2
test line 3
test line 4
Expected Output :
The content of the file test.txt is :
test line 1
test line 2
test line 3
test line 4

Code :
#include <stdio.h>

int main ()
{
FILE * fptr;
int i,n;
char str[100];
char fname[20]="test.txt";
char str1;

printf("\n\n Write multiple lines in a text file and read the file :\n");

printf("------------------------------------------------------------\n");
printf(" Input the number of lines to be written : ");
scanf("%d", &n);
printf("\n :: The lines are ::\n");
fptr = fopen (fname,"w");
for(i = 0; i < n+1;i++)
{
fgets(str, sizeof str, stdin);
fputs(str, fptr);
}
fclose (fptr);
/*-------------- read the file -------------------------------------*/
fptr = fopen (fname, "r");
printf("\n The content of the file %s is :\n",fname);
str1 = fgetc(fptr);
while (str1 != EOF)
{
printf ("%c", str1);
str1 = fgetc(fptr);
}
printf("\n\n");
fclose (fptr);
return 0;
}

Question :
Write a program in C to read the file and store the lines into an array.
Test Data :
Input the file name to be opened : test.txt
Expected Output :
The content of the file test.txt are :
test line 1
test line 2
test line 3
test line 4

Code:

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

#define LSIZ 128


#define RSIZ 10

int main(void)
{
char line[RSIZ][LSIZ];
char fname[20];
FILE *fptr = NULL;
int i = 0;
int tot = 0;
printf("\n\n Read the file and store the lines into an array :\n");
printf("------------------------------------------------------\n");
printf(" Input the filename to be opened : ");
scanf("%s",fname);

fptr = fopen(fname, "r");


while(fgets(line[i], LSIZ, fptr))
{
line[i][strlen(line[i]) - 1] = '\0';
i++;
}
tot = i;
printf("\n The content of the file %s are : \n",fname);
for(i = 0; i < tot; ++i)
{
printf(" %s\n", line[i]);
}
printf("\n");
return 0;
}

Question :

Write a program in C to Find the Number of Lines in a Text File.


Test Data :
Input the file name to be opened : test.txt
Expected Output :
The lines in the file test.txt are : 4

Code :

#include <stdio.h>

#define FSIZE 100

int main()
{
FILE *fptr;
int ctr = 0;
char fname[FSIZE];
char c;
printf("\n\n Read the file and count the number of lines :\n");
printf("--------------------------------------------------\n");
printf(" Input the file name to be opened : ");
scanf("%s",fname);
fptr = fopen(fname, "r");
if (fptr == NULL)
{
printf("Could not open file %s", fname);
return 0;
}
// Extract characters from file and store in character c
for (c = getc(fptr); c != EOF; c = getc(fptr))
if (c == '\n') // Increment count if this character is newline
ctr = ctr + 1;
fclose(fptr);
printf(" The lines in the file %s are : %d \n \n", fname, ctr-1);
return 0;
}

Question :

Write a program in C to count a number of words and characters in a file.


Test Data :
Input the file name to be opened : test.txt
Expected Output :
The content of the file test.txt are :
test line 1
test line 2
test line 3
test line 4
The number of words in the file test.txt are : 12
The number of characters in the file test.txt are :
36

Code :

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

void main()
{
FILE *fptr;
char ch;
int wrd=1,charctr=1;
char fname[20];
printf("\n\n Count the number of words and characters in a file :\n");
printf("---------------------------------------------------------\n");
printf(" Input the filename to be opened : ");
scanf("%s",fname);

fptr=fopen(fname,"r");
if(fptr==NULL)
{
printf(" File does not exist or can not be opened.");
}
else
{
ch=fgetc(fptr);
printf(" The content of the file %s are : ",fname);
while(ch!=EOF)
{
printf("%c",ch);
if(ch==' '||ch=='\n')
{
wrd++;
}
else
{
charctr++;
}
ch=fgetc(fptr);
}
printf("\n The number of words in the file %s are : %d\n",fname,wrd-
2);
printf(" The number of characters in the file %s are :
%d\n\n",fname,charctr-1);
}
fclose(fptr);
}

Question :

Write a program in C to delete a specific line from a file.


Assume that the content of the file test.txt is :
test line 1
test line 2
test line 3
test line 4
Test Data :
Input the file name to be opened : test.txt
Input the line you want to remove : 2
Expected Output :
The content of the file test.txt is :
test line 1
test line 3
test line 4

Code:

#include <stdio.h>
#include <string.h>

#define MAX 256

int main()
{
int lno, ctr = 0;
char ch;
FILE *fptr1, *fptr2;
char fname[MAX];
char str[MAX], temp[] = "temp.txt";
printf("\n\n Delete a specific line from a file :\n");
printf("-----------------------------------------\n");
printf(" Input the file name to be opened : ");
scanf("%s",fname);
fptr1 = fopen(fname, "r");
if (!fptr1)
{
printf(" File not found or unable to open the input
file!!\n");
return 0;
}
fptr2 = fopen(temp, "w"); // open the temporary file in write mode
if (!fptr2)
{
printf("Unable to open a temporary file to write!!\n");
fclose(fptr1);
return 0;
}
printf(" Input the line you want to remove : ");
scanf("%d", &lno);
lno++;
// copy all contents to the temporary file except the specific line
while (!feof(fptr1))
{
strcpy(str, "\0");
fgets(str, MAX, fptr1);
if (!feof(fptr1))
{
ctr++;
/* skip the line at given line number */
if (ctr != lno)
{
fprintf(fptr2, "%s", str);
}
}
}
fclose(fptr1);
fclose(fptr2);
remove(fname); // remove the original file
rename(temp, fname); // rename the temporary file to original name
/*------ Read the file ----------------*/
fptr1=fopen(fname,"r");
ch=fgetc(fptr1);
printf(" Now the content of the file %s is : \n",fname);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fptr1);
}
fclose(fptr1);
/*------- End of reading ---------------*/
return 0;

Question :

Write a program in C to copy a file in another name.


Assume that the content of the file test.txt is :
test line 1
test line 2
test line 3
test line 4
Test Data :
Input the source file name : test.txt
Input the new file name : test1.txt
Expected Output :
The file test.txt copied successfully in the file
test1.txt.

Code :

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

void main()
{
FILE *fptr1, *fptr2;
char ch, fname1[20], fname2[20];

printf("\n\n Copy a file in another name :\n");


printf("----------------------------------\n");

printf(" Input the source file name : ");


scanf("%s",fname1);

fptr1=fopen(fname1, "r");
if(fptr1==NULL)
{
printf(" File does not found or error in opening.!!");
exit(1);
}
printf(" Input the new file name : ");
scanf("%s",fname2);
fptr2=fopen(fname2, "w");
if(fptr2==NULL)
{
printf(" File does not found or error in opening.!!");
fclose(fptr1);
exit(2);
}
while(1)
{
ch=fgetc(fptr1);
if(ch==EOF)
{
break;
}
else
{
fputc(ch, fptr2);
}
}
printf(" The file %s copied successfully in the file %s.
\n\n",fname1,fname2);
fclose(fptr1);
fclose(fptr2);
getchar();
}

You might also like