# Open File and (allocate memorie) Read lines into a struct C



## ninja_indiano (Apr 12, 2005)

Hi there,
I want to do a program that open a txt file than reads it´s line allocates memorie 
of the line and puts de lines on a struct. I am having problems in the reading lines and allocating memory for it, so far I have this: 

```
char *getline(*file_name){
    int n;
    char *str = (char*) malloc(sizeof(char) * 100), * p=str;
    int size=100;
    char *file_name;
    while((*p++ = fgets( str, size, file_name)) != '\n')
        if(p >= str + size)
            str = (char *) realloc(str, sizeof(char)*(size +=100));
    *--p='\0';
    return str;
}
```
OK I don´t know what is the best way to get(fgets, getc, etc..) the lines from the file, the line can have any dimension (any size).
After that I would like to place the lines in order in a struct, that is, the first line into the fiste "thingy" of the structure.
My structure would look like this one:

struct person{
char *name;----->first line of the file
char *addres;---->second line of the file
char *zip;
int *phone;
int age;----------->fifth line of the file
};
Can I also have some tips on how to put the lines in the struct, I never used struct before. 

Thanks.


----------



## LoneWolf071 (Nov 10, 2004)

is this in C, i'm sorry, but you need to learn Syntex... um... And what does you program do, it opens a txt file and reads lines to allocate memory?


----------

