I have a set of txt files in a folder. These txt files have ASCII values, and I am converting the ASCII values to decimal values and storing them in an excel file (CSV). I am able to run the code and get correct results for one file where I manually provide the name of the file containing ASCII values as well as the CSV file.
Now, my goal is to automate this process for all the txt files in the folder. Currently, in my code attached below, I am manually providing the CSV file name where I want to store the decimal data (I will later modify my code such that the data from each txt file is stored in a new csv file, but for now I am just providing the filename). I am trying to get the file name using the functions in dirent.h
header file. But I am getting a segmentation fault at line fp1=fopen(entry->d_name,"r");
. I am not sure what is causing the problem. Can someone please help me resolve the issue so that I am able to loop through all the text files?
Thank you.
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
static void scan_dir(const char *dir)
{
FILE *fp1,*fp2;
struct dirent * entry;
DIR *d = opendir( dir );
char c;
int dec=0,k=0,stop=0;
if (d == 0) {
perror("opendir");
return;
}
while ((entry = readdir(d)) != 0) {
printf("%s\n", entry->d_name);
const char *dot = strrchr(entry->d_name, '.');
printf("%s\n",dot 1);
if(!(strcmp(dot 1,"txt"))){
//read your file here
fp1=fopen(entry->d_name,"r");
fp2 = fopen("decimal.csv","w");
while((c=fgetc(fp1))!=EOF){ /*code to convert ASCII values to decimal*/
dec=dec | c<< (8*k);
k ;
if(k==4){
k=0;
dec =dec>>14;
fprintf(fp2,"%d\n",dec);
//printf("%d\n",dec);
if(dec==-1){
stop=1;
}
dec=0;
}
if(stop==1){
stop=0;
break;
}
}
}
fclose(fp1);
fclose(fp2);
}
closedir(d);
}
int main()
{
scan_dir("C:/Users/Downloads/Newfolder");
return 0;
}
CodePudding user response:
In the line
fp1=fopen(entry->d_name,"r");
entry->d_name is the name of the file without the path so the file can be found and the pointer fp1 will be null, more over the file were closed without been opened an this bring out the crash.
The first part of you function could be fixed in the following way
static void scan_dir(const char *dir)
{
FILE *fp1,*fp2;
struct dirent * entry;
DIR *d = opendir( dir );
char c;
int dec=0,k=0,stop=0;
if (d == 0) {
perror("opendir");
return;
}
while ((entry = readdir(d)) != 0) {
printf("%s\n", entry->d_name);
char fname[256];
strcpy(fname,dir);
strcat(fname, entry->d_name);
const char *dot = strrchr(entry->d_name, '.');
printf("%s\n",dot 1);
if(!(strcmp(dot 1,"txt"))){
//read your file here
fp1=fopen(fname,"r");
fp2 = fopen("decimal.csv","w");
while((c=fgetc(fp1))!=EOF){ /*code to convert ASCII values to decimal*/
dec=dec | c<< (8*k);
k ;
if(k==4){
k=0;
dec =dec>>14;
fprintf(fp2,"%d\n",dec);
printf("%d\n",dec);
if(dec==-1){
stop=1;
}
dec=0;
}
if(stop==1){
stop=0;
break;
}
}
fclose(fp1);
fclose(fp2);
}
}
closedir(d);
}
then you have to modify the saving in the cvs file because you replace the previous converted dec value with the last one .