I have a homework, which I have to present on Linux with gcc. But I wrote the code on windows in codeblocks (gcc here as well).
It works perfectly on windows.
On linux however, it doesn't. It compiles without errors, but when I run it, it's blank, and waiting for something.
If I remove the 'fileOlvas();' from the main function it works.
Any Idea?
Code: (Sorry I don't know what's important, so I can't make it short)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct person {
char name[200];
char address[200];
char days[200];
int valid;
};
struct person persons[40];
int persons_index = 0;
int day[7] = { 0,0,0,0,0,0,0 };
int napskipp(char betu) {
if (betu == 'h')
{
day[0]++;
return 6;
}
if (betu == 'k')
{
day[1]++;
return 5;
}
if (betu == 'c')
{
day[3]++;
return 10;
}
if (betu == 'p')
{
day[4]++;
return 7;
}
if (betu == 'v')
{
day[6]++;
return 9;
}
if (betu == 'e')
{
day[2]++;
return 7;
}
if (betu == 'o')
{
day[5]++;
return 7;
}
return 0;
}
void count_days() {
day[0] = 0;
day[1] = 0;
day[2] = 0;
day[3] = 0;
day[4] = 0;
day[5] = 0;
day[6] = 0;
for (int i = 0; i < persons_index; i++)
{
int j = 0;
while (j < strlen(persons[i].days))
{
int value;
if (persons[i].valid == 1)
{
if (persons[i].days[j] == 's')
{
value = napskipp(persons[i].days[j + 2]);
j += value;
}
else
{
value = napskipp(persons[i].days[j]);
j += value;
}
}
}
}
}
void fileOlvas() {
char name[200];
char address[200];
char days[200];
int phase = 0;
int i = 0;
FILE* f;
char s;
f = fopen("jelentkezok.txt", "r");
if (f != NULL)
{
while ((s = fgetc(f)) != EOF) {
if (s != '\t')
{
if (phase == 0)
{
name[i] = s;
}
if (phase == 1)
{
address[i] = s;
}
if (phase == 2)
{
days[i] = s;
}
i++;
}
else
{
if (phase == 0)
{
name[i] = '\0';
}
if (phase == 1)
{
address[i] = '\0';
}
phase++;
i = 0;
}
if (s == '\n')
{
days[i - 1] = '\0';
i = 0;
phase = 0;
memcpy(persons[persons_index].name, name, strlen(name));
memcpy(persons[persons_index].address, address, strlen(address));
memcpy(persons[persons_index].days, days, strlen(days));
persons[persons_index].valid = 1;
persons_index++;
count_days(); // <--------- When this is removed, it starts working
}
}
}
fclose(f);
}
int main()
{
fileOlvas();
return 0;
}
Example file:
Lakatos Soma 9495 Kophaza Victor Hugo u. 35.. hetfo kedd
Vincze Imree 9495 Kophaza Victor Hugo u. 35.. szerda csutortok pentek
Bogdan Barna 9495 Kophaza Victor Hugo u. 35.. szombat
Voros Richard 9495 Kophaza Victor Hugo u. 35.. vasarnap hetfo
Szoke Tiborr 9495 Kophaza Victor Hugo u. 35.. kedd szerda csutortok pentek
Mate Viktorr 9495 Kophaza Victor Hugo u. 35.. szombat
Kovacs Florian 9495 Kophaza Victor Hugo u. 35.. vasarnap
Kiraly Dezso 9495 Kophaza Victor Hugo u. 35.. hetfo kedd
Soos Gaborrr 9495 Kophaza Victor Hugo u. 35.. szerda
Dudas Balazs 9495 Kophaza Victor Hugo u. 35.. csutortok
The code should Read data from the file, and it should be stored in a struct array. Then a menu shows up, where you can add modify remove from the array.