0

It took me about a week to figure out that the files for what was formerly pset6 had been moved to the pset5 folder, and that's why my code couldn't open them. Now, however, it is still not opening. I realize #define for the files is not going to work in the long-term, I was just trying to get load to work on one file for now. Apologies in advance if I fumble pasting in this code snippet, I am new to this:

Thank you for your help!

// default text
#define text "/home/cs50/pset5/texts/quote.txt"
#define dict "/home/cs50/pset5/dictionaries/small"

// typedef a node for the hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node* next;
} node;

// Define hash table
node* hashtable[TABLE_SIZE];

// Function declarations
int hash_it(char* needs_hashing); // Hash function
bool hashtable_add(int hashed, char word); // Loop to add to table

// Global variable for the head of the list
node* head = NULL;
node* new_node = NULL;

int main(int argc, char* argv[])
{
    // open the dictionary
    FILE* dictfile = fopen(dict, "r");    
    if (dictfile == NULL)
    {
        printf("Could not open %s.\n", dict);
        
        //unload();
        return 1;
    } 

    // repeat until the end of the dictionary
    while(!feof(dictfile))
    {
         // read string from file
          fscanf(dictfile, "%s", new_node->word);

        // try to open text
        FILE* fp = fopen(text, "r");

        if (fp == NULL)
        {
            printf("Could not open %s.\n", text);
        
           //unload();
           return 1;
        } 
    }
} 
2
  • I'm a little confused by your approach here because it looks as though you're implementing the load() function from the dictionary.c file, but you've got all of this coded under a main() function. Furthermore, I'm not sure why you're hardcoding the dictionaries. I think some of my confusion might be coming from you working on a previous version of the pset? I'm assuming this because of your comment about how they changed it from pset6 to pset5. If this is the case, I would suggest looking at the new version of the assignment, it might be a tad easier.
    – Michael F
    Jan 15 '15 at 9:18
  • You are right, I was trying to create a mini load function, that was stand alone, to make sure it worked; then I was going to put it in with the rest of the code. And yes, I started in 2014, but I looked at the new spec and, apart from the change in weeks, I don't see a change. Please let me know if I missed something.
    – Hudit
    Jan 15 '15 at 12:26
0

I don't know about the pset sequence/pathways this year, but are you using the most current version of the appliance? If so, if you go to that subdirectory using the above paths in gedit, are you able to see the files alright?

6
  • Thanks for helping. I am using the new appliance, and I can see those files in the directories via gedit. To make things more mysterious, I wrote a teeny program just to open those files, which works!! I'm not so good at SE, if I could figure out how I would post that code here.
    – Hudit
    Jan 11 '15 at 13:49
  • That sounds odd. I did have some problems with localhost filepaths on the appliance when I was working on the final project. You might try using relative paths instead of absolute paths as you did above: "~cs50/pset5/texts/quote.txt". If that doesn't work, you might try using #include "~cs50/pset5/texts/quote.txt" too. As for using SE, you can just copy and paste text directly, just use markdown editing, specifically " ` " for code: cs50.stackexchange.com/editing-help#comment-formatting
    – ronga
    Jan 14 '15 at 3:54
  • Thanks for the suggestion. Is there such a thing as a file being corrupt or something? Because nothing works. Anyway I'm just going to rebuild it from scratch.
    – Hudit
    Jan 15 '15 at 8:31
  • The file could be corrupt but then you wouldn't have been able to open it. My guess is it's an incorrect filepath that's causing it, I got stuck there for a while too. How do you know your test program opened it? Did you use the exact same filepath in your program (the one that opened it)? Did you use error handling, or a something like: *fp=fopen(some file); if (fp==NULL) printf("Error opening file")?
    – ronga
    Jan 15 '15 at 8:52
  • My appliance v19 still has it under pset 6, I'm using a relative filepath, which worked for me: #define DICTIONARY "~cs50/pset6/dictionaries/large". Maybe you can substitute pset5 for the pset6 in it, see if that works.
    – ronga
    Jan 16 '15 at 13:23
0

Here is the code that works (too long for a comment, sorry) to open the files. Lacks sophistication, but outputs a "c" and an "I".

// default text
#define text "/home/cs50/pset5/texts/quote.txt"
#define dict "/home/cs50/pset5/dictionaries/small"

int main(int argc, char* argv[])
 {
     // open the dictionary
     FILE* dictfile = fopen(dict, "r");

        if (dictfile == NULL)
        {
            printf("Could not open %s.\n", dict);
            return 1;
        }

        else
        {
            int letter = 0;
            letter = fgetc(dictfile);
            printf("%c\n", letter); 
        }  
    // try to open text
     FILE* fp = fopen(text, "r");

        if (fp == NULL)
        {
            printf("Could not open %s.\n", text);
            return 1;
        }

        else
        {
            int letter = 0;
            letter = fgetc(fp);
            printf("%c\n", letter); 
        }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .