Home > Blockchain >  Store line by line elements in array C language
Store line by line elements in array C language

Time:03-20

i'm trying to read a .txt file having in every line a name and a age and store them in an array, but apparently only the last line name is being stored on all of positions of array

My .txt file content:

Pedro 14
Erica 17
Paulo 23
Carlos 27
Mendes 30
Augusto 31
Geraldo 32

My code:

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

int main() {
   FILE *f = fopen("nomes.txt" , "r");
   char name[20];
   char* listnames[10];
   int cont;

   while(fscanf(f, "%s %d", name) != EOF) {
        listnames[cont] = name;
        cont  ;
   }

   fclose(f);

   for (int i=0; i<cont; i  ) {
       printf("%s ", listnames[i]);
   }
} 

Output:

Geraldo Geraldo Geraldo Geraldo Geraldo Geraldo Geraldo

CodePudding user response:

There are a couple of issues in your code:

First:

FILE *f = fopen("nomes.txt" , "r");

What if opening the file fails?

Second:

cont is not initialized. You may want to initialize it to 0.

Third:

fscanf(f, "%s %d", name)

You are scanning for a char* and an int, but you are only passing one argument (i.e. name).

Fourth:

char* listnames[10];
...
listnames[cont] = name;

Here, listnames[cont] is not initialized. And you are assigning the adress of name to every listnames entry. You may want to use strdup() to allocate space for your strings, or use a static array for that matter.


Here is how your code should look like:

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

int main(void)
{
    FILE *f = fopen("nomes.txt" , "r");
    if (!f) { // Handle failure
        perror("Could not open file");
        return 1;
    }
    
    char name[20];
    int number;
    char* listnames[10]; // (*) char listnames[10][20];
    int numbers[10];
    int count = 0;
    
    while(fscanf(f, "%s %d", name, &number) != EOF && count < 10) {
        listnames[count] = strdup(name); // (*) strcpy(listnames[count], name);
        numbers[count] = number; // If you want to save the numbers
        count  ;
    }
    
    fclose(f);
    
    for (int i = 0; i < count; i  ) {
        printf("%s %d\n", listnames[i], numbers[i]);
        free(listnames[i]); // Free space allocated by strdup()
    }
} 

CodePudding user response:

You have forgotten to initialize the cont to zero before entering the while loop.

CodePudding user response:

While @user2134584's comment is correct, the main problem is that you are only allocating a single buffer, then having each of the pointers reference that same, single buffer.

Allocating an array of char pointers does not allocate space for the strings they point to.

Instead, what you need to do is something like this:

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

int main() {
   FILE *f = fopen("nomes.txt" , "r");
   char listnames[10][20];
   int discard = 0;
   int cont = 0;

   while(fscanf(f, " s %d", listnames[cont], &discard) != EOF) {
       cont  ;
   }

   fclose(f);

   for (int i=0; i<cont; i  ) {
       printf("%s ", listnames[i]);
   }
} 

Note that I haven't tested this code, but it should at least put you on the right track.

  • Related