Need help with listing files.

This is a discussion on Need help with listing files. within the Windows Programming forums, part of the Platform Specific Boards category; Ok Here's my code. It compiles fine, but it don't list me files indirectory. In list.txt I have "C:\users\rain" but ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    10

    Need help with listing files.

    Ok Here's my code. It compiles fine, but it don't list me files indirectory.

    In list.txt I have "C:\users\rain" but my program shows following output.

    Code:
    C:\Users\rain>D:\Development\Cplusplus\del\del2\Debug\del2.exe
    C:\users\rain
    File Found: rain
    
    C:\Users\rain>
    I tried changing "C:\users\rain" to "C:\users\rain\" but then output is like this:

    Code:
    The first file found is ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ńŅ▀D..,
    I can't find out why it shows only File Found: rain. I have many files and folders in "C:\users\rain" and none of them are called "rain"

    Code:
    #include "stdafx.h"
    
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <string>
    #include <iostream>
    #include "Shlwapi.h"
    
    
    using namespace std;
    
    
    void main()
    {
       WIN32_FIND_DATA FindFileData;
       HANDLE hFind;
    
       //Read list file
       FILE *filein;
       long lSize;
       char * buffer;
       size_t result;
       //Get path to list.txt
       	 char szFileName[MAX_PATH];
         HINSTANCE hInstance = GetModuleHandle(NULL);
         GetModuleFileName(hInstance, szFileName, MAX_PATH);
    	 PathRemoveFileSpec(szFileName);
    	 PathAppend(szFileName,"\\list.txt");
    	 //Open file
       filein = fopen(szFileName, "r");
       //if (filein==NULL) {fputs ("File error",stderr); exit (1);}
    
        // obtain file size:
        fseek (filein , 0 , SEEK_END);
        lSize = ftell (filein);
        rewind (filein);
    
        // allocate memory to contain the whole file:
        buffer = (char*) malloc ((sizeof(char)*lSize)+1);
    
    	// copy the file into the buffer:
        result = fread (buffer,1,lSize,filein);
    	buffer[result] = '\0'; // make extra byte nul
    	/*//Print some info
        printf ("%s\n",buffer);// list.txt
    	printf ("%s\n",szFileName);//Path
        fclose(filein);
    	*/
    
    	//token
    	char *token;
    	token = strtok(buffer,"\r\n");
    	   while( token != NULL )
           {
               // While there are tokens in "string"
               printf( "%s\n", token );
    			//List files
    			hFind = FindFirstFile(token, &FindFileData);
    			do{
    				_tprintf (TEXT("File Found: %s\n"),FindFileData.cFileName);
    			}while(FindNextFile(hFind, &FindFileData));
    			FindClose(hFind);
    
               // Get next token: 
               token = strtok( NULL,"\r\n"); // C4996
           }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Try it with your error checking code enabled.

    Also, show us the content of your input file, or how you generated it.
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I'm surprised you didn't complain about void main, Salem!
    rain-13: Aside from a non-working programs, there are fatal flaws in your code.
    The first is void main. It's evil and should never be used: SourceForge.net: Void main - cpwiki
    The second is malloc. You must free what you malloc. This isn't some kind of free ticket. If you're going to use C and malloc, you had better well learn to deal with the consequences. Use free in the appropriate spot.
    And lastly, indent properly and consistently.

    And why in the hell are you using C with C++? Start deciding which language you want to use.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    This seems wrong:
    Code:
    token = strtok(buffer,"\r\n");    // \n already expands to whatever is appropriate for your platform
    @Elysia
    I don't see any C++ there, but he's mixing C standard functions with those of Win32 like it's going out of style.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    If you look at the OP's code, there are certain lines that says it's C++:

    #include <string>
    #include <iostream>

    using namespace std;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Elysia View Post
    If you look at the OP's code, there are certain lines that says it's C++:

    #include <string>
    #include <iostream>

    using namespace std;
    Oh. Missed those. I just went over the contents of main() and didn't see anything that looked like C++.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    I had to add * at the end of path

    Quote Originally Posted by Elysia View Post
    I'm surprised you didn't complain about void main, Salem!
    rain-13: Aside from a non-working programs, there are fatal flaws in your code.
    The first is void main. It's evil and should never be used: SourceForge.net: Void main - cpwiki
    The second is malloc. You must free what you malloc. This isn't some kind of free ticket. If you're going to use C and malloc, you had better well learn to deal with the consequences. Use free in the appropriate spot.
    And lastly, indent properly and consistently.

    And why in the hell are you using C with C++? Start deciding which language you want to use.
    I found codes from internet and put them together into one code, I didn't even know that I had C with C++. Which part i have C++ and which part is C? Is it bad if I have C with C++? If yes what replacements I need to do to get C only?

    Before I didn't know about that void main problem.

    Does that free(buffer); do what needed or do i need to free something other?


    Code:
    #include "stdafx.h"
    
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <string>
    #include <iostream>
    #include "Shlwapi.h"
    
    
    using namespace std;
    
    
    int main()
    {
       WIN32_FIND_DATA FindFileData;
       HANDLE hFind;
    
       //Read list file
       FILE *filein;
       long lSize;
       char * buffer;
       size_t result;
       //Get path to list.txt
       	 char szFileName[MAX_PATH];
         HINSTANCE hInstance = GetModuleHandle(NULL);
         GetModuleFileName(hInstance, szFileName, MAX_PATH);
    	 PathRemoveFileSpec(szFileName);
    	 PathAppend(szFileName,"\\list.txt");
    	 //Open file
       filein = fopen(szFileName, "r");
       //if (filein==NULL) {fputs ("File error",stderr); exit (1);}
    
        // obtain file size:
        fseek (filein , 0 , SEEK_END);
        lSize = ftell (filein);
        rewind (filein);
    
        // allocate memory to contain the whole file:
        buffer = (char*) malloc ((sizeof(char)*lSize)+1);
    
    	// copy the file into the buffer:
        result = fread (buffer,1,lSize,filein);
    	buffer[result] = '\0'; // make extra byte nul
    	/*//Print some info
        printf ("%s\n",buffer);// list.txt
    	printf ("%s\n",szFileName);//Path
        fclose(filein);
    	*/
    
    	//token
    	char *token;
    	token = strtok(buffer,"\r\n");
    	   while( token != NULL )
           {
               // While there are tokens in "string"
               printf( "%s\n", token );
    			//List files
    			hFind = FindFirstFile(token, &FindFileData);
    			do{
    				_tprintf (TEXT("File Found: %s\n"),FindFileData.cFileName);
    			}while(FindNextFile(hFind, &FindFileData));
    			FindClose(hFind);
    
               // Get next token: 
               token = strtok( NULL,"\r\n"); // C4996
           }
       free(buffer);
    return	0;
    }
    Thanks for hints and tips.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by rain-13 View Post
    I found codes from internet and put them together into one code, I didn't even know that I had C with C++. Which part i have C++ and which part is C? Is it bad if I have C with C++? If yes what replacements I need to do to get C only?
    It is true that you can compile C with C++. Many C++ programmers would scoff at such things, though.
    It is also fine if you compile C with a C++ compiler, so long as you know that C++'s stricter type system demands you to do some things that aren't necessary in C.
    About what is the C++ stuff: it's those things I quoted earlier. The headers and the using directive. Remove them and you get code that will compile as C.

    Before I didn't know about that void main problem.
    Now you do

    Does that free(buffer); do what needed or do i need to free something other?
    That should be enough since it's the only thing you malloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    How to remove * from path when I print it in console? * is last char of path.

    I tried this:
    Code:
    char *temp;
    strncpy (temp,token,strlen(token)-1);
    but for some reason it makes y program crash. any other methods to get rid of last symbol of string?

    And how to join temp and FindFileData.cFileName together into one string so I could check wether file is directory?

    Code:
    // del2.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <string>
    #include <iostream>
    #include "Shlwapi.h"
    
    
    using namespace std;
    
    
    int main()
    {
       WIN32_FIND_DATA FindFileData;
       HANDLE hFind;
    
       //Read list file
       FILE *filein;
       long lSize;
       char * buffer;
       size_t result;
       //Get path to list.txt
       	 char szFileName[MAX_PATH];
         HINSTANCE hInstance = GetModuleHandle(NULL);
         GetModuleFileName(hInstance, szFileName, MAX_PATH);
    	 PathRemoveFileSpec(szFileName);
    	 PathAppend(szFileName,"\\list.txt");
    	 //Open file
       filein = fopen(szFileName, "r");
       //if (filein==NULL) {fputs ("File error",stderr); exit (1);}
    
        // obtain file size:
        fseek (filein , 0 , SEEK_END);
        lSize = ftell (filein);
        rewind (filein);
    
        // allocate memory to contain the whole file:
        buffer = (char*) malloc ((sizeof(char)*lSize)+1);
    
    	// copy the file into the buffer:
        result = fread (buffer,1,lSize,filein);
    	buffer[result] = '\0'; // make extra byte nul
    	/*//Print some info
        printf ("%s\n",buffer);// list.txt
    	printf ("%s\n",szFileName);//Path
        fclose(filein);
    	*/
    
    	//token
    	char *token;
    	token = strtok(buffer,"\r\n");
    	   while( token != NULL )
           {
               // While there are tokens in "string"
               printf( "%s\n", token );
    
    		   char *temp;
    		   strncpy (temp,token,strlen(token)-1);
    		   //printf("%d\n",strlen(token)-1);
    		   //List files
    			hFind = FindFirstFile(token, &FindFileData);
    			do{
    				_tprintf (TEXT("File Found: %s%s\n"),temp,FindFileData.cFileName);
    			}while(FindNextFile(hFind, &FindFileData));
    			FindClose(hFind);
    
               // Get next token: 
               token = strtok( NULL,"\r\n"); // C4996
           }
    	free(buffer);
    return	0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  2. Listing specific files in a directory
    By knirirr in forum C Programming
    Replies: 14
    Last Post: 01-29-2008, 04:42 AM
  3. Listing files in C
    By Ezzetabi in forum Linux Programming
    Replies: 4
    Last Post: 10-19-2006, 11:17 PM
  4. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM
  5. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 04:18 PM

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