c read lines from text file in to array

Solutions on MaxInterview for c read lines from text file in to array by the best coders in the world

showing results for - "c read lines from text file in to array"
Alessandro
10 Oct 2017
1FILE *fp;        // pointer to file
2    char *file_name; // file path
3    char cur_char;
4    char *line = NULL; // line array
5    size_t len = 0;
6    ssize_t read;
7    int counter = 0;
8
9    //char content[MAX_NUM_LINES][MAX_LINE_LENGTH];
10    char strArray[150][150];
11    if (optarg == 0)
12    {
13        printf("File could not be opened.");
14        return;
15    }
16    // has file argument
17    fp = fopen(optarg, "r");
18
19    if (fp != NULL)
20    {
21
22        while ((read = getline(&line, &len, fp)) != -1) // loop thru lines and add them to the strArray
23        {
24            // fgets(line, 100, fp);
25            int i = 0;
26            while(line[i] != '\n'){ // loop thru the characters in the current line
27                strArray[counter][i] = line[i];
28                i++;
29            }
30            
31            counter++;//counts the number of lines in the file
32         
33        }
34        }