Home > OS >  C - word array keeps getting overridden
C - word array keeps getting overridden

Time:10-09

#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>

// This program is going to scan all files in the current directory. It will make a tree for every folder
// and the folder will have a subsection of files in the tree format. YES SORTING!

char **word;
int coun = 0;

void printdir(char *dir, int depth)
{
    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)) // Check if it's a directory
        {
            /* Found a directory, but ignore . and .. */
            if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
            {
                continue;
            }
            word[coun] = ("%s",entry->d_name); // Put the file name in the array.
        coun  ;
            printf("- %*s%s\n",depth,"",entry->d_name); // Print the name of the dir entry.
            /* Recurse at a new indent level */
            printdir(entry->d_name,depth 1);
        }
        else 
    {
            word[coun] = ("%s",entry->d_name); // Put the file name in the array.
        coun  ;
            printf("%*s - %s\n",depth,"",entry->d_name); // This will print the file.
        }
    }
    chdir("..");
    closedir(dp);
}

int main(int argc, char* argv[])
{
    word = calloc(1000, sizeof(*word));
    printdir(".", 0);
    printf("now, print the words in the order they were printed.\n");

    for (int i = 0; i < coun;   i)
    {
        printf("%s\n", word[i]);
    }
    exit(0);
}

My main aim for this code is to make a tree structure of the files that are currently in the directory. When I run it, I get this output.

- hw2
  - tree
  - Makefile
  - ls.c
  - tree.c
  - find.c
- hw1
  - grep.c
  - factor.c
  - uniq.c
  - monster.c
  - sort.c
 - .nfs0000000006c543ea0000e073
 - tree.c
 - tree
now, print the words in the order they were printed.
hw2
grep.c






hw1
grep.c
factor.c
uniq.c
monster.c
sort.c
.nfs0000000006c543ea0000e073
tree.c
tree

The tree works fine, but I still need to sort the files afterward. My plan is to put all of the file names into the global words array, then discern between folders and files, and print in the same format, but ordered alphabetically case-insensitive. I check the array, but the hw2 folder and its files gets overridden completely. I don't understand why this is happening because it should be working fine. Does anyone know a fix or maybe a better way to do this?

CodePudding user response:

I believe your issue lies here:

word[coun] = ("%s", entry->d_name);
  • You have not allocated any memory space for word[coun] to point to.
  • I have no idea what ("%s", ...) is meant to accomplish, but gcc throws warnings about it. It actually does return entry->d_name but the "%s is unused.
  • You have word[coun] pointing to something that changes before you try to access it. Instead you need to use strcpy to copy entry->d_name into the memory space you allocate.

Instead of:

word[coun] = ("%s", entry->d_name); 
coun  ;

You want:

word[coun] = malloc(strlen(entry->d_name)   1);
strcpy(word[coun  ], entry->d_name);

Make sure to allocate an extra byte for the '\0' that terminates every C string.

  • Related