C. How to check if the file is empty ?

I ran the code and it works I get the message when it has no content, but when the file is NOT empty I get the same error message "No information in data".. why is that, what am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//Check if exist or/and if file is empty
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define MAX 256

int main()
{
	int tmp, stat;
	unsigned opt;
	do
	{
		printf("\n\t\tRead files");
		printf("\n\n Choose:");
		printf("1. open file");
		printf("2. Exit program");
		printf("Select: ");
		stat = scanf("%d", &opt);
		
		while((tmp = getchar()) != EOF && tmp != '\n');
		if(!stat)
		{
			printf("\n\n Invalid option.. Choose 1 or 2");
			Sleep(2000);
                        system("cls");
                        options = 0;
                        continue;
		}
		else if(opt == 1)
		{
			FILE *file;
			char data[MAX];
			file = fopen("test.txt", "r");
			
			fseek(file, 0, SEEK_END);
			int size = ftell(file);
			
			while(1)
			{
				if(!file) //Check if file exists
				{
					printf("\n   File could not be open: \n\n\a");
					Sleep(1000);
					printf("   %s", strerror(errno));
					Sleep(1500);
					system("cls");
					break;
				}
				else if(size == 0) //Check if the file is empty
				{
					printf("\n\n   No information in data base\a");
					Sleep(1500);
					break;
				}
				else
				{
					for(int lNo = 0; fgets(data, 255, file) != NULL;)
						{
							printf("   %i.  %s\n", ++lNo, data);
						}
					printf("\n\n\n    Press any key to return menu..");
                                        fclose(file);
					getch();
					break;
				}
			}
		}
		else if(opt == 2)
		{
			printf("\n\n\t\t\t    Exiting program");
                        Sleep(2000);
                        system("cls");
		}
		else
		{
			printf("\n\n Invalid option.. Choose 1 or 2");
			Sleep(2000);
                        system("cls");
		}
	}while(opt != 2);
	
	return 0;
}


and the data in the file is not read..
Last edited on
Well since the file pointer is at the end of the file, how do you expect to read any data?

Perhaps you need to reposition the file pointer to the beginning of the file?

So I need to change the line 35 with SEEK_SET like this ?

 
fseek(file, 0, SEEK_SET);


Well I change that and run the program again ... first it tells

No information in data base
and right after it list me the data..
Last edited on
I think the loop is not correct or something
So I need to change the line 35 with SEEK_SET like this ?
No. At line 35 you want to seek to the end so you can get the size. You need to add fseek(file, 0, SEEK_SET); after this. I'd add it before line 57.

Why do you have the while loop at line 38?

You need to close the file when the size is zero.
Here's a little demo how to get the file size:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <assert.h>

long file_size(const char *filename)
{
	assert(filename != NULL && *filename != '\0');
	FILE *src = fopen(filename, "r");
	if(src == NULL)
		return -1;
	fseek(src, 0, SEEK_END);
	long result = ftell(src);
	fclose(src);
	
	return result;
}

int main(int argc, char **argv)
{
	long fsize = file_size(__FILE__);
	printf("File size: %ld", fsize);
	return 0;
}

Output: File size: 420
Okay.. understood :) Thank you so much.
There's also stat(). That returns information about the file, including the size, from the file's metatdata. It doesn't need to open the file.
Also note that on Windows, _stat and _stat64 (there are many variants, these are the only two I've tested) have problems on the System Volume Information folder on any drive. _stat and _stat64 will return -1, indicating an error, and errno will be set to 2, which is ENOENT, and is supposed to mean "no such file or directory." I reported the problem to Microsoft, no word back yet.
Topic archived. No new replies allowed.