explain function

Hi,
i do not understand this code. Pls can someone explain this one, especially
the function nacitajString

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


size_t resize = 10;

struct man
{
int ID;
int age;
char* firstname;
char* lastname;
};

char* nacitajString(FILE* inputFile)
{

char znak;
char* string = malloc(resize);

size_t alocated = resize;
size_t actual = 0;

while (EOF != (znak = fgetc(inputFile)) && znak != ' ' && znak != '\n')
{

if (actual +1 >= alocated)
{
alocated += resize;
char* tmp = realloc(string, alocated);
string = tmp;
}

string[actual++] = znak;
}
string[actual] = '\0';

return string;
}

int main()
{


FILE* inputFile = fopen("input.txt", "r");
char znak;
int i = 0;
char* pocet = nacitajString(inputFile);
int pocetRiadkov = atoi(pocet);

struct man person[pocetRiadkov];

for ( i = 0; i < pocetRiadkov; i++)
{
char* ID = nacitajString(inputFile);
person[i].ID = atoi(ID);

char* age = nacitajString(inputFile);
person[i].age = atoi(age);

person[i].firstname = nacitajString(inputFile);

person[i].lastname = nacitajString(inputFile);

}



FILE* outputFile = fopen("output.txt", "w");

for ( i = 0; i < pocetRiadkov; i++)
{
fprintf(outputFile, "%d ", person[i].ID);
}

fprintf(outputFile, "\n");

for ( i = 0; i < pocetRiadkov; i++)
{
fprintf(outputFile, "%d ", person[i].age);
}

fprintf(outputFile, "\n");

for ( i = 0; i < pocetRiadkov; i++)
{
fprintf(outputFile, "%s ", person[i].firstname);
}

fprintf(outputFile, "\n");

for ( i = 0; i < pocetRiadkov; i++)
{
fprintf(outputFile, "%s ", person[i].lastname);
}

fclose(outputFile);
fclose(inputFile);

return 0;
}
I will explain to you what nacitajString does, maybe you can figure out the rest of the code then alone.
What it does is simply giving back a pointer to a string in the file which does not contain a white space ' ' or a newline '\n' meaning that with further call of the function further strings within the file which are separated by white spaces and newlines are returned.

How it works is a bit more difficult. You know that a string could theoretically be of infinite length until it is terminated by a 0 Character ('\0'). At runtime your program doesn't know how long the strings within your text file are so there are basically two options:

*Reserve enough space so that most string can fit into it (for example reserve 1024 characters for each string (char[1024])) and print an error message if one string exceeds even that, or...

*Make the memory 'reservation' dynamically, meaning something like that:
Reserve 10 characters
String is too long?
Reserve another 10 characters
String is still to long?
Reserve another 10 characters
...

The last option is done by nacitajString. Follow the function carefully and you will understand. malloc and realloc are used for reserving and re-reserving memory and making it accessible for your application. The function fgetc(FILE*) returns one character of the file. The next call of fgetc(FILE*) returns the next character etc.

mfG
Machtl
Topic archived. No new replies allowed.