简体   繁体   中英

Accessing string from a structure, by using a series of pointers

typedef struct {
    char numeVolum[50]; ////this is what interests us!!!
    short int anPublicare; 
    unsigned char stare; 
    int idPersoana; 
} TVolum; 

typedef struct { 
    char numeAutor[50];  
    char codTara[2];  
    int nrVolume; 
    TVolum* volume;  /////this is what interests us!!!
} TAutor; 

Both of these structures are allocated dynamically! listaAutori[0] is an array of pointers to TAutor structures! (you can conclude that listaAutori is a TAutor** type)

fgets(listaAutori[0] -> numeAutor, 50, input); 
fgets(listaAutori[0] -> codTara, 3, input); 
fgets(listaAutori[0] -> volume[0].numeVolum, 53, input); 

The first two fgets are reading fine. But the third one gives me no output.

printf("\nNUme primul volum al lui Agatha: %s\n", listaAutori[0] -> volume[0].numeVolum);

In other words, I have two structures, A and B. Inside B, I have a string. (pointer to char)

There is a pointer pB in structure A. And there is an array of pointers to A.

Like this:

Array of pointers containing pA element -> structure A -> pB -> structure B -> my string.

I am trying to read a line from a file, and store that string inside the numeVolum[50] string. The only way I can access TVolum is by using a pointer to TAutor.

I don't know what is not working, that printf gives me no output. It should have printed something. (my file from which data is read contains info on every line)

Autor* alocaAutor(int nrVolume) 
{ 
    TAutor* autor = (TAutor*)calloc(1, sizeof(TAutor));  
    autor -> volume = (TVolum*)calloc(nrVolume, sizeof(TVolum)); 
    autor -> nrVolume = nrVolume; 
    return autor; 
} 

TAutor** alocaAutori(int nrAutori, int* nrVolumeAutor) 
{
    int i;
    TAutor** vectorAutori = (TAutor**)calloc(nrAutori, sizeof(TAutor*));  
    for(i = 0; i < nrAutori; i++) {
        vectorAutori[i] = alocaAutor(nrVolumeAutor[i]); 
    } 
    return vectorAutori; 
} 

example of the input file :

2 
1 
1
Agatha Christie
UK
Ultimul caz al lui Hercule Poirot

I am 100 percent positive that it reads everything before the last line. Then when I try to read the last line inside the string from TVolume, it simply doesn't work.

char *fgets(char *str, int n, FILE *stream)

stops when n-1 characters have been read ( source ). Because the value of n in the second fgets is 3 , it reads only the UK in the input file. The newline character after it is not read yet. It is then read by the third fgets , which, encountering the newline character immediately, does not read further. Thus, the final line in the input file is not read.

Your edited code (in your comment) works because the fscanf reads that newline character and stops at it, leaving the fgets to read the final line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM