File Index issue

This is a discussion on File Index issue within the C++ Programming forums, part of the General Programming Boards category; Hi, I wrote this little function to grab files inside a folder. However, although it compiles fine, the program crashes ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    41

    File Index issue

    Hi,

    I wrote this little function to grab files inside a folder. However, although it compiles fine, the program crashes on starting (Segmentation fault?):

    Code:
    #include <iostream>
    #include <windows.h> 
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    void grabFiles(char* root, vector<string> &endList); //Grabs files, and dumps them into vector.
    
    void grabFiles(char* root, vector<string> &endList)
    {
    	vector< WIN32_FIND_DATA > tmpFiles;
    	HANDLE h;
    	int i;
    	WIN32_FIND_DATA tmpFile;
    	
      // build a list of files
      h = FindFirstFile("*.*", &tmpFile);
      tmpFiles.push_back(tmpFile);
      if (h != INVALID_HANDLE_VALUE)
      {
        do
        {
          if (!(strcmp(tmpFile.cFileName, ".") == 0 || strcmp(tmpFile.cFileName, "..") == 0))
          {
            tmpFiles.push_back(tmpFile);
          }
        } while (FindNextFile(h, &tmpFile));
        if (GetLastError() != ERROR_NO_MORE_FILES) exit(-1);
        FindClose(h);
      }
      
      for(int x=0; x < tmpFiles.size(); x++)
      {
    		endList.push_back(tmpFiles[i].cFileName);
    	}
      
    }
    
    int main(int argc, char *argv[])
    {
    	vector<string> theLists;
        grabFiles(".", theLists); 
        
        for(int e=0; e<theLists.size(); e++)
        {
    		cout<<theLists[e]<<"\n\n";
    	}
    	cin.get();
      return(0);
    }

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    12
    I think instead of

    Code:
    int i;
    . . .
    for(int x=0; x < tmpFiles.size(); x++)
    {
        endList.push_back(tmpFiles[i].cFileName);
    }
    you should have

    Code:
    for(int x=0; x < tmpFiles.size(); x++)
    {
        endList.push_back(tmpFiles[x].cFileName);
    }
    I hope this helps.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    41
    Ahhh, how did I miss that?

    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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