Thread: Read in a Folder to a String Array

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Read in a Folder to a String Array

    I wonder how it could be possible using a windows form(C++) and open a folder on the harddrive somewhere and when open this folder, there will be ex: 5 .txt files inside this folder.
    When open this folder the names of these 5 .txt files will be red in into a string array.

    I know how to use the openFileDialog but wonder how this could be possible to do.
    I dont really know the approach to do something like this. What could be the first step.
    Last edited by Coding; 02-07-2008 at 07:18 PM.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    im not for sure but using the FindFirstFile(), FindNextFile() methods you can get the name attribute of a file, and save it to a string array. These articles may help you MSDN FindFirstFile()
    MSDN FileNextFile()

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you using C++ .NET again? You should be using C# or Native C++.
    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
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes I do for the moment as I have really got into this version and done so much code and solutions. I perheps will change to Native C++ later. The thing is that I really dont know the excatly differences between them to be motivated to change as it works great for now.
    I know that C++ is better and faster for calculations wich is very important for me as this is only what the applications will do. Hours and days of calculations and have really optimized the speed for this.

    But to mention I do have it in mind all the time as I am learning new thing all the time.

    I will look at FindFirstFile() and FileNextFile() to.
    Last edited by Coding; 02-08-2008 at 12:36 PM.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I know that C++ is better and faster for calculations wich is very important for me as this is only what the applications will do. Hours and days of calculations and have really optimized the speed for this.
    There's your answer. Under C++/CLI, most of the code is compiled to MSIL, and thus no faster than C# in execution.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not to mention it's non-standard. If you want to use .NET, then use C#. Slow.
    If you want it to be fast, use Native C++. Standard, fast.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    This was difficult. I beleive I am stuck just to know that FindFirstFile() and FindNextFile() can be used to locate files in a directory/folder on the harddrive and to read in the .txt-files found in a folder to an array.

    The thing is that I have an application on my computer that do exactly this task. The first thing that happens is when I press a button a Dialog appears that look simular like the openFileDialog1.

    I beleive this "Dialog" could be called "SHBrowseForFolder()"

    SHBrowseForFolder() in combination with FindFirstFile(), FindNextFile(), FindClose()
    (Found in this link) http://cboard.cprogramming.com/showthread.php?t=8573&highlight=FindFirstFile%28%2 9

    I found this code on this link that: "This function displays a dialog box that allows a user to select a folder"
    http://msdn2.microsoft.com/en-us/library/aa923860.aspx

    The code do compile when setting it to a button but nothing happens when pressing the button. This should be my first goal.
    Code:
    LPITEMIDLIST WINAPI SHBrowseForFolder(LPBROWSEINFO lpbi);
    Thanks
    Last edited by Coding; 02-08-2008 at 08:36 PM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Do you have an actual question?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm thinking you want to open a SHBrowseForFolder dialog but it fails for some reason when you click the button. So post your code.
    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.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes I have tried out many things and finally found a beginning for a solution.
    I have managed to open a BrowserDialogBox. So the code for this look like this:

    Code:
    folderBrowserDialog1->ShowDialog();
    this->textBox1->Text = folderBrowserDialog1->SelectedPath;
    I have created a textBox1 and what I am trying to do is to browse to a folder that contains *.txt files. When I will doubleclick this folder I will get All .txt files in a list in this textBox1.
    I tried out with the second line but know I perheps have to use FindFirstFile() etc... but dont really know how to begin.
    I do get the directorypath(SelectedPath) in the textBox1 when pressing the button.

    Thanks
    Last edited by Coding; 02-09-2008 at 01:53 PM.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I am trying with something like this to find the first file. I dont know if I am on the right track:
    (This doesn´t compile)

    Code:
    folderBrowserDialog1->ShowDialog();
    			
    HANDLE h;
    WIN32_FIND_DATA info;
    h = FindFirstFile(folderBrowserDialog1->SelectedPath, &info);
    
    this->textBox1->Text = h;
    Last edited by Coding; 02-09-2008 at 02:22 PM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Stop using C++ .NET and use C#. Or if you already are, repost in the C# section. This one is mostly for Native coding, and you are not using Native.
    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.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Okay I will post in the C# section. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc 1d string array
    By s_siouris in forum C Programming
    Replies: 3
    Last Post: 07-07-2008, 09:20 AM
  2. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM