0

I'm quite new to C programming and have just begun studying files. I'm wondering whether it is possible to read a file line by line (including spaces in each line) into an array of size equal to the number of lines in the file. I really have no idea where to start or whether this is even possible so any guidance at all would be much appreciated.

Example

A text file in the form of:

Computer Programming
Software Engineering
Computer Architecture

to be written into array such that:

char array[4];

array[0] = "Computer Programming";
array[1] = "Software Engineering";
array[2] = "Computer Architecture";

All I have so far is:

int main()
{
    char array[50];
    bool answer;

    FILE *classes;
    classes = fopen("classnames.txt", "r");

    if(classes == NULL){
        printf("\n ************* ERROR *************\n");
        printf("\n \"classnames.txt\" cannot be opened.\n");
        printf("\n         PROGRAM TERMINATED\n");
        exit(EXIT_FAILURE);
    }

And next I would like to write each class name into each element of the array.

PhillyNJ
  • 3,859
  • 4
  • 38
  • 64
KOB
  • 4,084
  • 9
  • 44
  • 88

2 Answers2

1

Yes, you just have to declare array as char** and dynamically allocate as you read each line. E.g.

int MAX_NUM_LINES = 1000;
int MAX_LINE_LEN = 256;

char** array;
malloc(array, MAX_NUM_LINES*sizeof(char*));
fp = fopen(...);

int line_ct = 0;
char line[MAX_LINE_LEN];
while ( fgets(line, MAX_LINE_LEN, fp) != NULL )
{
   int len = strlen(line);
   malloc(array[line_ct], len * sizeof(char));
   strcpy(array[line_ct], line);
   line_ct++;
}

I have not actually tried to compile this code, but something like this will work. You can also replace MAX_NUM_LINES with the actual value by doing a quick runthrough first and counting the lines--that would be preferable probably.

Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
  • 1
    The `while` loop should be `while ( fgets(line, MAX_LINE_LEN, fp) != NULL )`. See [this post](http://stackoverflow.com/a/18796037/3386109) for more. – user3386109 Mar 03 '15 at 21:36
  • 1
    @user3386109 Wow, that's a great thread, thanks. I have indeed gone and edited the post. – Matt Phillips Mar 03 '15 at 21:45
0

This is an example of a possible approach

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

int main()
{
    char        array[100][100];
    char        line[100];
    size_t      arraySize;
    size_t      count;
    FILE       *file;
    const char *filepath;

    filepath = "<put the file path here>";
    file     = fopen(filepath, "r");
    if (file == NULL)
     {
        perror("fopen()");
        return -1;
     }

    count     = 0;
    arraySize = sizeof(array) / sizeof(array[0]);
    while ((fgets(line, sizeof(line), file) != NULL) && (count < arraySize))
     {
        size_t length;

        length = strlen(line);
        if (line[length] == '\0')
            line[--length] = '\0';
        memcpy(array[count++], line, 1 + length);
     }
    fclose(file);

    return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97