Pointer being freed was not allocated, Abort trap: 6 - c

I'm not proficient in C programming so please excuse me if this isn't a strong question. In the following code, I can only allocate memory to samplesVecafter obtaining the value of nsamplepts, but I need to return the vector samplesVec to the main for further use (not yet coded). However, I'm getting the following error:
Error in Terminal Window:
ImportSweeps(3497,0x7fff7b129310) malloc: * error for object 0x7fdaa0c03af8: pointer being freed was not allocated
* set a breakpoint in malloc_error_break to debug
Abort trap: 6
I'm using Mac OS X Mavericks with the gcc compiler. Thanks for any help.
*EDITED!!! AFTER VALUABLE INPUTS FROM COMMENTATORS, THE FOLLOWING REPRESENTS A SOLUTION TO THE ORIGINAL PROBLEM (WHICH IS NO LONGER AVAILABLE) *
The following code modification seemed to solve my original questions. Thanks for the valuable inputs everyone!
/* Header Files */
#define LIBAIFF_NOCOMPAT 1 // do not use LibAiff 2 API compatibility
#include <libaiff/libaiff.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <math.h>
/* Function Declarations */
void FileSearch(char*, char*, char*, char*, char*);
int32_t *ImportSweeps(char*);
/* Main */
int main()
{
char flag1[2] = "N";
char binname[20] = "bin1"; // dummy assignment
char buildfilename[40] = "SweepR";
char skeletonpath[100] = "/Users/.../Folder name/";
int k, len;
/* Find the sweep to be imported in the directory given by filepath */
FileSearch(skeletonpath, binname, buildfilename, skeletonpath, flag1);
if (strcmp(flag1,"Y")) {
printf("No file found. End of program.\n");
} else {
len = (int) strlen(skeletonpath);
char *filepath = malloc(len);
for (k = 0; k < len; k++) {
filepath[k] = skeletonpath[k];
}
printf("File found! Filepath: %s\n", filepath);
// Proceed to import sweep
int32_t *sweepRfile = ImportSweeps(filepath);
if (sweepRfile) {
printf("Success!\n");
// Do other things with sweepRfile
free(sweepRfile);
}
free(filepath);
}
return 0;
}
/* Sub-Routines */
void FileSearch(char *dir, char *binname, char *buildfilename, char* filepath, char* flag1)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"Cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/* Found a directory, but ignore . and .. */
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
continue;
strcpy(binname,entry->d_name);
strcpy(buildfilename,"SweepR");
/* Recurse at a new indent level */
FileSearch(entry->d_name, binname, buildfilename, filepath, flag1);
}
else {
sprintf(buildfilename, "%s%s.aiff", buildfilename, binname);
if (strcmp(entry->d_name,buildfilename)) {
strcpy(buildfilename,"SweepR");
} else {
sprintf(filepath, "%s%s/%s", filepath, binname, buildfilename);
strcpy(flag1,"Y");
break;
}
}
}
chdir("..");
closedir(dp);
}
int32_t *ImportSweeps(char *filepath)
{
char *filepathread = filepath;
/* Initialize files for importing */
AIFF_Ref fileref;
/* Intialize files for getting information about AIFF file */
uint64_t nSamples;
int32_t *samples = NULL;
int32_t *samplesVec = NULL;
int channels, bitsPerSample, segmentSize, ghost, nsamplepts;
double samplingRate;
/* Import Routine */
fileref = AIFF_OpenFile(filepathread, F_RDONLY) ;
if(fileref)
{
// File opened successfully. Proceed.
ghost = AIFF_GetAudioFormat(fileref, &nSamples, &channels, &samplingRate, &bitsPerSample, &segmentSize);
if (ghost < 1)
{
printf("Error getting audio format.\n");
AIFF_CloseFile(fileref); return (int32_t) 0;
}
nsamplepts = ((int) nSamples)*channels;
samples = malloc(nsamplepts * sizeof(int32_t));
samplesVec = malloc(nsamplepts * sizeof(int32_t));
ghost = AIFF_ReadSamples32Bit(fileref, samples, nsamplepts);
if (ghost) {
for (int k = 0; k < nsamplepts; k++) {
samplesVec[k] = *(samples+k);
}
}
free(samples);
AIFF_CloseFile(fileref);
}
return samplesVec;
}

So... as far as I can see... :-)
samplesVec, the return value of ImportSweeps is not initialized, if fileref is false. Automatic (== local) variables have no guarantees on its value if samplesVec are not explicitly initialized - in other words samplesVec could carry any address. If samplesVec is not NULL on luck (which on the other hand might be often the case), you try free a not allocated junk of memory, or by very bad luck an somewhere else allocated one.
If I'm correct with my guess you can easy fix this with:
int32_t *samples;
int32_t *samplesVec = NULL;
It is a good idea anyway to initialize any variable as soon as possible with some meaningful error or dummy value, if you not use it in the very next line. As pointers are horrible beasts, I always NULL them if I don't initialize them with a useful value on declaration.
Edit: Several minor small changes for a readable approximation to English. :-)

If AIFF_OpenFile fails, ImportSweeps returns an undefined value because samplesVec wasn't initialized. If that value is non-NULL, main will try to free it. You can either initialize samplesVec = NULL, or you can reorganize the code as
fileref = AIFF_OpenFile(filepathread, F_RDONLY) ;
if(!fileref) {
{
// print error message here
return NULL;
}
// File opened successfully. Proceed.
...
There are people who will insist a functon that should only have one exit -- they are poorly informed and voicing a faulty dogma handed down from others who are likewise uninformed and dogmatic. The check for error and return above is known as a guard clause. The alternate style, of indenting every time a test succeeds, yields the arrow anti-pattern that is harder to read, harder to modify, and more error prone. See http://blog.codinghorror.com/flattening-arrow-code/ and http://c2.com/cgi/wiki?ArrowAntiPattern for some discussion.

Related

C program to count specific words in FILE

The objective of the program is to rate a person's resume. The program should open and read two .txt type FILES. One of the files contains the keywords and the other is the resume itself. The process of the program consists in looping through the keywords.txt and then try to find a similar word in the resume.txt. I got it almost working but the program seems to be considering the first space as the end of the file in the keywords FILE.
This is what I have:(I tried switching the first word on the keywords and the count seems to work/would be goo to scan only characters without symbols and its necessary to count the occurrence of every single keyword)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
FILE* txtKey;
FILE* txtResume;
char keyWords[1000];
char word[10000];
int count;
txtKey=fopen("keywords.txt", "r");
if(txtKey == NULL){
printf("Failed to open txtKey file \n");
return 1;
}
txtResume=fopen("resume.txt", "r");
if(txtResume == NULL){
printf("Failed to open txtResume file \n");
return 1;
}
while (fscanf(txtKey, "%s", keyWords) != EOF)
{
while (fscanf(txtResume, "%s", word) != EOF)
{
if (strstr(word, keyWords) != NULL)
{
count++;
}
}
}
printf("The keywords were found %d times in your resume!", count);
fclose(txtResume);
fclose(txtKey);
return 0;
}//END MAIN
Note: This is prefaced by my top comments.
I've created a word list struct that holds a list of words. It is used twice. Once, to store the list of keywords. And, a second time to parse the current line of the resume file.
I coded it from scratch, because it's somewhat different than what you had:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef DEBUG
#define dbgprt(_fmt...) \
do { \
printf(_fmt); \
} while (0)
#else
#define dbgprt(_fmt...) \
do { \
} while (0)
#endif
typedef struct {
int list_max;
int list_cnt;
char **list_words;
} list_t;
list_t keywords;
list_t linewords;
char buf[10000];
int
wordsplit(FILE *xf,list_t *list,int storeflg)
{
char *cp;
char *bp;
int valid;
if (! storeflg)
list->list_cnt = 0;
do {
cp = fgets(buf,sizeof(buf),xf);
valid = (cp != NULL);
if (! valid)
break;
bp = buf;
while (1) {
cp = strtok(bp," \t\n");
bp = NULL;
if (cp == NULL)
break;
// grow the list
if (list->list_cnt >= list->list_max) {
list->list_max += 100;
list->list_words = realloc(list->list_words,
sizeof(char *) * (list->list_max + 1));
}
if (storeflg)
cp = strdup(cp);
list->list_words[list->list_cnt++] = cp;
list->list_words[list->list_cnt] = NULL;
}
} while (0);
return valid;
}
void
listdump(list_t *list,const char *tag)
{
char **cur;
dbgprt("DUMP: %s",tag);
for (cur = list->list_words; *cur != NULL; ++cur) {
dbgprt(" '%s'",*cur);
}
dbgprt("\n");
}
int
main(void)
{
FILE *xf;
int count;
xf = fopen("keywords.txt","r");
if (xf == NULL)
return 1;
while (1) {
if (! wordsplit(xf,&keywords,1))
break;
}
fclose(xf);
listdump(&keywords,"KEY");
count = 0;
xf = fopen("resume.txt","r");
if (xf == NULL)
return 2;
while (1) {
if (! wordsplit(xf,&linewords,0))
break;
listdump(&linewords,"CUR");
for (char **str = linewords.list_words; *str != NULL; ++str) {
dbgprt("TRYCUR: '%s'\n",*str);
for (char **key = keywords.list_words; *key != NULL; ++key) {
dbgprt("TRYKEY: '%s'\n",*key);
if (strcmp(*str,*key) == 0) {
count += 1;
break;
}
}
}
}
fclose(xf);
printf("keywords found %d times\n",count);
return 0;
}
UPDATE:
Any option to make it simpler? I don't think I know all the concepts of this answer, although tis result is perfect.
Yes, based on your code, I realized that what I did was a bit advanced. But, by reusing the list as I did, it actually saved a bit of replicated code (e.g. Why have separate parsing code for the keywords and resume data when they are both very similar.
There's standard documentation for all the libc functions (e.g. fgets, strtok, strcmp).
If you know the [maximum] number of keyword beforehand [this is possible to do], you could use a fixed size char ** array [similar to what you had].
Or, you could just do a realloc on a char **keywords array on every new keyword (e.g. cp). And, maintain a separate count variable (e.g. int keycnt). This would be fine if we only needed one list (i.e. we could forego the list_t struct).
We could replicate some of the keyword code for the second loop in main, and again, use different variables for the array and its count.
But, this is wasteful. list_t is an example of using realloc efficiently (i.e. calling it less often). This is a standard technique.
If you do a websearch on dynamic resize array realloc, one of entries you'll find is: https://newton.ex.ac.uk/teaching/resources/jmr/appendix-growable.html
Note the use of strdup to preserve the word values for the keyword list beyond the next call to fgets.
Hopefully, that covers enough so you can study it a bit. The whole "how to implement a dynamic resizing array using realloc?" shows up quite frequently a question on SO, so you could also search here for a question on it.
Also, how could it word if the keywords.txt list has words separated by ","?
To parse by ",", just change the second arg to strtok to include it (e.g. " \t,\n"). That will work for abc def, abc,def, or abc, def.

Is there a way to chdir() into a directory where only the beginning of the directory name is known?

So, basically, I have a program that forks off a child process and creates a directory with the name + the child processes ID. This is done in another part of the code.
So, lets say the user names the directory "TestDir#". It will be TestDir12342 or something similar in the end.
So, later on, the user could enter a search term for that directory by typing in TestDir#. I want to lope off the "#", and have chdir() search for a directory that begins with that name, "TestDir". I don't have to worry about repeat files or similarly named files for this program.
Does anyone know a simple way to do this with chdir()? I have tried many different test code, but I am at a lose.
I have also attempted to store the child process ID in the parent process, but for some reason I can never get them to match. I am aware that fork() gives the child process ID in return to the parent. Yet, for some reason, the program refuses to make them match.
So, I am trying this as a workaround (searching the beginning part of the file name). Thanks for any assistance if someone knows of a way to do this.
readdir can be used to get the entries of the directory.
The following searchFirstDir finds the first prefix-matched directory. (tested in Ubuntu Linux)
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
int searchFirstDir(const char *workingDir, const char *prefix, char *resultBuffer, int bufferLen)
{
DIR *pDir = NULL;
int found = 0;
// opendir
{
pDir = opendir(workingDir);
if (pDir == NULL) {
perror("ERROR: opendir");
return -1;
}
}
// readdir
{
int ret;
struct dirent *pEntry;
struct dirent *result;
int prefixLen = strlen(prefix);
// refer: man readdir (in Linux)
{
long name_max = pathconf(workingDir, _PC_NAME_MAX);
if (name_max == -1) /* Limit not defined, or error */
name_max = 255; /* Take a guess */
size_t len = offsetof(struct dirent, d_name) + name_max + 1;
pEntry = malloc(len);
}
do {
ret = readdir_r(pDir, pEntry, &result);
if (ret) {
perror("ERROR: readdir_r");
break;
}
if (pEntry->d_type == DT_DIR && strncmp(pEntry->d_name, prefix, prefixLen) == 0) {
strncpy(resultBuffer, pEntry->d_name, bufferLen);
found++;
break;
}
} while(ret == 0 && result != NULL);
free(pEntry);
}
// closedir
closedir(pDir);
return found > 0 ? 0 : -1;
}
int main(int argc, char *argv)
{
char resultBuffer[255];
int ret = searchFirstDir("workingdirectory", "TestDir", resultBuffer, 255);
if (ret == 0) {
printf("First matched directory: %s\n", resultBuffer);
}
}
Yes, there is a way to perform the requested type of chdir taking advantage of globbing, i.e. filename expansion using a wildcard of "*", as follows:
#include <string.h>
#include <glob.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/* Convert a wildcard pattern into a list of blank-separated
filenames which match the wildcard. */
char * glob_pattern(char *wildcard)
{
char *gfilename;
size_t cnt, length;
glob_t glob_results;
char **p;
glob(wildcard, GLOB_NOCHECK, 0, &glob_results);
/* How much space do we need? */
for (p = glob_results.gl_pathv, cnt = glob_results.gl_pathc;
cnt; p++, cnt--)
length += strlen(*p) + 1;
/* Allocate the space and generate the list. */
gfilename = (char *) calloc(length, sizeof(char));
for (p = glob_results.gl_pathv, cnt = glob_results.gl_pathc;
cnt; p++, cnt--)
{
strcat(gfilename, *p);
if (cnt > 1)
strcat(gfilename, " ");
}
globfree(&glob_results);
return gfilename;
}
int main() {
char *directory;
int ret;
directory = glob_pattern("te*");
ret = chdir (directory);
printf("Result of chdir: %d\n",ret);
}
Note: The "globbing" portion of the code comes from here
Linux has a glob utility so if you wish to do the same in C, you have to write the code yourself as this example portrays. When the program finishes however you will be back in the directory you originally used to run this script. When the code does a successful directory change, the return result is zero. Note, this code executed in a directory containing a subdirectory named "test".

An element of a struct loses its value after a function is called passing another struct as argument

I have a problem that I can't figure out. I have the following files: file_reader.c, file_reader.h, file_writer.c, file_writer.h, test_file_reader.c
I'm working with 'struct' to read and write files. For better understanding I wrote the following code test_file_reader.c:
#include <stdio.h>
#include "file_reader.h"
#include "file_writer.h"
int main ()
{
char *file_path = "/home/freitas/Dropbox/projects/gcleaner/cleaners/custom.xml";
struct FileReader *fr = malloc(sizeof(struct FileReader));
file_reader_new (file_path, fr);
show_file_reader_values(fr);
struct FileWriter *fw = malloc(sizeof(struct FileWriter));
fw->file_path = "/tmp/text1.txt";
fw->content = "aaa";
write (fw);
show_file_reader_values(fr);
return 0;
}
void show_file_reader_values(const struct FileReader *fr)
{
printf("==========FILE READER==========\n");
printf("file path: %s\n", fr->file_path);
printf("----------file content---------\n");
printf("content:\n%s\n", fr->content);
printf("----------file content---------\n");
printf("n lines: %d\n", fr->n_lines);
printf("n characters: %d\n", fr->n_characters);
printf("==========FILE READER==========\n\n");
}
The function 'file_reader_new' reads the file and then signs the content, file path, number of lines and number of characters to the 'struct' 'FileReader'.
When I call the function 'show_file_reader_values' in the first time I do not have problems with the content but when I call the function 'write' and then call the function 'show_file_reader_values' again, the content is not the same anymore. The question is that the function 'write' of the file 'file_writer.c' and its struct does not have any relation to the file 'file_reader' and its struct. So, how can a function using another struct change the values of another struct of another file ?
The output:
[freitas#localhost test]$ ./test_file_reader
==========FILE READER==========
file path: /home/freitas/Dropbox/projects/gcleaner/cleaners/custom.xml
----------file content---------
content:
<cleaner> <id>k3b</id> <label>k3b</label> <description>Disc writing software</description> <option> <id>log</id> <label>Log</label> <description>Delete the log file which contains information about the last writing session(s).</description> <command>delete</command> <search>glob</search> <path>~/.kde/share/apps/k3b/*.log</path> </option> <option> <id>log2</id> <label>Log</label> <description>Delete the log file which contains information about the last writing session(s).</description> <command>delete</command> <search>glob</search> <path>~/.kde/share/apps/k3b/*.log</path> </option> </cleaner>
----------file content---------
n lines: 1
n characters: 621
==========FILE READER==========
==========FILE READER==========
file path: /home/freitas/Dropbox/projects/gcleaner/cleaners/custom.xml
----------file content---------
content:
<cleaner> <id>k��U�N
----------file content---------
n lines: 1
n characters: 621
==========FILE READER==========
Did you see ? In the first call I had the entire output:
<cleaner> <id>k3b</id> <label>k3b</label> <description>Disc wri...
but in the second call I had:
<cleaner> <id>k��U�N
file_reader.c
#include <stdio.h>
#include <stdlib.h>
#include "file_reader.h"
int file_reader_new(const char *file_path, struct FileReader *fr)
{
char *content; // holds the file content.
int counter; // holds the file number of lines.
size_t i; // indexing into content.
size_t buffer_size; // size of the content.
char *temp; // for realloc().
char c; // for reading from the input.
FILE *input; // our input stream.
if ((input = fopen(file_path, "r")) == NULL) {
fprintf(stderr, "Error opening input file %s\n", file_path);
exit(EXIT_FAILURE);
}
/* Initial allocation of content */
counter = 0;
i = 0;
buffer_size = BUFSIZ;
if ((content = malloc(buffer_size)) == NULL) {
fprintf(stderr, "Error allocating memory (before reading file).\n");
fclose(input);
}
while ((c = fgetc(input)) != EOF) {
/* Enlarge content if necessary. */
if (i == buffer_size) {
buffer_size += BUFSIZ;
if ((temp = realloc(content, buffer_size)) == NULL) {
fprintf(stderr, "Ran out of core while reading file.\n");
fclose(input);
free(content);
exit(EXIT_FAILURE);
}
content = temp;
}
/* Add input char to the content. */
content[i++] = c;
/* If the character is a break of line
* then the counter will be incremented.
*/
if (c == '\n')
counter++;
}
/* Test if loop terminated from error. */
if (ferror(input)) {
fprintf(stderr, "There was a file input error.\n");
free(content);
fclose(input);
exit(EXIT_FAILURE);
}
/* Make the content a bona-fide string. */
if (i == buffer_size) {
buffer_size += 1;
if ((temp = realloc(content, buffer_size)) == NULL) {
fprintf(stderr, "Ran out of core (and only needed one more byte too ;_;).\n");
fclose(input);
free(content);
exit(EXIT_FAILURE);
}
content = temp;
}
content[i] = '\0';
/* Assigns the variables to the corresponding
* element of the struct.
*/
fr->file_path = file_path;
fr->content = content;
fr->n_lines = counter;
fr->n_characters = i;
/* Clean up. */
free(content);
fclose(input);
return 0;
}
file_reader.h
#ifndef FILE_READER_H_
#define FILE_READER_H_
typedef struct FileReader
{
char *content; // holds the file content.
char *file_path; // holds the file path.
int *n_lines; // holds the number of lines.
int *n_characters; // holds the number of characters.
} FileReader;
// file_reader_new - reads the file
int file_reader_new(const char *file_path, struct FileReader *fr);
#endif
file_writer.c
#include <stdio.h>
#include "file_writer.h"
void write (struct FileWriter *fw)
{
FILE *f = fopen(fw->file_path, "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
fprintf(f, "%s", fw->content);
fclose(f);
}
file_writer.h
#ifndef FILE_WRITER_H_
#define FILE_WRITER_H_
typedef struct FileWriter
{
char *file_path;
char *content;
int *error;
} FileWriter;
#endif
Can you help me ? Thanks!
struct FileReader *fr = malloc(sizeof(struct FileReader));
There is no need to do this. All you need is this:
struct FileReader fr;
Same here:
struct FileWriter fw;
Then just pass the address of these variables to the requisite function(s).
Note this was not given to you as an answer, only as a comment to clean up your code a bit to remove extraneous calls to the heap. It just so happens that the real problem exists elsewhere, and what you're seeing here is undefined behavior in full glory.
I am not sure how are you reading from the file, character by character or block, but anyhow ,
since you update the read data in content buffer, and store the address of content buffer inside file_reader_new() into variable fr->content and immediately releasing the memory will end up loosing the data you read. and lead to condition called Dangling pointer
Dangling pointer
( a pointer variable, which points to a released memory )
that's why its always advised to set the pointer variable after releasing to NULL. Dereferencing a dangling pointer is will lead to Segmentation fault or undefined behavior in some scenarios.
Also, since all you member variables of struct are pointers its better to initialize them to NULL.
you can use calloc to initialize all the variables in a struct, instead of malloc to initialize all the members to NULL, if you are going with dynamic allocation. which goes for string also.
Here is an issue that I see:
fr->content = content;
fr->n_lines = counter;
fr->n_characters = i;
/* Clean up. */
free(content); /* <-- Danger */
You do this in your file_reader_new function. You then call show_file_reader_values and in that function, you're accessing content:
printf("content:\n%s\n", fr->content);
Since you called free() on the content, that pointer no longer points to valid memory, thus undefined behavior occurs.
The fix is to allocate space on fr for the content and copy the characters of content to this space, or simply not call free on content.
So either do this:
fr->content = malloc(i + 1);
strcpy(fr->content, content);
fr->n_lines = counter;
fr->n_characters = i;
/* Clean up. */
free(content);
or this:
fr->content = content;
fr->n_lines = counter;
fr->n_characters = i;
/* No call to free(content) done */

C, looping array of char* (strings) does't work. Why?

I have problem with my array of char*-
char *original_file_name_list[500];
while(dp=readdir(dir)) != NULL) {
original_file_name = dp->d_name;
original_file_name_list[counter] = original_file_name;
printf("%s\n",original_file_name_list[0]);
printf("%d\n",counter);
counter++;
}
The problem is, that it prints all files fine. It should print only first file, right?
And if I try printf("%s\n",original_file_name_list[1]); It doesn't work , which means that it is writing only in 1st string. Any idea why?
edit: There is no syntax error due to compiler.
You're not copying the string at all - also your file_name_list array hasn't enough space for a list of filenames - just for a list of pointers. But dp->d_name is just a pointer to a char* - you can't know for how long the memory behind the pointer is valid. Because of that you have to make a copy for yourself.
#include <string.h>
#include <dirent.h>
int main(int argc, char** argv){
char original_file_name_list[50][50];
size_t counter = 0;
while(dp=readdir(dir)) != NULL) // does work fine (ordinary reading files from dir)
{
size_t len = strlen(dp->d_name);
if(len >= 50) len = 49;
strncpy(original_file_name_list[counter], dp->d_name, len);
original_file_name_list[counter][len] = '\0';
printf("%d\n",counter);
counter++;
}
printf("%s\n",original_file_name_list[1]); // <- will work if you have at least 2 files in your directory
return 0;
}
I'm not sure about purpose of counter2 (I have replaced it with counter) but I can propose the following code with strdup() call to store the file names:
char *original_file_name_list[500] = {0}; // it is better to init it here
while(dp=readdir(dir)) != NULL) {
original_file_name_list[counter] = strdup(dp->d_name); // strdup() is ok to use
// here, see the comments
printf("%s\n%d\n",original_file_name_list[counter], counter);
counter++;
}
/* some useful code */
/* don't forget to free the items of list (allocated by strdup(..) )*/
for (int i = 0; i < 500; ++i) {
free(original_file_name_list[i]);
}

How can I check if file is text (ASCII) or binary in C

I need to write C code that checks to see if a file is text(ASCII) or Binary
Could someone help?
Thanks
Typical method is to read the first several hundred bytes and look for ASCII NUL.
If the file contains NUL, it is definitely a binary file. Most binary files do contain NUL bytes, but text files should never contain NUL bytes.
#include <string.h>
bool is_binary(const void *data, size_t len)
{
return memchr(data, '\0', len) != NULL;
}
Be warned that this is a heuristic. In other words, it will be wrong sometimes.
Read all characters and see if all of them are ASCII, that is, with codes from 0 to 127 inclusive.
Some tools determine whether a file is a text file or a binary file by just checking whether or not it has any byte with code 0.
Clearly, if you apply both of these methods, you will get different results for some files, so, you have to define what it is exactly that you're looking for.
You can use libmagic. The code below will show you roughly the way the "file" command does it. (The code below is quick and dirty -- it probably needs to be cleaned up.)
#include <string.h>
#include <magic.h>
#include <stdio.h>
//------------------------------------------------------------------------------
struct magic_set * prep_magic(int flags)
{
struct magic_set *magic = magic_open(flags);
const char *errstring;
int action = 0;
const char *magicfile = NULL;
if (magicfile == NULL)
magicfile = magic_getpath(magicfile, action);
if (magic == NULL)
{
printf("Can't create magic");
return NULL;
}
if (magic_load(magic, magicfile) == -1)
{
printf("%s", magic_error(magic));
magic_close(magic);
return NULL;
}
if ((errstring = magic_error(magic)) != NULL)
printf("%s\n", errstring);
return magic;
/* END FUNCTION prep_magic */ }
//------------------------------------------------------------------------------
int main(int argc, char **argv)
{
int flags = 0;
struct magic_set *msetptr = NULL;
const char *testfile = (char *)"/etc/motd";
msetptr = prep_magic(flags);
if( msetptr == NULL )
printf("no mset ptr\n");
const char *typer;
typer = magic_file( msetptr, testfile );
printf("typer = %s\n", typer );
return 0;
/* END PROGRAM */ }

Resources