Thread: Is this me or Windows?

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404

    Is this me or Windows?

    I have been trying to read all of the directories and files within a specified directory. This directory only contains subdirectories. They have names of F00 all the way to F49. I kept getting an error when it got to F12 so I decided to check it out. When I decided to printf all of the f.cFileNames's to the console, F12 contained incorrect characters. Here is the output from the command line:
    Code:
    C:\Ben\F00\* - 12
    C:\Ben\F01\* - 12
    C:\Ben\F02\* - 12
    C:\Ben\F03\* - 12
    C:\Ben\F04\* - 12
    C:\Ben\F05\* - 12
    C:\Ben\F06\* - 12
    C:\Ben\F07\* - 12
    C:\Ben\F08\* - 12
    C:\Ben\F09\* - 12
    C:\Ben\F10\* - 12
    C:\Ben\F11\* - 12
    C:\Ben\↑{▼\* - 12
    C:\Ben\F13\* - 12
    C:\Ben\F14\* - 12
    C:\Ben\F15\* - 12
    C:\Ben\F16\* - 12
    C:\Ben\F17\* - 12
    C:\Ben\F18\* - 12
    C:\Ben\F19\* - 12
    C:\Ben\F20\* - 12
    C:\Ben\F21\* - 12
    C:\Ben\F22\* - 12
    C:\Ben\F23\* - 12
    C:\Ben\F24\* - 12
    C:\Ben\F25\* - 12
    C:\Ben\F26\* - 12
    C:\Ben\F27\* - 12
    C:\Ben\F28\* - 12
    C:\Ben\F29\* - 12
    C:\Ben\F30\* - 12
    C:\Ben\F31\* - 12
    C:\Ben\F32\* - 12
    C:\Ben\F33\* - 12
    C:\Ben\F34\* - 12
    C:\Ben\F35\* - 12
    C:\Ben\F36\* - 12
    C:\Ben\F37\* - 12
    C:\Ben\F38\* - 12
    C:\Ben\F39\* - 12
    C:\Ben\F40\* - 12
    C:\Ben\F41\* - 12
    C:\Ben\F42\* - 12
    C:\Ben\F43\* - 12
    C:\Ben\F44\* - 12
    C:\Ben\F45\* - 12
    C:\Ben\F46\* - 12
    C:\Ben\F47\* - 12
    C:\Ben\F48\* - 12
    C:\Ben\F49\* - 12
    Here is the code used to do it:
    Code:
    				if (strncmp(f.cFileName, "..", 2) > 0)
    				{
    					j++;
    					directories[j] = (char *)calloc((strlen(base) + strlen(f.cFileName) - 1), sizeof(char));
    					strncat(directories[j], base, strlen(base) - 1);
    					strcat(directories[j], f.cFileName);
    					strcat(directories[j], "\\*");
    
    					printf("%s - %d\n", directories[j], strlen(directories[j]));
    				}
    So, what the hell?! I checked all of the settings of F12 against all of the other folders and they were identical.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The space you allocate isn't long enough.
    No account for the \\* and the trailing \0
    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.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Even so, that wouldn't have anything to do with the part of the string that is being affected, and why it is so consistent to occur on a single directory and no other.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by carrotcake1029 View Post
    Even so, that wouldn't have anything to do with the part of the string that is being affected, and why it is so consistent to occur on a single directory and no other.
    Since you are "relying on undefined behaviour", there is nothing you can say about what is expected and what is not - overwriting the end of a buffer can cause a whole load of different things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Ok, well I fixed it and the error still occurs.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by carrotcake1029 View Post
    Ok, well I fixed it and the error still occurs.
    Show the fixed code, and perhaps you also should show the code that calls FindFirstFile/FindNextFile?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Code:
    				if (strncmp(f.cFileName, "..", 2) > 0)
    				{
    					j++;
    					directories[j] = (char *)calloc((strlen(base) + strlen(f.cFileName) + 2), sizeof(char));
    					strncat(directories[j], base, strlen(base) - 1);
    					strcat(directories[j], f.cFileName);
    					strcat(directories[j], "\\*");
    
    					printf("%s - %d\n", directories[j], strlen(directories[j]));
    				}
    Keep in mind on my strncat i copy 1 less byte, so I need to add just 2 iirc.

    Here is where I call FindFirstFile
    Code:
    	char *base = "C:\\Ben\\*";
    
    	h = FindFirstFile(base, &f);
    Then it runs through a series of loops and such.
    Code:
    	while (h != INVALID_HANDLE_VALUE)
    	{
    		while (FindNextFile(h, &f))

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Try printing out f.cFileName each iteration and see if that's messed up.
    Also, I'm assuming you ran DIR /X to see if the directory name itself is corrupted?

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    How about stepping through the code in a debugger and watching what happens? It's a great way to learn...

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How is directories declared?

    char **directories[10];
    perhaps?

    In which case, you need to add
    && j < 10
    to whatever loop is running through all the directory entries.



    > strncmp(f.cFileName, "..", 2) > 0
    Use != 0
    Directory names beginning with say space (which is legal) would not be caught by your code.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 98/2000 programming in Windows XP
    By Bill83 in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:16 PM
  2. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  3. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM

Tags for this Thread