сделал программу, но 1-не знаю как перегрузить функцию ввода в текстовом файле без r 2-при работе с текстовым файлов, не правильно выводиться tmp.ves читал что нужно преобразовать символьную строку в число с помощью atof, но незнаю как
| Код | #include <iostream> #include <cstdlib> #include <string> #include <windows.h> #include <stdlib.h> using namespace std; struct sub { char name[15]; double ves; char provod[15]; }; int WriteFile(FILE *f) { sub arr; int n; if (f == NULL) { printf("Error"); exit(0); } cout<<"Введете количество записей:"<<endl; cin>>n; cout<<"Введите название эелемента, удельный вес и тип проводимости"<<endl; for(int i = 0; i < n; i++) { cin >> arr.name >> arr.ves >> arr.provod; fwrite(&arr, sizeof(sub), 1 , f); cout << endl; } fclose(f); return n; } void ReadFile(FILE *f, int z, char *pr) { sub arr; f = fopen("file.dat", "rb"); if (f == NULL) { printf("Error"); exit(0); } printf("\n"); printf("Output file:\n"); cout << "\nПолупроводники:" << endl; for(int i = 0; i < z; i++) { fread(&arr, sizeof(sub), 1 , f); if(strcmp(arr.provod, pr) == 0) cout << arr.name << ' ' << arr.ves << endl; } fclose(f); } //текстовый файл int WriteFile(FILE *f1, float r) { char name[15]; char ves[15]; char provod[15]; int n; if (f1 == NULL) { printf("Error"); exit(0); } cout<<"Введете количество записей:"<<endl; cin>>n; cout<<"Введите название эелемента, удельный вес и тип проводимости"<<endl; for(int i = 0; i < n; i++) { cin >> name; fputs(name , f1); fprintf(f1,"\n"); cin >> ves ; fputs(ves , f1); fprintf(f1,"\n"); cin >>provod; fputs(provod , f1); fprintf(f1,"\n"); cout << endl; } fclose(f1); return n; } void ReadFile(FILE *f1,double m, char *pr) { //string name; //string ves; //string provod; sub tmp; f1 = fopen("file.txt", "r"); if (f1 == NULL) { printf("Error"); exit(0); } printf("\n"); printf("Output file:\n"); cout << "\nПолупроводники:" << endl; for(int i = 0; i < m; i++) { //fgets(name, , f1);fgets(ves ,, f1); fscanf(f1,"%s", &tmp.name); fscanf(f1,"%d", &tmp.ves); fscanf(f1,"%s", &tmp.provod); //cout<<tmp.provod; //memset(provod,0,15); //cout<<provod; //fgets(provod , , f1); //cout<<provod; if(strcmp(tmp.provod, pr) == 0){ //cout<<provod; cout<<tmp.name<<" "; cout<< tmp.ves << endl;}cout<<endl; } fclose(f1); } int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); FILE *f; FILE *f1; int z,x; float r; double m; char* pr = "полупроводник"; cout<<"Выберите тип файла:"<<endl; cout<<"1-текстовый"<<endl; cout<<"2-типизированный"<<endl; cin>>x; cout<<"левое число"<<endl; cin>>r; switch(x) {case 2: f = fopen("file.dat", "wb"); z = WriteFile(f); ReadFile(f, z, pr ); break; case 1: f1 = fopen("file.txt", "w"); m= WriteFile(f1, r); ReadFile(f1, m, pr); } system("pause"); return 0; }
|
| Код | #pragma once int WriteFile( FILE *f); void ReadFile( FILE *f, int z, char* pr); int WriteFile( FILE *f1, r); void ReadFile( FILE *f1, double m, char* pr);
|
|