0

i'm taking a programming class in C and i am trying to make a dictionary with symbols and letters for a program ex. A = ?, B =! and so on.

the letter and it's defined symbol are saved in a text file, each in a line so:

A ?

B !

C =

D &

etc...

i'm using FILE * fPointer and gets to read and save line by line with a while loop, i want to save each line in an array :

#include <stdio.h>

int main()
{
    FILE * fPointer;
    fPointer = fopen("symbols.txt", "r");
    char line[50];
    char vocab[36];
    while(!feof(fPointer))
    {
        for(int i=0;i<=36;i++)
        {
            fgets(line, 50, fPointer);
            vocab[i] = line;
            printf("%s", vocab[i]);
        }
    }
}

Ideally, it would be saving each line in the array position vocab[0] = "A ?" vocab[1] = "B !" ...

It gives me the next warning: assignment makes integer from pointer without a cast,

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • 1
    You can't just assign one string to another and get a deep copy, and you certainly can't assign one entire array of `char` to the single `char` you get from `vocab[i]`. So the error is totally correct. A single `char` is an integer, and an array of `char` is (or rather, decays to) a pointer. I suggest researching multi-dimensional arrays and `strcpy()`. – underscore_d Jan 08 '20 at 15:53
  • https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Yunnosch Jan 08 '20 at 16:46
  • @screaiking You should not use `while(!feof(fPointer))`. It is considered as very bad style, look at the link Yunnosch has posted. Rather do it in the kind of `while((c = getchar(fPointer)) != EOF)`. – RobertS supports Monica Cellio Jan 08 '20 at 17:12

1 Answers1

0

You can use fgets directly instead feof as the following:

#define LINES 10
#define CHARS 30

char content[LINES][CHARS];
FILE *file;
unsigned int i = 0;
file = fopen("c:\\file.txt", "r");
while ((fgets(content[i], CHARS, file)) != NULL) {
        printf("%s\n", content[i]);
        i++;
}
fclose(file);
Lion King
  • 32,851
  • 25
  • 81
  • 143