Sep 12, 2020 at 7:48pm UTC
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 Sep 12, 2020 at 9:18pm UTC
Sep 12, 2020 at 8:10pm UTC
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?
Sep 12, 2020 at 8:28pm UTC
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 Sep 12, 2020 at 8:30pm UTC
Sep 12, 2020 at 8:31pm UTC
I think the loop is not correct or something
Sep 14, 2020 at 7:35am UTC
Okay.. understood :) Thank you so much.
Sep 14, 2020 at 10:06am UTC
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.
Sep 16, 2020 at 5:26am UTC
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.