Thread: File check

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Cool File check

    hey

    hope you guys can help me! I know what I want to do in words but am finding difficulty in putting it in to code.

    Basically, when my program starts I want it to check whether the file it uses has any data in it. If it does not, then I want to initialise a certain element of a struct. If it does, then I don't!

    Problem I have is how do I check whether the file has any data in it?

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why would you have a file with nothing in it? [/rhetorical]

    Open it up with fopen, read something with one of the many standard reading functions. See if it's what you expect, usually by simply checking the return value on said function.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    look at fseek(.,0,SEEK_END) / ftell pair of functions for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Exclamation

    thanks for your replies

    So, how would I set up a test condition using fseek to see if there was anything in the file?

    would I seek the beginning of the file in question and then fscanf to see if there was anything there????

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    IF the file FNAME has just been created. Should not the size be equal to zero?

    Code:
    int file_check()
    {
    	FILE *fptr;
    	int size;
    
    	fptr = fopen(FNAME, "rb");
    	fseek(fptr, 0, SEEK_END);
    	size = ftell(fptr);
    
    	return size;
    }

    please help

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by strokebow
    IF the file FNAME has just been created. Should not the size be equal to zero?



    please help
    Why not just try it and see by yourself?

    Note that the ftell returns long
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question

    No it doesn't! :-(

    Why does it not? :-S

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    how do you create a file? For me - it is working fine
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Thumbs up

    Hey! this is the part of my code which uses this function. Basically, the first time the user, uses the program I want the strcut flag to be set to zero.


    Code:
    main()
    {
    	pass customers[ROW][LABEL];
    	int i, j;
    	long empty;
    
    	clear_screen();
    	atexit(thank_you);
    	read_file(customers);
    	empty = file_check();
    	if(empty == 0) //If there is no data in the file loaded then we should initialise flag to zero for all structs.
    	{
    		for(i = 0; i < ROW; i++) 
    			for(j = 0; j < LABEL; j++)
    			customers[i][j].flag = 0;
    	}
    }
    Code:
    void read_file(pass cust[][LABEL])
    {
    	FILE* fptr;
    
    	fptr = fopen(FNAME, "rb");
    	if(fptr == NULL)
    	{
    		fprintf(stderr, "Could not open %s - %s\n", FNAME, sys_errlist[errno]);
    		if(yesno("Do you wish to create this file?"))
    		{
    			write_file(cust);
    			fptr = fopen(FNAME, "rb");
    		}
    		else
    			exit(1);
    	}
    	
    	fread(cust, sizeof(cust), 1, fptr);
    	fclose(fptr);
    }

    Code:
    void write_file(pass cust[][LABEL])
    {
    	FILE *fptr;
    
    	fptr = fopen(FNAME, "wb");
    	
    	if(fptr == NULL)
    		{
    			fprintf(stderr, "Could not open %s - %s\n", FNAME, sys_errlist[errno]);
    			exit(1);
    		}
    	
    	fwrite(cust, sizeof(cust), 1, fptr);
    	fclose(fptr);
    }
    Code:
    long file_check()
    {
    	FILE *fptr;
    	int size;
    
    	fptr = fopen(FNAME, "rb");
    	fseek(fptr, 0, SEEK_END);
    	size = ftell(fptr);
    
    	return size;
    }

    Thanks for your help! Its much appreciated!

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Surely, if I've just created any file it should be 0 bytes?

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I don't see any strcut flag, what I see - when file does not exists - it is written with sizeof(cust) bytes of data. So the emty file is never created.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Code:
    struct	passenger
    {
    int flag;  //Use as a boolean - 1 if seat taken, 0 if not taken
    char *first;
    char *last;
    };
    
    typedef struct passenger pass;
    b.t.w - This is the structure - sorry!

    So the file never gets created???



    Also yesno function - returns a 1 if user puts yes and a zero if user puts no.

    So what you're saying is (assuming the user wants to create the file) that the file will never be created. But when I run the program the file does get created but it is 1KB in size. This is confusing :-S

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    the empty file is never created...
    after creation the function fwrite is called. So from the very beginning file already contains some data.

    The file will never be empty when you get to the file_check function...
    So you should rethink your condition...
    If you want to distinguish the file that was just created from the file that was build on the previos run - you should reeturn some flag from your read_file function and check this flag, for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    AHA!!! I have found what the problem is. I can fix it but the way I'm thinking won't look pretty. So I'm here to see if you "experts" know any smarter ways.

    Code:
    #define ROW 7
    #define LABEL 2
    
    
    struct	passenger
    {
    int flag;  //Use as a boolean - 1 if seat taken, 0 if not taken
    char *first;
    char *last;
    };
    Code:
    main()
    {
    	pass customers[ROW][LABEL];
    	int i, j;
    
    	atexit(thank_you);
    	if(read_file(customers))
    	{
    		for(i = 0; i < ROW; i++) 
    			for(j = 0; j < LABEL; j++)
    			customers[i][j].flag = 0;
    		write_file(customers);
    	}
    }
    Code:
    int read_file(pass cust[][LABEL])
    {
    	FILE* fptr;
    	int new_file = 0;
    	
    
    	fptr = fopen(FNAME, "rb");
    	if(fptr == NULL)
    	{
    		fprintf(stderr, "Could not open %s - %s\n", FNAME, sys_errlist[errno]);
    		if(yesno("Do you wish to create this file?"))
    		{
    			write_file(cust);
    			fptr = fopen(FNAME, "rb");
    			new_file = 1;
    		}
    		else
    			exit(1);
    	}
    	
    	fread(cust, sizeof(cust), 1, fptr);
    	fclose(fptr);
    	
    	return new_file;	
    }
    Code:
    void write_file(pass cust[][LABEL])
    {
    	FILE *fptr;
    
    	fptr = fopen(FNAME, "wb");
    	
    	if(fptr == NULL)
    		{
    			fprintf(stderr, "Could not open %s - %s\n", FNAME, sys_errlist[errno]);
    			exit(1);
    		}
    
    	printf("In write file size of cust is %d", sizeof(cust));
    	getchar();
    	fflush(stdin);
    	
    	fwrite(cust, sizeof(cust), 1, fptr);
    	fclose(fptr);
    }

    Here's the problem!! The struct passed into the different functions is the pointer. So If I take the sizeof - it will be 4 bytes. How do I get the sizeof the original array of structures using the pointer (cust)?


    Thanks

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    without taking the name arrays into consideration
    sizeof(*cust) * ROWS

    if you want a size that takes into consideration extra memory allocated to the names, thats gonna be more difficult

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 11:56 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM