ЛР 6 правки

parent f4bc8066
BUILD_DIR=build
SRC_DIR=src
CC=gcc
CFALGS=-Wall -g
all: main1.elf main2.elf
main1.elf: libstudents.so
$(CC) $(CFALGS) -lstudents -L ./$(BUILD_DIR)/ -o main1.elf $(SRC_DIR)/main1.c
echo export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$PWD"
main2.elf: libstudents.so
$(CC) $(CFALGS) -lstudents -L ./$(BUILD_DIR)/ -o main2.elf $(SRC_DIR)/main2.c
echo export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$PWD"
libstudents.so:
$(CC) $(CFALGS) -shared -fpic -o $(BUILD_DIR)/libstudents.so $(SRC_DIR)/students.c
clean:
rm $(BUILD_DIR)/*o
rm main*.elf
#include <stdlib.h>
#include "students.h"
int
main()
{
int n;
printf("Enter the amount of students:");
scanf("%d", &n);
getchar();
struct Student *students = allocate_array(n);
fill_array(n, students);
FILE *file = NULL;
file = fopen("students.cvs", "a");
write_array(file, n, students);
fclose(file);
free(students);
puts("Students to read:");
scanf("%d",&n);
getchar();
if(n <= 0)
exit(0);
file = fopen("students.cvs","r");
if(!file){
puts("Could not open file!");
exit(0);
}
struct Student *students2 = read_file_to_array(file, n);
fclose(file);
puts("From file:");
print_array(n, students2);
free(students2);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "students.h"
int
main()
{
int n;
printf("Enter the amount of students:");
scanf("%d", &n);
getchar();
struct Student *students = allocate_array(n);
fill_array(n, students);
FILE *file = NULL;
file = fopen("students.bin", "wb");
bin_write_array(file, n, students);
fclose(file);
free(students);
puts("Students to read:");
scanf("%d",&n);
getchar();
if(n <= 0)
exit(0);
file = fopen("students.bin","rb");
if(!file){
puts("Could not open file!");
exit(0);
}
struct Student *students2 = bin_read_file_to_array(file, n);
fclose(file);
puts("From file:");
print_array(n, students2);
free(students2);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct Student{
unsigned id;
char name[50];
char faculty[50];
float rating;
};
struct Student *
allocate_array(int n){
struct Student *students = NULL;
students = malloc(sizeof(struct Student)*n);
if(!students)
exit(1);
return students;
}
void
fill_array(int n, struct Student *students)
{
for(int i = 0; i < n; i++){
printf("\nEnter student's № %d info:\n", i);
printf("\tid:");
scanf("%u", &students[i].id);
getchar();
printf("\tname:");
fgets(students[i].name, sizeof(students[i].name), stdin);
students[i].name[strcspn(students[i].name, "\n")] = 0;
printf("\tfaculty:");
fgets(students[i].faculty, sizeof(students[i].faculty), stdin);
students[i].faculty[strcspn(students[i].faculty, "\n")] = 0;
printf("\trating:");
scanf("%f", &students[i].rating);
getchar();
}
}
void
write_array(FILE *file, int n, struct Student *students)
{
for(int i = 0; i < n; i++){
fprintf(file, "%u,%s,%s,%f\n", students[i].id, students[i].name, students[i].faculty, students[i].rating);
}
}
void
bin_write_array(FILE *file, int n, struct Student *students)
{
fwrite(&students[0], sizeof(struct Student), n, file);
}
struct Student*
bin_read_file_to_array(FILE *file, int n)
{
struct Student *students = allocate_array(n);
fread(students, sizeof(struct Student), n, file);
return students;
}
struct Student *
read_file_to_array( FILE *file, int n)
{
char *buf = NULL;
buf = malloc(255);
struct Student *students = allocate_array(n);
if(!buf)
exit(1);
char *tmp;
int i = 0;
while(fgets(buf, sizeof(struct Student), file) != NULL && i != n){
if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == '\n'))
buf[strlen (buf) - 1] = '\0';
tmp = strtok(buf, ",");
students[i].id = atoi(tmp);
tmp = strtok(NULL, ",");
strcpy(students[i].name, tmp);
tmp = strtok(NULL, ",");
strcpy(students[i].faculty, tmp);
tmp = strtok(NULL, ",");
students[i].rating = atof(tmp);
i++;
}
free(buf);
return students;
}
void
print_array(int n, struct Student *students)
{
for(int i = 0; i < n; i++){
printf("Student №%d\n\tid:%u\n\tname:%s\n\tfaculty:%s\n\trating:%f\n", i, students[i].id, students[i].name, students[i].faculty, students[i].rating);
}
}
#ifndef STUDENTS_H
#define STUDENTS_H
#include <stdio.h>
struct Student *allocate_array(int n);
void fill_array(int n, struct Student *students);
void print_array(int n,struct Student *students);
void write_array(FILE *file, int n, struct Student *students);
struct Student* read_file_to_array(FILE *file, int n);
void bin_write_array(FILE *file,int n, struct Student *students);
struct Student* bin_read_file_to_array(FILE *file, int n);
#endif
1,nig,ger,0.000000
2,maxwel gaylord,travel,10.000000
1,ni,ggdr,2.000000
2,j,j,10.000000
1,jf,fa,9.000000
1,2,3,4.000000
1,2,3e,4.000000
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment