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.

Dani AI

Generated

A few practical points that explain why the OP’s approach (using find -> save list -> fgets -> fopen) often fails, and two short, safe ways to do it.

Reading lines produced by find is fine, but fgets keeps the trailing newline (and possibly a CR if the list came from Windows). That extra character makes fopen fail. Filenames with spaces or newlines also break simple line-based parsing. and were on the right track; the usual fixes are: strip CR/LF before calling fopen, make sure the path is correct relative to the program’s working directory (or use absolute paths), and check permissions with perror/errno so failures are visible.

A minimal, working pattern that fixes the newline problem:

#include <stdio.h>
#include <string.h>
#include <limits.h>

#ifndef PATH_MAX
#define PATH_MAX 4096
#endif

char path[PATH_MAX];
FILE *list = fopen("filelist.txt", "r");
while (fgets(path, sizeof path, list)) {
    path[strcspn(path, "\r\n")] = '\0';   // remove CR/LF
    FILE *f = fopen(path, "a");
    if (!f) { perror(path); continue; }
    fputc('A', f);
    fclose(f);
}
fclose(list);

For robustness (handles spaces, embedded newlines, and recursion asked about by ): either walk directories in C with opendir/readdir and stat (skip non-regular files), or have find produce NUL-delimited names and read those (use find . -type f -print0 and read until '\0', or use getdelim(&buf,&len,'\0',stream) on POSIX systems). Always check S_ISREG(st_mode) before writing and use perror to report exact failures.

Cautions: back up files before batch-modifying, avoid appending to binary files, and ensure the user running the program has write permission. Printing filenames with visible delimiters (for example printf("'%s'\n", path)) helps spot stray whitespace or CR characters during debugging.

Recommended Answers

All 9 Replies

What do you know about reading directories in C?

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.....???

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.

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.

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

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...... :)

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 ?

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

How to open c++ from msdos

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.