Using fopen on a folder

This is a discussion on Using fopen on a folder within the C Programming forums, part of the General Programming Boards category; Hey guys I'm currently writting a basic encryption program which works flawless on all types of files. Unfortunately when I ...

  1. #1
    Unregistered
    Guest

    Using fopen on a folder

    Hey guys I'm currently writting a basic encryption program which works flawless on all types of files.

    Unfortunately when I attempt to run it on a folder instead of individual files my fopen function always returns NULL.

    Is there some sort of hidden file extension for Windows folders that I need to include with the folder name, or maybe some other way I should go about this?

    Here is my basic open file code that works fine on all file types except folders:
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    
    int main()
    {
    	int count, key, random, flag;
    	char end;
    	char ORGfile[10] = "ORGfolder"; //original folder name
    	char ENCfile[10] = "ENCfolder"; //encrypted folder name
    	FILE *in, *out;
    
    		printf("\nEnter encryption key: ");
    		scanf("%s", &str);
    
    		key = atoi(str);
    		srand(key);
    
    		if (( in = fopen(ORGfile, "rb")) == NULL)
    		{
    			printf("\n\t\..........* ERROR: unable to open file for reading ***\n");
    			return(-1);
    		}
    		else
    		{
    			//the rest has been omitted since it has nothing to do with this question...
    		}
    	
    	return(0);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    http://www.cprogramming.com/cboard/s...der=descending

    Use findfirst/findnext to locate each file/subfolder, then use fopen to open each file

  3. #3
    Registered User purple's Avatar
    Join Date
    Mar 2002
    Posts
    28
    I was hoping to avoid encrypting each file within the folder one-by-one.

    I did figure out a way to kind of rig-it and force it to work. I simply used the rename() function within stdio.h to first rename the folder so it has filename with extension. It's a cheesy way to do it, but at least it works.

    If anyone still knows how I can encrypt the entire folder in one shot instead of doing it file-by-file I would still appreciate your ideas.

    I hate doing it with this chessy intermediate renaming step, but I will if all else fails.

    Thanks...

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    83

    folders

    i don't know of any other ways but you brought up a good question "is there a hidden extension for folders", i don' think there is but i have another question. if folders have no extension then how come if i make a new file, called say H, with no extension, its not a folder? Is this a stupid question?
    "What this country needs is more free speech worth listening to." - -Hansell B. Duckett

  5. #5
    Registered User purple's Avatar
    Join Date
    Mar 2002
    Posts
    28

    Talking

    No stupid questions, just stupid people...(just kidding) =)

    There is a function prototype in DIRECT.H called _mkdir() for creating directories. This is part of the Visual C++ 6.0 headers, although with other compilers the header might be called dir.h

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >I was hoping to avoid encrypting each file within the folder one-by-one.
    To "avoid" doing so, means you need to combine all files into one first. A bit pointless really.

    >I simply used the rename() function within stdio.h to first rename the folder so it has filename with extension.
    Do you know what a folder actually is? You cannot encrypt it, because it's not a true file, it more than likely just an entry in your OS allocation table.

    >If anyone still knows how I can encrypt the entire folder in one shot instead of doing it file-by-file I would still appreciate your ideas.
    Don't think there is any other way (other than I first suggested).... but I'd like someone to proove me wrong!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User purple's Avatar
    Join Date
    Mar 2002
    Posts
    28
    Yes I believe you are right. I thought at first that renaming the folder worked, but after closer inspection the program still had not encrypted the files. The only thing that had actually occured was the renaming of the folder.

    Oh well, I guess I'll just have to do it with each file one-by-one. I was hoping to keep the program simple since the program is just for personal use.

    Thank you to everyone that added their input on the subject...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  3. read only folder on Windows
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-05-2007, 08:18 AM
  4. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  5. Hiding a folder
    By nextstep in forum Windows Programming
    Replies: 16
    Last Post: 03-20-2005, 02:07 PM

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