2

I'm trying to open a file called dictonary.txt and copy its contents to a string array. But I forget how to do it.

Here's what I have so far:

int main(int argc, char** argv) 
{
    char dict[999][15];
    FILE *fp;
    fp = fopen("dictionary.txt", "r");
    while(feof (fp))
    {
            
    }
}

Can someone help me with this?

EDIT: There are 999 words in the file and all of them have 15 characters or less.

9
  • 1
    while (!feof(file)) is always wrong and while (feof(file)) doesn't make sense Commented Mar 27, 2021 at 17:08
  • It will depend on the structure of the input file, but fgets() and fscanf() or sscanf() will be useful. Commented Mar 27, 2021 at 17:08
  • 1
    @pmg It is actually while(feof (fp)) here. No ! and it is worse. Commented Mar 27, 2021 at 17:08
  • 1
    do you want up to 999 words shorter than 15 chars each... or up to 15 words shorter than 999 chars each (as your code suggests)? Commented Mar 27, 2021 at 17:11
  • 1
    @Kazumi411 it should work flawlessly, I added some more code to help you pinpoint the problem, I also added a live sample, check it wandbox.org/permlink/TDDyZXswA07imf9a Commented Mar 27, 2021 at 18:50

2 Answers 2

3

As stated in the comments while(!feof (fp)) will not work as intended, you are also missing the negator but that's beside the point, a simple way to do this is to use fgets and take advantage of its return value to detect the end of the file, as it returns NULL when there are no more lines to read:

#include <stdio.h>
#include <string.h>

int main()
{
    char dict[999][15];
    size_t ind; // store the index of last read line

    FILE *fp = fopen("dictionary.txt", "r");

    if (fp != NULL) // check if the file was succefully opened
    {
        // read each line until he end, or the array bound is reached
        for (ind = 0; fgets(dict[ind], sizeof dict[0], fp) && ind < sizeof dict / sizeof dict[0]; ind++)
        {
            dict[ind][strcspn(dict[ind], "\n")] = '\0'; // remove newline from the buffer, optional
        }

        for (size_t i = 0; i < ind; i++) // test print
        {
            puts(dict[i]);
        }
    }
    else
    {
        perror("Error opening file");
    }
}

Note that this will also read empty lines, i.e. lines with \n only or other blank characters.

Test sample

Sign up to request clarification or add additional context in comments.

4 Comments

Also be careful: fgets() will put the newline character read to the buffer, so you may want to remove that before further processing.
@MikeCAT added an option to remove it, also a note that blank lines will also be parsed.
I just posted a function to have fgets without the new line if you want to avoid parsing your string twice: codereview.stackexchange.com/questions/258772/… your insights are welcome! @MikeCAT and anastaciu
note the edit in the question correcting the array dimension order.
0
#include <stdio.h>

int main()
{
    char dict[999][15];
    FILE * fp;
    fp = fopen("dictionary.txt", "r");

    int i = 0;
    while (fscanf(fp, "%s", dict[i]) != EOF)
            i++;

    // i is your length of the list now so you can print all words with
    int j;
    for (j = 0; j < i; j++)
            printf("%s ", dict[j]);

    fclose(fp);
    return 0;
}

Instead of != EOF, you can also do == 1 because fscanf returns a value of scanned things. So if you do:

fscanf(fp, "%s", dict[i]);

it will return 1 if a word is successfully scanned. And it won't when it reaches the end of the file. So you can do it that way too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.