Checking file identity/equivalence

This is a discussion on Checking file identity/equivalence within the C Programming forums, part of the General Programming Boards category; I have a program that iterates through files and subdirectories, reads them in, does something and then dumps that into ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Checking file identity/equivalence

    I have a program that iterates through files and subdirectories, reads them in, does something and then dumps that into a single output file. I did this with the Win32 API. Now my problem is, if the user designates an output file that is in the same directory as the one I'm working in, my file iteration routine will attempt to open that as well, and basically, you end up reading from and writing to the same file at the same time.

    My question is, is there a native C function which allows you to check whether two files are the same (and I don't mean, do they have the same content, I mean, are they really the same file).

    Here's the function I'm working on:

    Code:
    void recurseDirectories(char *root, char *ext, FILE* f_out, void (*callBack)(FILE* f_source, FILE* f_output), bool recurse = true)
    {
    	flist           list = { 0, 0, NULL };
    	HANDLE          h;
    	WIN32_FIND_DATA info;
    	int             i, i_dir_count = 0, count = 0;
    
    	h = FindFirstFile(ext, &info);
    	if (h != INVALID_HANDLE_VALUE)
    	{
    		do
    		{
    			//This is where I would like to put some kind of conditional 
    			//statement which compares the output file and the current handle, 
    			//and if it turns out that they're the same, then it would not add a file to the list.
    			if (!(strcmp(info.cFileName, ".") == 0 || strcmp(info.cFileName, "..") == 0))
    			{
    				addfile(&list, info);
    			}
    		} while (FindNextFile(h, &info));
    		if (GetLastError() != ERROR_NO_MORE_FILES) errormessage();
    		FindClose(h);
    	}
    	else
    	{
    		errormessage();
    	}
    
    	for (i = 0; i < list.num_entries; i++)
    	{
    		if (list.files[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    		{
    			i_dir_count++;
    		}
    		else
    		{
    			FILE* f_in = fopen(list.files[i].cFileName, "rb");
    			if(f_in)
    			{
    				callBack(f_in, f_out);
    				fclose(f_in);
    			}
    			f_out->_base;
    			else
    			{
    				fprintf(stderr, "File %s could not be opened!\n", list.files[i].cFileName);
    			}
    		}
    		count++;
    		printf("%d\t%d\n", i, count);
    	}
    
    	if(recurse)
    	{
    		list.files = (WIN32_FIND_DATA *) realloc(list.files, i_dir_count * sizeof(WIN32_FIND_DATA));
    		for (i = 0; i < i_dir_count; i++)
    		{
    			char  newroot[MAX_PATH];
    			sprintf(newroot, "%s\\%s", root, list.files[i].cFileName);
    			SetCurrentDirectory(list.files[i].cFileName);
    			recurseDirectories(newroot, ext, f_out, callBack, recurse);
    			SetCurrentDirectory("..");
    		}
    	}
    	free(list.files);
    }
    Now, I guess you could do this by passing the filename as well into this function, but what if the user actually designated a separate path for the output file and that a file in the current directory had the same name as the output file. Then it would be skipped, right? So I'm looking for an alternative solution.

    Any help is appreciated.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Build the list of input files first, before creating the output file.
    Then your search will fail to find it, and it stops being a problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    That would mean I would have to rethink how I process my output file. Oh well, I guess if you of all people don't know of any native functions, it means there are none.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21