-1

again. I'm new to C. Still thinking in Python terms (readlines, append them to variable) so I'm having difficulties translating that to C. This is what I want to do: open a text file for reading, store each line in an array row by row, print it out to make sure it's stored.

This is how far I've got:

int main(){

FILE * fp = fopen("sometext.txt", "r"); 

char text[100][100];

if(fp == NULL){
    printf("File not found!");
}
else{
    char aLine[20];

    int row = 0;
    while(fgets(aLine, 20, fp) != NULL){

    printf("%s", aLine);
    //strcpy(text[row], aLine); Trying to append a line (as row)
    return 0; 
}

Please don't start with "invest in some more time and look somewhere else because it's easy and has been answered". I'm bad at this, and I'm trying.

  • There is at least 1 missing `}` look where you've closed `while(fgets(aLine, 20, fp) != NULL){` scope – Cid Oct 30 '18 at 13:35
  • 1
    Possible duplicate of [How to use fgets to read a file line by line](https://stackoverflow.com/questions/41902471/how-to-use-fgets-to-read-a-file-line-by-line) – BurnsBA Oct 30 '18 at 13:36
  • Yeah, I forgot 1 "}" while I was editing. Btw using tab while editing here is annoying xD –  Oct 30 '18 at 13:52

2 Answers2

1

You can try this. Basically you need an array of arrays to store each line. You find the length of the longest line in the file and you allocate space for it. Then rewind the pointer to the start of the file and use fgets to get each line from the file and strdup to allocate space and copy the line to the respective position. Hope this helps.

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

int main(int argc, char *argv[]) {
    FILE * fp = fopen("sometext.txt", "r");
    int maxLineSize = 0, count = 0;
    char c;

    while ((c = fgetc(fp)) != EOF) {
        if (c == '\n' && count > maxLineSize) maxLineSize = count;
        if (c == '\n') count = 0;
        count++;
    }
    rewind(fp);

    char ** lines = NULL;
    char * line = calloc(maxLineSize, sizeof(char));
    for (int i = 0 ; fgets(line, maxLineSize + 1, fp) != NULL ; i++) { // +1 for \0
        lines = realloc(lines, (i + 1) * sizeof(char *));
        line[strcspn(line, "\n")] = 0; // optional if you want to cut \n from the end of the line
        lines[i] = strdup(line);
        printf("%s\n", lines[i]);
        memset(line, maxLineSize, '\0');
    }

    fclose(fp);
}
user694733
  • 15,208
  • 2
  • 42
  • 68
Alex G
  • 1,897
  • 2
  • 10
  • 15
0

You could solve it without copy

The follow code could work:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>

int main()
{
    FILE * fp = fopen("sometext.txt", "r");
    if(fp == NULL){
        printf("File not found!");
        return -1;
    }
    char text[100][20];

    int row = 0;
    while(row < 100 && fgets(text[row], sizeof(text[0]), fp) != NULL)
        ++row;
    for (int i= 0; i != row; ++i)
        fputs(text[i], stdout);
    return 0;
}
Yunbin Liu
  • 1,484
  • 2
  • 11
  • 20