I am a little new to using multi-dimensional arrays, but I think after extensive research, I was starting to feel super confident I could work with them correctly. Apparently, I guess I don't quite know them completely lol. In this case, I am trying to read in all the test files from directory ("path" in this case), insert each "d_name" into its own row of a 2-dimensional char array. I am able to print each string element (filename, in this case) successfully when I use the "[]" array-type notation. However, after I pass a pointer for this 2-dimensional char array into a "readFiles" function, I am able to successfully print out the 1st string of this array, but when I attempt to move the pointer in order to point at the next row (the 2nd string, or filename), I receive unexpected garbage results, eventually ending in a seg fault. So something is wrong with the way I am iterating through an array with multiple rows like this. I REALLY do not wish to print character by character here, I just want to print out 1 string (filename) at a time by simply moving the pointer down 1 row after a string element is printed. Could somebody please help if you can ? I really really appreciate your time on the weekend like this!
my program (just a giant function for now)...
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include<unistd.h>
#include <stdbool.h>
#include <errno.h>
#include <sys/types.h>
void alphabetcountmulthreads( char *path )
{
void readFiles( char **allfiles, int total_rows )
{
printf( "The pointer passed into the function is at %p and its first element is...\n", (void *)*allfiles );
for ( int k = 0; k < total_rows; ++k )
{
printf( "%s\n", *( allfiles + k ) );
printf( "...which was found at...%p\n", (void *)*(allfiles + k) );
printf( "...because the pointer was added by %d (string should be in row %d )\n", k, k );
}
}
DIR *dir;
struct dirent *in_file;
char fileList[100][30];
char *filename_reader = &fileList[0][0];
int filled_rows = 0;
int file_count = 0;
dir = opendir( path ); // open the data directory
if( dir == NULL )
{
printf( "Unable to read directory!" );
exit( 1 );
}
while( ( in_file = readdir( dir ) ) != NULL )
{
FILE *entry_file;
char *check_filename; // for checking if file ends EXACTLY with ".txt"
int c;
check_filename = strrchr( (in_file->d_name), '.' ); // move to last "."
if ( !strcmp ( in_file->d_name, "." ) || !strcmp ( in_file->d_name, ".." ) )
{
continue;
}
if ( check_filename )
{
if ( strcmp( ( check_filename ) , ".txt" ) ) // does it end in ".txt" ?
{
continue;
}
file_count++;
int filename_length = strlen( (in_file->d_name) );
// path = "../data"
printf( "%s...%d c's long\n", in_file->d_name, filename_length );
strcpy( fileList[filled_rows], (in_file->d_name) );
printf( "The string, %s\n", fileList[filled_rows] );
printf( "had its first character stored at...%p\n", &fileList[filled_rows][0] );
filled_rows++;
}
}
closedir( dir );
fileList[ (filled_rows - 1 + 1) ][0] = '\0';
filename_reader = &fileList[0][0];
char **function_insert1 = &filename_reader;
printf( "The actual address of the 2D array is %p\n", (void*)&fileList[0][0] );
printf( "Its pointer's location is at %p\n", filename_reader );
readFiles( function_insert1, filled_rows );
}
and here is my output........
test11.txt...10 c's long
The string, test11.txt
had its first character stored at...0x7fffd2b86180
test10.txt...10 c's long
The string, test10.txt
had its first character stored at...0x7fffd2b8619e
test2.txt...9 c's long
The string, test2.txt
had its first character stored at...0x7fffd2b861bc
test1.txt...9 c's long
The string, test1.txt
had its first character stored at...0x7fffd2b861da
test3.txt...9 c's long
The string, test3.txt
had its first character stored at...0x7fffd2b861f8
test12.txt...10 c's long
The string, test12.txt
had its first character stored at...0x7fffd2b86216
test13.txt...10 c's long
The string, test13.txt
had its first character stored at...0x7fffd2b86234
The actual address of the 2D array is 0x7fffd2b86180
Its pointer's location is at 0x7fffd2b86180
The pointer passed into the function is at 0x7fffd2b86180 and its first element is...
test11.txt
...which was found at...0x7fffd2b86180
...because the pointer was added by 0 (string should be in row 0 )
...which was found at...0x7fffd2b860e0
...because the pointer was added by 1 (string should be in row 1 )
...which was found at...0xa5b420
...because the pointer was added by 2 (string should be in row 2 )
Segmentation faultSegmentation fault