We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,692 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

C program to open files

Hi,
I wanted to write a C program which searches for text files in a directory(assuming the directory and files are not write protected),list them and open each one of them(files) write a letter A in them.Can anyone give me a sample code. I believe it can be done with shell embedded in C..........

Hope someone can throw some light here.

5
Contributors
9
Replies
4 Months
Discussion Span
1 Year Ago
Last Updated
10
Views
Question
Answered
digitalhead
Newbie Poster
4 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What do you know about reading directories in C?

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

What do you know about reading directories in C?

Well I used the find shell command to list files.....but then how to open these individual files.....???

digitalhead
Newbie Poster
4 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Well, you can use the shell command to write the list into a file, then your program can read that file to retrieve the files.

Since I'm not familiar with a "find shell command". what OS are you using? When dealing with directories, that's an important piece of information.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

Like WaltP said, you should have the command write to a file and then read from that file. You should probably use the "system" function in stdlib.h. You don't have to "embed" a shell inside of your C program. Also, the good thing about the find command is it lists all the files it finds line by line, so you only need to use a function like "fgets" to read the filenames from the file generated from find's output.

chrjs
Junior Poster in Training
96 posts since Feb 2011
Reputation Points: 58
Solved Threads: 20
Skill Endorsements: 0

Like WaltP said, you should have the command write to a file and then read from that file. You should probably use the "system" function in stdlib.h. You don't have to "embed" a shell inside of your C program. Also, the good thing about the find command is it lists all the files it finds line by line, so you only need to use a function like "fgets" to read the filenames from the file generated from find's output.

Hi,
Thanks for the reply.....I used the same approach before....like using find to list files which are saved in a file. From that file I read every line(by fgets()) and tried to open ....but the files are not opening.....Can you give me a sample code for this....to understand better....By the way I am using Ubuntu 10.04

digitalhead
Newbie Poster
4 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi,
Thanks for the reply.....I used the same approach before....like using find to list files which are saved in a file. From that file I read every line(by fgets()) and tried to open ....but the files are not opening.....Can you give me a sample code for this....to understand better....By the way I am using Ubuntu 10.04

Hi,
I fixed it.....ran smoothly...... :)

digitalhead
Newbie Poster
4 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 2 Years Ago by WaltP and chrjs

I see that this thread has been marked as solved but I had a quick question for the OP.

Did you handle the case when the Directory contained sub directories as well ?

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
Skill Endorsements: 0

How can i open c++ from ms dos suggest me very fast

ashok.bandari88
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

How to open c++ from msdos

ashok.bandari88
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0845 seconds using 2.71MB