C++ File Handling Question
C++ File Handling Question
C++ File Handling Question
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>
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);
Question :
Code :
#include <stdio.h>
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 :
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 :
Code:
#include <stdio.h>
#include <string.h>
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 :
Code :
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fptr1, *fptr2;
char ch, fname1[20], fname2[20];
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();
}