Results 1 to 3 of 3

Thread: [RESOLVED] Help Displaying data form an Array

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    417

    Resolved [RESOLVED] Help Displaying data form an Array

    Hi,
    I am working on a little example that Bascily scans a Path and grabs all the file names, then store them into an array. Anyway most of the code is working find all expect the last part were I need to display the output.

    I left a copy of all my code below and commented. it

    VB Code:
    1. #include <iostream>
    2. #include <fstream>
    3. #include <windows.h>
    4. using namespace std;
    5.  
    6. int main(int agvc)
    7. {
    8.    WIN32_FIND_DATA fd;
    9.    HANDLE FindFile;
    10.    int cnt=0;
    11.    char findPath[80];
    12.    char **Filename;
    13.  
    14.    strcpy(findPath,"C:\\*.*"); // path to scan
    15.    FindFile = FindFirstFile(findPath, &fd); // Get first file handle
    16.    
    17.    if(FindFile == INVALID_HANDLE_VALUE)
    18.    {
    19.       // oops error
    20.       cout << "INVALID_HANDLE_VALUE\n";
    21.       exit(1);
    22.  
    23.    }
    24.  
    25.    do{
    26.       if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    27.          == FILE_ATTRIBUTE_DIRECTORY) continue;
    28.  
    29.       // is it possiable I am doing something wrong here.
    30.       Filename = (char**) malloc(cnt); // Reszie arry to hold the filenames
    31.       Filename[cnt] = fd.cFileName; // store filename
    32.       cnt++;
    33.  
    34.    }while(FindNextFile(FindFile,&fd));
    35.    FindClose(FindFile); // close file handle
    36.    
    37.    std::cout << Filename[0] << endl; // Why is this giveing an error
    38.  
    39.    return 0;
    40. }

    If anyone can help I whould be very greatfull
    When your dreams come true.
    On error resume pulling hair out.

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551

    Re: Help Displaying data form an Array

    There are several things going wrong here with "resizing the array" and "storing the file name".
    I could do two things now, namely 1) explain what's going wrong and way, and tell you a much better way, or 2) just tell you the much better way. I opt for the last, unless you really want to know what you're doing wrong.

    Use std::string to store string values, instead of character arrays, because they're much more convenient. You can just use the assignment operator (=) to assign one string value to another. Also, use std::vector to have array-like functionality. You don't need to manually resize it, this class just does it automatically for you. Also, in both cases you don't need to worry about freeing memory (which is completely forgotten in the snippet you posted).
    std::string and std::vector are both part of the Standard Template Library (STL). This library is a part of the C++ standard, so you should be able to use it on every somewhat recent compiler.

    This is the way how you can use std::string and std::vector in your case.
    Code:
       std::vector<std::string> vecFiles;   // Don't be scared by the syntax ;)
       do{ 
          if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
             == FILE_ATTRIBUTE_DIRECTORY) continue; 
    
          // "Convert" the filename to a string, and add it to the vector.
          // The conversion (cast) isn't strictly necessary here, but has been
          // added to show you what's happening under the hood.
          vecFiles.push_back(std::string(fd.cFileName));
       }while(FindNextFile(FindFile,&fd));
    You don't have to keep track of the count anymore. A vector has a size() method, which gives you the number of elements in the vector. This is simply done this way: vecFiles.size().
    If you want to retrieve the contents of one of the elements in the vector, you can use the subscript operator ([]), just like you would do with an array. Keep in mind that the given value should be in the range (0 <= value < vecFiles.size()), or else a runtime error will occur!

    If you want to iterate through the vector, because you want to access the items, you can use a simple counter, but it's better to use the special iterator.
    An example:
    Code:
    for(std::vector<std::string>::iterator iterFile = vecFiles.begin(); \
       iterFile != vecFiles.end(); ++iterFile)
    {
       std::cout << "File: " << *iterFile << std::endl;
    }
    *iterFile shows the value which you're pointing at the moment. (The dereference operator, *, has been used here.)
    You can also "calculate" the index of the item you're pointing at in case you need it, but this doesn't work for every STL container. It is done this way: (iterFile - vecFiles.begin()). If you need the index while you're iterating, it's better to use the old way: for(int idx = 0; idx < vecFiles.size(); ++idx).

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    417

    Re: Help Displaying data form an Array

    Hi all not been to this topic for a few days almost fogot about it.

    anyway Thank you very much for your help riis, eveything is now working correctly.
    When your dreams come true.
    On error resume pulling hair out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width