linked list issue

This is a discussion on linked list issue within the C Programming forums, part of the General Programming Boards category; Okay so the thing is that i want to list the current directory in alphabetical order using a linked list. ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    linked list issue

    Okay so the thing is that i want to list the current directory in alphabetical order using a linked list.

    Now i need to create a

    struct node ; which will have a data part and a link part.

    Is there any standard function which can hold the information about the file and also how do i iterate through the current directory?

    By standard function i mean any struct which can hold the file info.

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    well, on windows you can use FindNextFile(), and doing an insertion sort into the linked list as you read in the info for each file. Since the function by default returns the information in a WIN32_FIND_DATA Structure you can just use that as the attached object for the linked list.
    Last edited by abachler; 08-31-2009 at 11:37 PM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    But then this is win32 api specific function. Also i have no idea on how to use this function.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    [insert]
    Code:
    #include <windows.h>
    #include <tchar.h> 
    #include <stdio.h>
    #include <strsafe.h>
    
    void DisplayErrorBox(LPTSTR lpszFunction);
    
    int _tmain(int argc, TCHAR *argv[])
    {
       WIN32_FIND_DATA ffd;
       LARGE_INTEGER filesize;
       TCHAR szDir[MAX_PATH];
       size_t length_of_arg;
       HANDLE hFind = INVALID_HANDLE_VALUE;
       DWORD dwError=0;
       
       // If the directory is not specified as a command-line argument,
       // print usage.
    
       if(argc != 2)
       {
          _tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]);
          return (-1);
       }
    
       // Check that the input path plus 3 is not longer than MAX_PATH.
       // Three characters are for the "\*" plus NULL appended below.
    
       StringCchLength(argv[1], MAX_PATH, &length_of_arg);
    
       if (length_of_arg > (MAX_PATH - 3))
       {
          _tprintf(TEXT("\nDirectory path is too long.\n"));
          return (-1);
       }
    
       _tprintf(TEXT("\nTarget directory is %s\n\n"), argv[1]);
    
       // Prepare string for use with FindFile functions.  First, copy the
       // string to a buffer, then append '\*' to the directory name.
    
       StringCchCopy(szDir, MAX_PATH, argv[1]);
       StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
    
       // Find the first file in the directory.
    
       hFind = FindFirstFile(szDir, &ffd);
    
       if (INVALID_HANDLE_VALUE == hFind) 
       {
          DisplayErrorBox(TEXT("FindFirstFile"));
          return dwError;
       } 
       
       // List all the files in the directory with some info about them.
    
       do
       {
          if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
          {
             _tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);
          }
          else
          {
             filesize.LowPart = ffd.nFileSizeLow;
             filesize.HighPart = ffd.nFileSizeHigh;
             _tprintf(TEXT("  %s   %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
          }
       }
       while (FindNextFile(hFind, &ffd) != 0);
     
       dwError = GetLastError();
       if (dwError != ERROR_NO_MORE_FILES) 
       {
          DisplayErrorBox(TEXT("FindFirstFile"));
       }
    
       FindClose(hFind);
       return dwError;
    }
    
    
    void DisplayErrorBox(LPTSTR lpszFunction) 
    { 
        // Retrieve the system error message for the last-error code
    
        LPVOID lpMsgBuf;
        LPVOID lpDisplayBuf;
        DWORD dw = GetLastError(); 
    
        FormatMessage(
            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            dw,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR) &lpMsgBuf,
            0, NULL );
    
        // Display the error message and clean up
    
        lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
            (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
        StringCchPrintf((LPTSTR)lpDisplayBuf, 
            LocalSize(lpDisplayBuf) / sizeof(TCHAR),
            TEXT("%s failed with error %d: %s"), 
            lpszFunction, dw, lpMsgBuf); 
        MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 
    
        LocalFree(lpMsgBuf);
        LocalFree(lpDisplayBuf);
    }
    I used this function and tried passing the directory which i want to observe for the list of files and passed the command line argument as c:/Program Files (copy and pasted into the command arguments section of VS) BUT it continues to give me the usage thingy again and again.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    what usage thing? please post the error it is giving.

    added- oh i see, you are just using their example, which actually has to be run from the command line, not just from the IDE as it requires a command line argument. This example is just one more example of how horrible Microsoft example code can be for illustrating how to use a function. It's way overkill and the function of interest is buried in tons of other functionality.

    Here use this example -

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char* argv[]){
    
    	WIN32_FIND_DATA FindData;
    
    	HANDLE hFind = FindFirstFile(TEXT("*.*") , &FindData);
    
    	if(hFind != INVALID_HANDLE_VALUE){
    		// do some stuff for the first file
    
    		while(FindNextFile(hFind , &FindData)){
    			// do some stuff to subsequent files
    			}
    		
    		}
    
    	return 0;
    	}
    Last edited by abachler; 09-01-2009 at 01:02 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    No its not an error . But it gives the following
    [insert]
    Code:
     if(argc != 2)
       {
          _tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]);
          return (-1);
       }
    I am passing the directory which i want to observe as the command line argument but it continues to give the above printf statement which lists the current directory where my Visual Studio is running. Why does it not list the files of the directory which i am passing.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Because argv[0] is not the first parameter, but the name of the program. In this case windows is giving the full path as the name. To get your arguement, access argv[1].
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM

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