Witam :)
Podrzuci ktoś pomysł, bądź wytknie błędy w moim kodzie odnośnie funkcji "funkcja" ?:)
Wyjaśniam, że:
n-liczba rekordów z wczytanego pliku;
tab1- tablica dynamiczna, o 1 mniejsza od tab w mainie;
Funkcja pyta o ID studenta i kopiując zawartość z tab do tab1 pomija go.
Oto i kod:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void funkcja(int n,int i, struct student *p){
struct student *tab1;
int id;
printf("n=%d",n);
tab1=( struct student *) malloc ((n-1)* sizeof(struct student));
printf("Podaj ID ktore ma byc usuniete:\n");
scanf("%d", &id);
for(int j=0, i=0;i<n;i++){
if(id == p[i].nr_ID){
continue;
}else{
tab1[j]=p[i];
j++;
}
}
for(i=0;i<n-1;i++){
printf("%d %s %s %d\n", tab1[i].nr_ID, tab1[i].imie, tab1[i].nazwisko, tab1[i].punkty);
}
free(tab1);
};
int wiersze(FILE *plik){
int wiersz=0;
char znak;
while((znak = getc(plik)) != EOF) {
if(znak == '\n')
++wiersz;
}
return wiersz;
}
struct student{
int nr_ID;
char nazwisko[20];
char imie[20];
double punkty;
};
int main(){
struct student *tab;
char plik[10];
int i=0,n=0;
FILE *stream;
printf("Podaj nazwe pliku, ktory chcesz otworzyc:\n");
scanf("%s",plik);
strcat(plik, ".txt");
stream=fopen(plik,"r");
if(stream==NULL){
printf("Blad przy otwieraniu pliku. Koniec programu\n");
return 1;
}else{
printf("Wczytano plik\n");
}
n = wiersze(stream);
printf("n=%d", n);
n=n+1;
rewind(stream);
tab = (struct student *)malloc(n*sizeof(struct student));
if (fgetc(stream) == EOF)
{
printf("\nPlik jest pusty!\nKoniec programu\n");
fclose(stream);
system("pause");
return 0;
}
else
{
rewind(stream); //jesli plik nie jest pusty, przewijamy kursor na poczatek pliku
}
printf("\n\nZawartosc %s:\n\n", plik);
while (!feof(stream)) {
for(i=0;i<n;i++){
fscanf( stream,"%d%s%s%d", &tab[i].nr_ID, &tab[i].imie, &tab[i].nazwisko, &tab[i].punkty);
printf("%d %s %s %d\n", tab[i].nr_ID, tab[i].imie, tab[i].nazwisko, tab[i].punkty);
}
}
printf("Zawartosc tab\n");
for(i=0;i<n;i++){
printf("%d %s %s %d\n", tab[i].nr_ID, tab[i].imie, tab[i].nazwisko, tab[i].punkty);
}
funkcja(n,i,tab);
fclose( stream );
free(tab);
return 0;
}
Dołączam listę błędów: http://www10.zippyshare.com/v/G6RujaMM/file.html