954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

code to use all .htm files in a directory

hi.
question 1:
i am writing a small C script to convert files to another format.
in this case i need my script to use all files in a directory that have extension .htm - how can i do this?
the user will manually specify the directory path but then i want the software to find all .htm files and take their contents to paste them in another file

question 2:
after the user wrote the directory's path (eg. /home/frank/mywebsite) it will save it in a char variable.
when i need to open a certain file I have to specify the path myself, like this

fp = fopen("/var/tmp/string.txt", "r")


but now can i do this

fp = fopen("user_path/some_file.htm", "r")


?

trashed
Light Poster
30 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

>i need my script to use all files in a directory
You can search a directory with opendir and readdir and closedir. Check your man pages for details.

>but now can i do this
You need to build the path string manually:

char user_path[] = "user_path";
char filename[] = "some_file";
char full_path[BUFSIZ];

sprintf ( full_path, "%s/%s.htm", user_path, filename );

fp = fopen ( full_path, "r" );
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You