So I’ve been looking around for a while, and can’t find an answer to my question…
I am attempting to create a program that, when a directory is inputted in a command line argument, will access each individual file in the directory and save the file name in a linked list. Then, the program will tokenize the contents of the file, and save each token to a linked list headed by the appropriate file name. The list will be constructed like this:
[ListHeader][file1.txt][file2.txt][file3.txt]...[filen.txt]
[token 1 ][token 1 ][token 1 ]...[token 1 ]
[token 2 ][token 2 ][token 2 ]...[token 2 ]
[token n ][token n ][token n ]...[token n ]
Now, I have finished the tokenizer/linked list function. I am having no trouble in the current working directory with opening files using the filestream fopen() command. What I am having trouble with, however, is opening a file in a new directory.
For example, let’s say I input:
user@usermachine:~/Cprograms$ gcc -o d directorytokenizer.c
user@usermachine:~/Cprograms$ ./d test.txt
Where test.txt is in the same directory (in the above case Cprograms) as the program itself.
This example works fine.
However, if the input is
user@usermachine:~/Cprograms$ gcc -o d directorytokenizer.c
user@usermachine:~/Cprograms$ ./d testdirectory
where “testdirectory” is a directory contained in the directory Cprograms (testdirectory is a folder in Cprograms).
My program opens the directory fine, and it can output the filenames within the directory (I did this to debug, and it worked). However, when I try to pass the individual filenames into a function that filestreams them (fopen()), the filestream command can’t find the file.
Here are the relevant portions of my code:
//Sortedlist.h-
#ifndef SORTED_LIST_H
#define SORTED_LIST_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>
struct SLNodes
{
char *word;
struct SLNodes * next;
int occurencecounter;
};
typedef struct SLNodes* SLNode;
struct SortedList_
{
char * filename;
SLNode Listhead;
struct SortedList_ * nextfile;
};
typedef struct SortedList_* SortedList;
SortedList SLCreate();
void tokenstore(char *inputstr, SortedList NewList);
#endif
//indexer.c (truncated)-
//this is the mainfile
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include "listheader.h"
#include "tokenstore.c"
#include "diropen.c"
int main(int argc, char *argv[])
{
DIR *dip;
if (argc < 2)
usage(argc, argv[0]);
if ((dip = opendir(argv[1])) == NULL)
{
printf("Not a directory, now attempting to open file...\n");
SortedList NewList = CreateNewList(argv[1]);
NewList = tokenize(NewList, argv[1]);
SLNode curr = NewList->Listhead;
if (curr != NULL)
{
SLNode curr = NewList->Listhead;
printf("The following alphabetized and tokenized list of words is in the file: %s\n", NewList->filename);
while(curr != NULL)
{
printf("<listbegin>\n");
printf("The word <%s> appears <%d> times\n", curr->word, curr->occurencecounter);
printf("<endlist>\n\n");
curr = curr->next;
}
}
else
printf("File did not contain any words. Please make sure to input a file with words in it. Remember, words consist of upper case and lower case letters, and numbers... Nothing else.\n");
}
else
{
SortedList NewList = CreateNewList(argv[1]);
printf("You have input a directory. Now attempting to open and sort...\n");
openadir(argv[1], NewList);
}
}
//diropen.c (truncated)-
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
SortedList openadir(char *dirname, SortedList NewList)
{
DIR *inputdir;
struct dirent *dirstruct;
SortedList placeholder;
int i = 0;
/* DIR *opendir(const char *name);
*
* Open a directory stream to argv[1] and make sure
* it's a readable and valid (directory) */
if ((inputdir = opendir(dirname)) == NULL)
{
perror("opendir");
return;
}
printf("Directory stream is now open\n");
/* struct dirent *readdir(DIR *dir);
*
* Read in the files from argv[1] and print */
while ((dit = readdir(dip)) != NULL)
{
i++;
printf("\n%s\n", dit->d_name);
NewList->filename = dirstruct->d_name;
NewList = tokenize(NewList, dirstruct->d_name);
NewList->nextfile = malloc(sizeof(SortedList));
placeholder = NewList->nextfile;
placeholder->filename = NULL;
placeholder->Listhead = NULL;
placeholder->nextfile = NULL;
NewList = placeholder;
}
}
//tokenstore.c (truncated) -
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>
#include "listheader.h"
SortedList tokenize(SortedList NewList, char *filename) //tokenize the input file
{
printf("filename is : %s\n", filename);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* The following function simply steps through the input file and records the total
* character count, in order to allocate a buffer of the correct size
* to contain the eventual tokenized string.
*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
FILE *fc = fopen(filename, "r");
if(fc == NULL)
fatal(": Input file does not exist");
int hold;
int count=0;
do
{
hold = fgetc(fc);
count++;
}
while( hold != EOF );
fclose(fc);
}
Now, as I said earlier, inputting a filename that is in the same directory as the program works fine, it’s when I try to pass in filenames to fopen from a different directory that the program throws a fatal: file not found. (fatal error function is not included in the source code I posted, but it works fine).
My question is, how do I pass filenames from another directory into fopen() in such a manner that the program will open the files?
Thank you for taking the time to read all this.
The
d_namemember of thestruct direntcontains, as you have noticed, only the file name. It does not contain the directory name.All you need to do is to concatenate the directory name, directory separator, and that
d_namestring to get a relative path that you can use withfopen.Also note:
This is not good. You must copy that string if you want to retain it.
Your
tokenizefunction is declared are returning aSortedList, but it doesn’t return anything.Please make sure you’ve turned on your compiler’s warnings.