-1

I'm attempting to set each line of a file, into an char *arr[].

For Example, I have an file set like this
arq.txt:

linha1
linha2
linha3
linha4
linha5
linha6
linha7
linha8
linha9
linha10

I want to store linha1 into the first position from my string array, so arr[0] = "linha1"
What I have coded to attempt to obtain this result was:

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

int main(){
    FILE *in_file  = fopen("arq.txt", "r"); 
    char singleLine[150];
    char* test[30];
    int i = 0;

    if (!in_file) 
    {   
        printf("Error! Could not open file\n"); 
        return 0;
    }

    while (!feof(in_file)){
        fgets(singleLine, 150, in_file);
        if (i == 0){
            test[0] = singleLine;
            puts(singleLine);
        }
        i++;
    }

    size_t length = sizeof(test)/sizeof(test[0]);
    printf("Size: %d", length);

    printf("Here: %s\n", test[0]);

    fclose(in_file);

    return 0;
}

Althought, the result from that is printing the wrong data value:

(puts): linha1
Size: 30
Here: linha10

I attempted to get the size of the array from the content inside of it, so in this case, having only one element, I should have gotten 1 instead of 30.

Nilton Schumacher F
  • 814
  • 3
  • 13
  • 43
  • 1
    Removing the C++ tag since you're clearly not using any C++ features. Otherwise I would suggest a `std::vector`. – Botje Sep 17 '21 at 13:04
  • 1
    Does this answer your question? [How to read text from file into Array](https://stackoverflow.com/questions/31581993/how-to-read-text-from-file-into-array) – Botje Sep 17 '21 at 13:05
  • 1
    size_t length = sizeof(test)/sizeof(test[0]); printf("Size: %d", length); - this code will always give you 30 as you are statically assigning memory – JosiP Sep 17 '21 at 13:10
  • 2
    1/ [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/3545273) and 2/ `test` is an array of pointers which will all point to the real same string `singleLine`. You should use `strdup` to allocate a new copy for each line (and free them when done...) – Serge Ballesta Sep 17 '21 at 13:15
  • Nilton Schumacher F, who or what text suggested `while (!feof(in_file)){...`? – chux - Reinstate Monica Sep 17 '21 at 14:02

2 Answers2

2

In your code test[0] is a pointer, which points to the initial element of the array singleLine. In other words

 test[0] = singleLine;

does not "copy" the content of singleLine into test[0], rather it makes test[0] to point to the singleLine. It has no association with the stored value in singleLine at that point of time.

While trying to print the content of the memory location pointed to by test[0], it will always refer to the latest value stored in singleLine.

Now, in your while loop, the value of singleLine keeps getting updated, and later when you try to print the value after the loop, it prints the last read value which is stored into singleLine.

That said,

  1. Please see Why is “while ( !feof (file) )” always wrong?
  2. You need to allocate memory to each element of test array, and then copy (strcpy()) the content, if you want the values to persist.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

You can use something like this

#include<bits/stdc++.h>
using namespace std;

int main() {
    vector <string> v;
    string line;    
    ifstream fin("arq.txt");
    while(getline(fin,line)){
        v.push_back(line);
    }
    cout<<v.size();
}
GOVIND DIXIT
  • 1,748
  • 10
  • 27