Too many types in declaration - C (СИ)
Формулировка задачи:
#include <stdio.h>
#include <stdlib.h>
void textfile(int, FILE*);
void sort(int, int, FILE*);
struct proportions{
int x;
int y;
int z;
}
struct laptop {
char model[20]; /*Название модели*/
int price; /*Цена*/
int wt; /*Вес*/
struct proportions size;
int cpu;
};
main() {
FILE *binFile; /*Указатель на файл*/
int option, /*Параметр выбора сортировки*/
i,
countN; /*Количество записей*/
/*Создать laptop с информацией по умолчанию*/
struct laptop sc={ " ", 0, 0 , {0.0 , 0.0 , 0.0}, 0};
if ( (binFile=fopen("C:\\scan.txt", "wb+"))==NULL )
printf("Ошибка файла.\n");
else {
printf("Введите кол-во записей: ");
scanf("%d", &countN);
fwrite(&countN,2,1,binFile);
/*Ввод информации*/
for (i=0; i<countN; i++){
printf("Vvedite model, price, x_,y_size, optr, grey:\n");
fscanf(stdin, "%s%d%d%f%f%f%f", sc.model, &sc.price, &sc.wt, &sc.size.x, &sc.size.y, &sc.size.z, &sc.cpu);
fseek( binFile, i*sizeof(struct laptop)+2, SEEK_SET);
fwrite(&sc, sizeof(struct laptop), 1, binFile);
}
printf("Выберите параметр для сортировки:\n");
printf("1-price\n");
printf("2-grey\n");
scanf("%d", &option);
if (option==1 || option==2)
sort(option,countN,binFile); /*Сортировка с выбором параметра*/
else
printf("Неправильный параметр или ошибка!\n");
textfile(countN, binFile); /*Создание текстового файла на остнове бинарного*/
fclose(binFile);
}
return 0;
}
void sort(int choose, int countN, FILE *signBin) {
int i=0, m[10]={0,1,2,3,4,5,6,7,8,9}, t[10], j, f;
struct laptop sc={ " ", 0, 0 , {0.0 , 0.0 , 0.0}, 0};
struct laptop x={ " ", 0, 0 , {0.0 , 0.0 , 0.0}, 0};
fseek(signBin, 2 ,SEEK_SET);
while (i<countN) {
fread(&sc, sizeof(struct laptop), 1, signBin );
if (choose==1)
t[i]=sc.price;
if (choose==2)
t[i]=sc.cpu;
i++;
}
for(i=0; i<countN-1; i++) {
for(j=1; j<countN; j++) {
if (t[j]<t[j-1]) {
f=t[j];
t[j]=t[j-1];
t[j-1]=f;
f=m[j];
m[j]=m[j-1];
m[j-1]=f;
}
}
}
for (i=0; i<countN; i++) {
fseek(signBin, (m[i])*sizeof(struct laptop)+2, SEEK_SET);
fread(&sc, sizeof(struct laptop), 1, signBin);
fseek(signBin, i*sizeof(struct laptop)+2, SEEK_SET);
fread(&x, sizeof(struct laptop),1, signBin );
fseek(signBin, (m[i])*sizeof(struct laptop)+2, SEEK_SET);
fwrite(&x, sizeof(struct laptop), 1, signBin);
fseek(signBin, i*sizeof(struct laptop)+2, SEEK_SET);
fwrite(&sc, sizeof(struct laptop), 1, signBin);
}
return;
}
void textfile (int countN,FILE *signBin) {
FILE *textf;
int i=0;
struct laptop sc={ " ", 0, 0 , {0.0 , 0.0 , 0.0}, 0};
if ( (textf=fopen("c:\\text.txt", "w"))==NULL )
printf("Ошибка файла(2).");
else {
fseek(signBin, 2, SEEK_SET);
fprintf(textf, "%s%d\n\n", "Vsego strok: ", countN);
fprintf(textf, "|%-20s|%-6s|%-6s|%-10s|%-10s|%-10s|%-8s|\n", "Модель", "Цена", "Вес", "x", "у", "z","CPU");
for(i=0;i<countN;i++) {
fread(&sc, sizeof(struct laptop), 1, signBin );
fprintf(textf, "|%-20s|%6d|%6d|%10.2f|%10.2f|%10.2f|%8f|\n",sc.model, sc.price, sc.wt, sc.size.x, sc.size.y, sc.size.z, sc.cpu);
}
fclose(textf);
}
}Решение задачи: «Too many types in declaration»
textual
Листинг программы
if ( fwrite(&count, sizeof(short), 1, binFile) != 1 ){
perror("fwrite");
exit(EXIT_FAILURE);
}
Объяснение кода листинга программы
- Переменная
countобъявлена как короткое целое число (short). - Функция
fwriteиспользуется для записи данных в файлbinFile. - В качестве передаваемых данных используется переменная
count. - Размер данных, которые нужно записать, указан как 1 байт (sizeof(short)).
- Если запись данных не удалась (возникла ошибка), то выводится сообщение об ошибке с помощью функции
perror. - Выполняется выход из программы с кодом ошибки EXIT_FAILURE.