Chapter - 13: File Input-Output

Point out the errors, if any, in the following programs:


A
Sections
1
Exercises

(a)

#include<stdio.h>

void openfile(char*, FILE**);

int main()
{
	FILE *fp;
	openfile("Myfile.txt", fp);
	if (fp == NULL)
		printf("Unable to open file…");
	return 0;
}
void openfile(char *fn, FILE **f)
{
	*f = fopen(fn, "r");
}

Error: Argument of openfile() is of FILE** type, while in main FILE* is passed in the function.


(b)

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

int main()
{
	FILE *fp;
	char c;
	fp = fopen("TRY.C", "r");
	if (fp == null)
	{
		puts("Cannot open file");
		exit();
	}
	while ((c = getc(fp)) != EOF)
		putch(c);
	fclose(fp);
	return 0;
}

Error:
1. null is not defined, NULL should be used at that place.
2. exit() function should have an int type argument, which is not present there.
3. putch() is defined in conio.h file which is also not present.


(c)

#include<stdio.h>

int main()
{
	char fname[] = "c:\\students.dat";
	FILE *fp;
	fp = fopen(fname, "tr");
	if (fp == NULL)
		printf("\nUnable to open file...");
	return 0;
}

Error: "tr" is not any valid file opening mode, it should be "r", "rt" or "rb". And a .dat should be open in binary mode, that is in "rb" mode.


(d)

#include<stdio.h>
int main()
{
	FILE *fp;
	char str[80];
	fp = fopen("TRY.C", "r");
	while (fgets(str, 80, fp) != EOF)
		fputs(str);
	fclose(fp);
	return 0;
}

Error:

1. fgets() do not return EOF, so while loop statement is invalid.

2. We are storing 81 characters in str variable, 80 characters form the file and 1 character is added automatically the NULL  character that caused the overflow of the array.

3. fputs() accepts three arguments, char* and FILE*, but only one argument is passed.


(e)

#include<stdio.h>
int main()
{
	unsigned char;
	FILE *fp;
	fp = fopen("trial", "r");
	while ((ch = getc(fp)) != EOF)
		printf("%c", ch);
	fclose(fp);
	return 0;
}

Error: ch is not defined in this scope.


(f)

#include<stdio.h>
int main()
{
	FILE *fp;
	char name[20];
	int i;
	fp = fopen("students.dat", "wb");
	for (i = 0; i <= 10; i++)
	{
		puts("\nEnter name: ");
		gets(name);
		fwrite(name, size of(name), 1, fp);
	}
	close(fp);
	return 0;
}

Error:
1. sizeof operator does not have space between it.
2. close() function is undefined. Use fclose();


(g)

#include<stdio.h>
int main()
{
	FILE *fp;
	char name[20] = "Ajay";
	int i;
	fp = fopen("students.dat", "r");
	for (i = 0; i <= 10; i++)
		fwrite(name, sizeof(name), 1, fp);
	close(fp);
	return 0;
}

Error: 
1. Cannot use fwrite() function on the file opened in  "r" (read-only) mode.
2. close() function is undefined. Use fclose();


(h)

#include<stdio.h>
int main()
{
	int fp;
	fp = open("pr22.c", "r");
	if (fp == -1)
		puts("cannot open file");
	else
		close(fp);
	return 0;
}

Error: 
1. open() and close() functions are UNIX platform-specific. Use _open() or _close() and include io.h file if you are using Visual Studio.


(i)

#include<stdio.h>

int main()
{
	int fp;
	fp = fopen("students.c", READ | BINARY);
	if (fp == -1)
		puts("cannot open file");
	else
		close(fp);
	return 0;
}

Error:
1. close() and open() functions are not defined, with the same reason stated above.
2. fopen() returns FILE* but it is assigned to an int type variable.
3. READ | BINARY are undefined


© 2021 Garbage Valuegarbage value logo