Click to See Complete Forum and Search --> : Load Data


psiubest
November 29th, 2002, 10:08 PM
I have a litle problem in a C program. The following code is giving
me a lot of work, and it doesn´t work. I have a file that contains data, and i would like to load
that data, but it dont work. I wich to confirm if a line of the file is read is the getline function.
So what i really want to do is to load the data from the file to the 3 fields of the line structure.
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* malloc*/
#include <stddef.h>

#define LF 10
#define nmaxap 40

struct contas_info
{
char num_contas[10];
char nome[50];
char saldo[50];
};

struct contas_info line[nmaxap];


void prnt (int);

int main (void)
{
size_t size;
char *ln, string;
FILE *fptr;
const char delimiters[]="\n";
char *cp;
int i = 0, saldo;
char *token;

if ((fptr = fopen("/usr/users2/deec/ee97122/so/contas.dat", "r")) == NULL)
{
printf ("Não foi possivel abrir o ficheiro \"contas_info\" \n");
return 1;
}
else
{
printf ("\nO ficheiro \"contas_info\" foi aberto \n\n\n");

// ln = (char*)malloc(300);

ln = NULL;
while ((string = getline (ln, &size, fptr)) >= 0)
{


cp = strdup (ln);


token = strtok (cp, delimiters);
strcpy (line[i].num_contas, token);

token = strtok (NULL, delimiters);
strcpy (line[i].nome, token);

token = strtok (NULL, delimiters);
strcpy (line[i].saldo, token);

i++;
}
printf("\nEnd Of File\n\n");
printf ("%s\n",line[i].num_contas);


}

prnt(i);
free(ln);
}

void prnt ( int j)
{
int i;

for (i=0; i<j; i++)
{
printf ("%s\n",line[i].num_contas);
printf ("%s\n",line[i].nome);
printf ("%s\n",line[i].saldo);

}
}


Thnks ppl