Witam
Mam problem. Kiedy ładuje rekordy imię nazwisko i id z pliku do struktury to przy wyświetlaniu pokazuje mi jakąś dodatkową linię której w ogóle nie ma w pliku.. Wydaje mi się że to problem ze fscanf bo po wczytaniu wszystkich rekordow do struktury zostanie \r\n i właśnie przez to jakies dodatkowe linie. Proszę o jakąś pomoc lub radę. Z góry dzięki. Dodaje tylko kawałek kodu bo jest strasznie dlugi. Mam nadzieje ze to wystarczy.
void Load_File()
{
FILE *pFile;
pFile = fopen("Database.txt", "r");
if(!pFile)
return;
while(!feof(pFile))
{
AddRecord(pFile);
}
fclose(pFile);
}
void AddRecord(FILE* f = NULL)
{
const unsigned int bufferSize = 256;
char strBuffer[bufferSize];
StudentCount++;
Students = (SStudentData*)realloc(Students, (StudentCount)*sizeof(SStudentData));
strBuffer[0] = 0;
if(f == NULL) // load from user
GetString(strBuffer, bufferSize-1, "Podaj imie:");
else // load from file
fscanf(f,"%s",strBuffer);
Students[StudentCount-1].FName = (char*)calloc(strlen(strBuffer)+1,sizeof(char));
strcpy(Students[StudentCount-1].FName, strBuffer);
if(f == NULL) // load from user
GetString(strBuffer, bufferSize-1, "Podaj nazwisko:");
else // load from file
fscanf(f,"%s",strBuffer);
Students[StudentCount-1].LName = (char*)calloc(strlen(strBuffer)+1,sizeof(char));
strcpy(Students[StudentCount-1].LName, strBuffer);
if(f == NULL){ // load from user
printf("Podaj nr indeksu:");
scanf("%d", &(Students[StudentCount-1].ID));
}
else
fscanf(f,"%d",&(Students[StudentCount-1].ID));
}
void PrintDatabase()
{
for(int i=0; i<StudentCount; i++)
printf("%s | %s | %06d\n",
Students[i].FName,
Students[i].LName,
Students[i].ID);
printf("\n");
}
int main()
{
const int answerBufLen = 7;
char answer[answerBufLen];
bool exitProgram = false;
SetCP1250();
Load_File();
do
{
printf("1. Wyswietlenie wszystkich rekordow.\n");
printf("2. Dodanie rekordu.\n");
printf("3. Sortowanie po ID.\n");
printf("4. Sortowanie po imieniu.\n");
printf("5. Sortowanie po nazwisku.\n");
printf("6. Wyjscie z programu.\n\n");
GetString(answer, answerBufLen-1, "Wybierz opcje:");
if(strcmp(answer,"1")==0)
{
PrintDatabase();
}
else if(strcmp(answer,"2")==0)
{
AddRecord();
}