After hours and hours of searching, I became desperate because I just fail to find uninitialized reads that I apparantly have, according to Valgrind and Dr. Memory, so I'll ask here for help. I used the Code Blocks' debugger and its variable watch, but all I saw there was everything going smoothly and as planned.
#define ADRESA "adressbook.txt"
struct Adress
{
char name[31], lastname[31], email[48], major[10];
int year;
};
int read(struct Adresa array[])
{
char temp_line[121];
int i = 0, j, c = 0;
FILE* adressbookf = fopen(ADRESA, "r");
if (adressbookf != NULL)
{
printf("File successfully loaded.\n")
while (fgets(temp_line, 121, adressbookf) != NULL && strlen(temp_line) > 0)
{
j = 0;
c = 0;
while (temp_line[c] != ',' && c < strlen(temp_line))
{
array[i].name[j] = temp_line[c];
++j;
++c;
}
++c;
array[i].name[j] = '\0';
j = 0;
while (temp_line[c] != ',' && c < strlen(temp_line))
{
array[i].lastname[j] = temp_line[c];
++j;
++c;
}
++c;
array[i].lastname[j] = '\0';
j = 0;
while (temp_line[c] != ',' && c < strlen(temp_line))
{
array[i].email[j] = temp_line[c];
++j;
++c;
}
++c;
array[i].email[j] = '\0';
j = 0;
while (temp_line[c] != ',' && c < strlen(temp_line))
{
array[i].major[j] = temp_line[c];
++j;
++c;
}
++c;
array[i].year = temp_line[c] - 48;
++i;
}
fclose(adressbookf);
return i;
}
else return 0;
}
int main()
{
int number_of_elements = 0;
struct Adress adressbook[1000];
printf("Welcome.\n");
number_of_elements = read(adressbook);
...
Apparently, the lines with error are 32, 43 and 54 (Conditional jump or move depends on uninitialised value(s)).
The file looks something like this:
name,lastname,nlastname1@x.com,EE,1
john,doe,jdoe1@x.com,AA,1
And it usually ends with a trailing line containing '\n' (but the file doesn't necessarily exists).
I have one more error in the program, but I'd have to copy the whole program here. I hope that one will be resolved with this.
If someone would willing to compile it and examine it that way, I'll be happy to post it.
Here's the error message from Valgrind:
Conditional jump or move depends on uninitialised value(s) at 0x4010F1: ucitaj (bs_test_2366.c:156) by 0x40192E: _main (bs_test_2366.c:255) by 0x4021EE: main (bs_test_2366.c:385)
Conditional jump or move depends on uninitialised value(s) at 0x4011AD: ucitaj (bs_test_2366.c:167) by 0x40192E: _main (bs_test_2366.c:255) by 0x4021EE: main (bs_test_2366.c:385)
Conditional jump or move depends on uninitialised value(s) at 0x401269: ucitaj (bs_test_2366.c:178) by 0x40192E: _main (bs_test_2366.c:255) by 0x4021EE: main (bs_test_2366.c:385)
Note that lines 156, 167 are 178 the first, second and the third
while (temp_line[c] != ',' && c < strlen(temp_line))
respectively. 255 is the line where the function is called, and 385 is outside of my program.