Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <conio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string>
- #pragma warning (disable:4996)
- bool CreateNewFile(char * FileName, int stQuantity);
- bool PrintFile(char * FileName);
- bool WriteNewFileBySgroup(char * readFileName, char * writeFileName, char* group);
- struct Student {
- char group[30];
- char fam[50];
- int stip;
- };
- int main()
- {
- int StQuantity(0);
- char Sfile[100], ESfile[100];
- printf("Input Students file filename: ");
- gets_s(Sfile);
- printf("Input quantity of students: ");
- scanf("%d", &StQuantity);
- printf("\n");
- getchar();
- bool checkStatus = CreateNewFile(Sfile, StQuantity);
- if (checkStatus) {
- printf("File was created!\n");
- }
- else {
- printf("Error occured!\n");
- return -1;
- }
- checkStatus = PrintFile(Sfile);
- if (checkStatus) {
- printf("File was read!\n");
- }
- else {
- printf("Error occured!\n");
- return -1;
- }
- printf("\n");
- char group[30];
- printf("Input specified group of students: ");
- scanf("%s", &group);
- printf("\nInput new students file filename: ");
- scanf("%s", &ESfile);
- checkStatus = WriteNewFileBySgroup(Sfile, ESfile, group);
- if (checkStatus) {
- printf("File was created!\n");
- }
- else {
- printf("Error occured!\n");
- return -1;
- }
- checkStatus = PrintFile(ESfile);
- if (checkStatus) {
- printf("File was read!\n");
- }
- else {
- printf("Error occured!\n");
- return -1;
- }
- system("pause");
- return 0;
- }
- bool CreateNewFile(char * FileName, int stQuantity) {
- FILE * fileStream = fopen(FileName, "wb");
- if (!fileStream) {
- return false;
- }
- Student st;
- for (int i = 0; i < stQuantity; i++) {
- printf("Input student group: ");
- scanf("%s", &st.group);
- printf("Input fam of student: ");
- scanf("%s", &st.fam);
- printf("Input stip of student: ");
- scanf("%d", &st.stip);
- fwrite(&st, sizeof(st), 1, fileStream);
- printf("\n");
- }
- fclose(fileStream);
- return true;
- }
- bool PrintFile(char * FileName) {
- FILE * fileStream = fopen(FileName, "rb");
- if (!fileStream) {
- return false;
- }
- Student st;
- while (fread(&st, sizeof(st), 1, fileStream)) {
- printf("%s : %s : %d\n", st.group, st.fam, st.stip);
- }
- fclose(fileStream);
- return true;
- }
- bool WriteNewFileBySgroup(char * readFileName, char * writeFileName, char* group) {
- FILE * readFileStream = fopen(readFileName, "rb");
- FILE * writeFileStream = fopen(writeFileName, "wb");
- if ((!readFileStream) || (!writeFileStream)) {
- return false;
- }
- Student st;
- while (fread(&st, sizeof(st), 1, readFileStream)) {
- if (strcmp(st.group, group) == 0 && st.stip > 0) {
- fwrite(&st, sizeof(st), 1, writeFileStream);
- }
- }
- fclose(readFileStream);
- fclose(writeFileStream);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement