0

Haven't done any C programming in awhile but I have the following that outputs a string but I'd like to put it into an array that I can then manipulate (at the --------- in comments in the code). I would think I need to declare the size of the array but need to handle a variable amount. Was thinking of doing something like system('wc -l filename') but that sounds really awful. Must be a better way:

#include <stdio.h>

int main()
{
    char *inname = "test.txt";
    FILE *infile;
    char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
    char line_number;

    infile = fopen(inname, "r");
    if (!infile) {
        printf("Couldn't open file %s for reading.\n", inname);
        return 0;
    }
    printf("Opened file %s for reading.\n", inname);

    line_number = 0;
    while (fgets(line_buffer, sizeof(line_buffer), infile)) {
        ++line_number;
        /* note that the newline is in the buffer */
        // ---------------------
        // would like to put into an array of strings here rather than just printf'ing out out

         printf("%4d: %s", line_number, line_buffer);
    }
    printf("\nTotal number of lines = %d\n", line_number);
    return 0;
}

3 Answers 3

2

First of all:

char *inname = "test.txt";

should be

const char *inname = "test.txt";

Then, you want to allocate an array big enough to store all the lines. Since you don't know the number of lines beforehand, you can use exponential storage expansion: double the size of the array when it's exhausted.

Sample code (error checking omitted for clarity, don't copy-paste this into production code):

size_t n = 0;
size_t alloc_size = 4;
char buffer[LINE_MAX];

char **arr = malloc(alloc_size * sizeof arr[0]);

while (fgets(buffer, sizeof buffer, fp) != NULL) {
    if (++n > alloc_size) {
        alloc_size *= 2;
        arr = realloc(arr, alloc_size * sizeof arr[0]); // don't do this
    }

    arr[n - 1] = strdup(buffer);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could go through the whole file (slow if the file is big) and count every "new line". Then create an array with this count size and then rewind the file and read in every line.

mfg

Comments

1

Unfortunately there is no dynamic array in C (if you are thinking of something like vector in C++). You could use list and each time you are reading line from file just append new list entry at the end of list.

There is also "dynamic" array since C99 called VLA (variable length array). You can declare array with dynamic size (or rather known while program is running), but this will not help you with your problem as you will have to every time declare new array with size bigger than previous one and copy content of old to new - this would be very inefficient.

So summing up there could be hard to find something better than list.

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.