简体   繁体   中英

How do i save individual lines from a text file in an array?

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,

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM