C language parse file's detail - c

I'm a beginner with C.
I want to parse all the source code (e.g., *.c, *.h) under a directory.
I want to know the file name, size, how many lines in the source.
After searching, I can parse one file's detail (to get how many lines in the source). I can also use system() to know the size and file name, or file list in the directory (without size).
But I have no idea about how to combine all these into one program, so I'm looking for guidance on that.
Thanks all!
below is my code for now
have no idea about next step..
int main (void){
DIR *dp;
FILE *fp;
struct dirent *ep;
dp = opendir ("./");
fp = fopen ("output.txt", "w");
if (dp != NULL)
{
while (ep = readdir (dp))
fprintf(fp,"%s\n", ep->d_name);
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
return 0;
}

Here's the things you need to look in to:
how to iterate over file names, such as with opendir(), readdir() and so on, including while statement for the actual iteration of course.
how to get file details, such as with fstat().
how to open and read files, such as with fopen(), fgetchar() and fclose(), including how to recognise line end characters with if.
That should be the tools you need to start the job, I'd suggest looking in to them then trying to construct your program. Specific problems with the program can then be bought to our attention in other questions.
Note that the examples given above (specifically those in the opendir bullet point) may be platform-specific. If they're not available, you'll need to find equivalents for your platform since standard C does not provide that functionality.

Related

Opening all the files from a local directory/folder [duplicate]

This question already has answers here:
Using a variable file name in C to read from multiple files with similar names?
(2 answers)
Closed 7 years ago.
Using Visual Studio 2015 how would i open and read all the file in a directory.
The Input Parameters for the program are
Number of Sensors (N): Determines the number of input files
File Location: A local directory/folder where the files are located. Each file will be named: sensor_0.txt, sensor_1.txt, ... sensor_(n - 1).txt
I can open and read individual files in the directory by hard coding them using fopen, but since the number of input files is not constant I don't know how I would read all of the files in the directory regardless of how many input files there are.
I was thinking that i would need to create the file names since the only thing changing in the file names is the sensor number but that doesn't seem to work since fopen requires a const char * file name.
I have searched for solutions and i found a DIR variable type in dirent.h header file, but that doesn't work with the the Visual Studio Compiler and a package needs to be installed in order to use that header file.
I am in an intro to programming class so i feel like installing outside programs would be the wrong approach to solving this issue, but I could be wrong. I have also looked into functions like FindFirstFile, and FindNextFile but those also seem too advanced for me.
Any help would be really would be appreciated. Thank you in advance.
If you're writing a Windows-specific application (rather than something that needs to be portable to other operating systems) then look into the FindFirstFile, FindNextFile, and FindClose APIs.
Here's a sample of how to use these APIs (based somewhat on the samples from the above links):
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
if (argc != 2) {
printf("Usage: %s [target_file]\n", argv[0]);
return 1;
}
printf("Target file is %s\n", argv[1]);
hFind = FindFirstFile(argv[1], &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
printf("FindFirstFile failed, error %d\n", GetLastError());
return 1;
}
do {
printf("File name = %s\n", FileFindData.cFileName);
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
return 0;
}
Disclaimer: I haven't had a Windows dev environment years, so I have no way to compile & verify this sample. It should get you pointed in the right direction, though.
You can just do it by hardcoding the base name and iterating with an index to generate the specific name, something like this
for (size_t i = 0 ; ; ++i)
{
char filepath[MAX_PATH];
FILE *file;
// In principle, you should check the return value to ensure
// it didn't truncate the name
snprintf(filepath, sizeof(filepath), "sensor_%d.txt", i);
// Try to open the file, if it fails it's probably because
// the file did not exist, but it's not the only possible
// reason.
file = fopen(filepath, "r"); // Or "rb", depends ...
if ((done = (file == NULL)) != 0)
break; // Cannot open this, probably there are no more files.
// Process the file here
}
A better way would be to pass the name to another function, so you can later change the name generation method by looking at the directory instead of assuming it.
NOTE 1: Secure c Runtime, in MSVC compiler will probably complain about fopen() and snprintf() since snprintf() uses the POSIX name style or something like that (perhaps using the safe version snprintf_s()) I don't remember. But this is standard c (as per C11) so it should compile with any c compiler.
NOTE 2: You should also, use the full path unless the files are in the CWD. Something like (assuming the files are in drive "C:")
snprintf(filepath, sizeof(filepath), "C:\\full\\path\\sensor_%d.txt", i);

Segmentation fault with flex bison and yyparse

I was trying to implement flex and bison when this loop returned segmentation fault whith core dumped
With the first file it worked fine but the next file crashed and printed into terminal segmentation fault.
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("./Corpus")) != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
if ((strcmp(ent->d_name,".") != 0) && (strcmp(ent->d_name,"..") != 0))
{
printf("%s\n",ent->d_name);
yyin = fopen(ent->d_name,"r");
yyparse();
}
}
closedir (dir);
}
else
{
// could not open directory
perror ("");
return EXIT_FAILURE;
}
If you are using a reasonably recent version of flex, there is nothing wrong with switching input files in the way you do it. However, without seeing more code, it is impossible to know for certain what the problem is.
One obvious problem is that you do not check the value of yyin after the call to fopen. If the open fails (which is likely, see below), then yyin will be NULL and that will certainly create a segfault when flex attempts to read.
Also, you don't seem to be closing yyin, which will leak file descriptors. This should not be a problem on the second file, but it will eventually cause the open to fail for lack of file descriptors.
The problem with the fopen is that ent->d_name is just the basename of the file, without any path. So fopen will search for the file in the current working directory. However, the directory being read is ./Corpus, which is a subdirectory; unless the file is duplicated between Corpus and the main directory, the file will not be found.
For the benefit of commentators, the Flex manual states:
If the scanner reaches an end-of-file, subsequent calls are undefined unless either yyin is pointed at a new input file (in which case scanning continues from that file)… Essentially there is no difference between just assigning yyin to a new input file or using yyrestart() to do so; the latter is available for compatibility with previous versions of flex, and because it can be used to switch input files in the middle of scanning.
It is also possible that the segfault is independent of file handling. It would be best to use a debugger to determine where exactly the segfault occurs.

How to discover which files exist in a folder

I have written a program, where it takes an input file, does some operations on it and gives its corresponding output file. i.e., for inp1.txt output is out1.txt, for inp2.txt output is out2.txt and so on, both in different folders.
Right now I have used a file_count variable and have used switch case method, to open the particular file.
The problem is, if I add one more file to the folder, then I have to re-edit the program with another case statement.
Please suggest me the usage of directory pointer in , I browsed all over the net but didnt get an exact solution.
Thanks a lot in anticipation.
There's no way to read the contents of a directory using only standard C APIs, so you'll have to use platform-specific APIs instead:
On *nix systems, you use opendir(3)/readdir(3)/closedir(3) to read the contents of a directory.
On Windows systems, you use FindFirstFile/FindNextFile/FindClose
If you know the file names in advance ie. they follow a pattern like this: f1.txt f2.txt fn.txt then you can loop over the files:
for (int i = 0 -> num_files)
char * filename;
filename = create_your_filename(i)
open(filename)
dostuff
close(filename)
you can follow the same pattern or even edit the filename for the output files.
Otherwise you can also call the program with all the input files in the command:
your_program *.txt
Then all of the file names will appear in argv[], which you can iterate over.
you have to find the files in input folder in runtime before processing. you can sort the files by extension,file name,created date etc...
Here is a simple function that show you if a file (in parameters) is in the path you put in argument.
Return 0 if file exit, 1 if not.
int is_file_enabled(char *path, char *filename)
{
char exec[255], line[255];
sprintf(exec, "ls %s | grep \"%s\"", path, filename);
FILE* cmd_res = popen(exec, "r");
if (cmd_res != NULL)
while (fgets(line, sizeof(line), cmd_res) != NULL)
if (line != NULL) { pclose(cmd_res); return 0; }
pclose(cmd_res);
return 1;
}

Trouble testing copy file function in C

Okay so this is probably has an easy solution, but after a bit of searching and testing I remain confused.. :(
Here is a snippet of the code that I have written:
int main(int argc, char *argv[]){
int test;
test = copyTheFile("test.txt", "testdir");
if(test == 1)
printf("something went wrong");
if(test == 0)
printf("copydone");
return 0;
}
int copyTheFile(char *sourcePath, char *destinationPath){
FILE *fin = fopen(sourcePath, "r");
FILE *fout = fopen(destinationPath, "w");
if(fin != NULL && fout != NULL){
char buffer[10000];//change to real size using stat()
size_t read, write;
while((read = fread(buffer, 1, sizeof(buffer), fin)) > 0){
write = fwrite(buffer, 1, read, fout);
if(write != read)
return 1;
}//end of while
}// end of if
else{
printf("Something wrong getting the file\n");
return 0;}
if(fin != NULL)
fclose(fin);
if(fout != NULL)
fclose(fout);
return 0;
}
Some quick notes: I am very new to C, programming, and especially file I/O. I looked up the man pages of fopen, fread, and fwrite. After looking at some example code I came up with this. I was trying to just copy a simple text file, and then place it in the destination folder specified by destinationPath.
The folder I want to place the text file into is called testdir, and the file I want to copy is called test.txt.
The arguments I have attempted to use in the copyFile function are:
"test.txt" "testdir"
".../Desktop/project/test.txt" ".../Desktop/project/testdir"
"/Desktop/project/test.txt" "/Desktop/project/testdir"
I just get the print statement "Something wrong getting the file" with every attempt. I am thinking that it may be because 'testdir' is a folder not a file, but then how would I copy to a folder?
Sorry if this a really basic question, I am just having trouble so any advice would be awesome!
Also, if you wanted to be extra helpful, the "copyTheFile" function is supposed to copy the file regardless of format. So like if its a .jpg or something it should copy it. Let me know if any of you guys see a problem with it.
This is with ISO/POSIX/C89/C99 on Linux.
At the start, you'll want to include stdio.h to provide FILE and the I/O function declarations:
#include <stdio.h>
Aside from this, your program compiles and works properly for me. Unfortunately you can't copy to a directory without using stat() to detect if the destination is a directory, and if so, appending a file name before opening the file.
Some other minor suggestions:
A buffer with a power of two bytes such as 4096 is probably more efficient due to it lining up with filesystem and disk access patterns
Conventionally, C functions that return a status code use 0 for success and other values such as 1 for failure, so swapping your return values may be less confusing
When a standard library function such as fopen, fread or fwrite fails, it is a good idea to use perror(NULL); or perror("error prefix"); to report it, which may look something like:
$ ./a.out
...
error prefix: No such file or directory
if you are trying to write a new file in a directory, you should be giving the full path of the file to be written. in your case
"C:...\Desktop\project\testdir\testfile"

Reading multiple text files in C

What is the correct way to read and extract data from text files when you know that there will be many in a directory? I know that you can use fopen() to get the pointer to the file, and then do something like while(fgets(..) != null){} to read from the entire file, but then how could I read from another file? I want to loop through every file in the directory.
Sam, you can use opendir/readdir as in the following little function.
#include <stdio.h>
#include <dirent.h>
static void scan_dir(const char *dir)
{
struct dirent * entry;
DIR *d = opendir( dir );
if (d == 0) {
perror("opendir");
return;
}
while ((entry = readdir(d)) != 0) {
printf("%s\n", entry->d_name);
//read your file here
}
closedir(d);
}
int main(int argc, char ** argv)
{
scan_dir(argv[1]);
return 0;
}
This just opens a directory named on the command line and prints the names of all files it contains. But instead of printing the names, you can process the files as you like...
Typically a list of files is provided to your program on the command line, and thus are available in the array of pointers passed as the second parameter to main(). i.e. the invoking shell is used to find all the files in the directory, and then your program just iterates through argv[] to open and process (and close) each one.
See p. 162 in "The C Programming Language", Kernighan and Ritchie, 2nd edition, for an almost complete template for the code you could use. Substitute your own processing for the filecopy() function in that example.
If you really need to read a directory (or directories) directly from your program, then you'll want to read up on the opendir(3) and related functions in libc. Some systems also offer a library function called ftw(3) or fts(3) that can be quite handy too.

Resources