Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6680513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:29:01+00:00 2026-05-26T04:29:01+00:00

So I’ve been looking around for a while, and can’t find an answer to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:29:01+00:00Added an answer on May 26, 2026 at 4:29 am

    The d_name member of the struct dirent contains, 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_name string to get a relative path that you can use with fopen.

    Also note:

    NewList->filename = dirstruct->d_name;
    

    This is not good. You must copy that string if you want to retain it.

    Your tokenize function is declared are returning a SortedList, but it doesn’t return anything.

    Please make sure you’ve turned on your compiler’s warnings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.