How to use files in a path?

This is a discussion on How to use files in a path? within the C Programming forums, part of the General Programming Boards category; Hi, I need some help with a program. The thing is that I can't find a function to do the ...

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Baires
    Posts
    12

    How to use files in a path?

    Hi, I need some help with a program.
    The thing is that I can't find a function to do the following,

    For example:

    I have 1.txt, 2.txt, 3.txt in C:\files

    And I need to work with those files with a program, but i don't know how to read the path and save the file names in an array or something, to use them later.

    Anyone please help!

    Thanks!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Read the path from where?

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    Baires
    Posts
    12
    A function or something...

    The main thing is to save the file names in an array or buffer...

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    But it has to be available to the program somehow. So it's either hard-coded in the source or supplied on stdin.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes, but read the path from where? A file? Hardcoded into your program? Something the user types in? An open file dialog?

  6. #6
    Registered User
    Join Date
    May 2009
    Location
    Baires
    Posts
    12
    Hardcoded into the program

    To read always from the same path.

    Thanks!

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then you just have to type the path into your program, like
    Code:
    const char *filenames[] = {"path\\1", "path\\2"};
    You need to use two backslashes to get a literal backslash into your string.

  8. #8
    Registered User
    Join Date
    May 2009
    Location
    Baires
    Posts
    12
    But the thing is that I won't always know the names of the files, so I need to read all of them, and save the names in an array to use them.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by guriwashere View Post
    But the thing is that I won't always know the names of the files, so I need to read all of them, and save the names in an array to use them.
    But where are you planning to read them from? Do you intend to do a dir/ls/moral equivalent to get a list of the files in the directory, or what?

  10. #10
    Registered User
    Join Date
    May 2009
    Location
    Baires
    Posts
    12
    Quote Originally Posted by tabstop View Post
    But where are you planning to read them from? Do you intend to do a dir/ls/moral equivalent to get a list of the files in the directory, or what?
    YES, I need to do a list of the files in a path, and save them in an array.

    I don't care how to do it, I just need that.

    Sorry for my english, I'm from Argentina

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by guriwashere View Post
    But the thing is that I won't always know the names of the files, so I need to read all of them, and save the names in an array to use them.
    You are interested in the functions opendir(), readdir(), and closedir()
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by guriwashere View Post
    YES, I need to do a list of the files in a path, and save them in an array.

    I don't care how to do it, I just need that.

    Sorry for my english, I'm from Argentina
    No one could have ever asked that before, surely. Have you searched the boards? Even better, have you searched the FAQs?

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    So the path is known? Ie, you could hardcode the path:
    Code:
    path[]="/some/place/";
    Read up on opendir(). You can use that to create an array of files in the directory.

    If you have to recurse into the tree, that is more work.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM

Tags for this Thread


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