C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-07-2009, 05:13 PM   #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!!
guriwashere is offline   Reply With Quote
Old 06-07-2009, 05:14 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Read the path from where?
tabstop is offline   Reply With Quote
Old 06-07-2009, 05:17 PM   #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...
guriwashere is offline   Reply With Quote
Old 06-07-2009, 05:31 PM   #4
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
But it has to be available to the program somehow. So it's either hard-coded in the source or supplied on stdin.
itCbitC is offline   Reply With Quote
Old 06-07-2009, 05:32 PM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Yes, but read the path from where? A file? Hardcoded into your program? Something the user types in? An open file dialog?
tabstop is offline   Reply With Quote
Old 06-07-2009, 05:35 PM   #6
Registered User
 
Join Date: May 2009
Location: Baires
Posts: 12
Hardcoded into the program

To read always from the same path.

Thanks!
guriwashere is offline   Reply With Quote
Old 06-07-2009, 05:40 PM   #7
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 06-07-2009, 07:19 PM   #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.
guriwashere is offline   Reply With Quote
Old 06-07-2009, 07:25 PM   #9
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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?
tabstop is offline   Reply With Quote
Old 06-07-2009, 07:45 PM   #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
guriwashere is offline   Reply With Quote
Old 06-07-2009, 07:50 PM   #11
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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()
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 06-07-2009, 07:51 PM   #12
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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?
tabstop is offline   Reply With Quote
Old 06-07-2009, 07:54 PM   #13
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,945
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Reply

Tags
array, file names

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error opening files in a different dir Ozzie C++ Programming 3 10-09-2008 06:55 AM
Linking header files, Source files and main program(Accel. C++) Daniel Primed C++ Programming 3 01-17-2006 11:46 AM
Multiple Cpp Files w4ck0z C++ Programming 5 11-14-2005 02:41 PM
Folding@Home Cboard team? jverkoey General Discussions 398 10-11-2005 08:44 AM
Batch file programming year2038bug Tech Board 10 09-05-2005 03:30 PM


All times are GMT -6. The time now is 07:50 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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