It is necessary to remove all items whose release year is less than the specified one.

Here is the code: it creates the file and we write the structure there, but then I need to enter the year and the program should delete all the lines that have a year less than the specified one. How to do it?

#include <iostream> #include <fstream> #include <string> using namespace std; struct avto { string mark; string cost; string year; string cvet; }; int main() { int MAX; cout <<"vvedite col-vo avto\n"; cin>>MAX; avto b[MAX]; for (int i=0;i<MAX;++i) { cout << "Enter mark :" << endl; cin >> b[i].mark; cout << "Enter cost :" << endl; cin >> b[i].cost; cout << "Enter year :" << endl; cin >> b[i].year; cout << "Enter cvet :" << endl; cin >> b[i].cvet; } ofstream outfile; outfile.open("Out.txt"); for (int i=0;i<MAX;++i) { outfile << b[i].mark << " " << b[i].cost << " " << b[i].year << " " << b[i].cvet << endl; } system("pause"); return 0; } 
  • one
    1) Open the file with records (F1) for reading. 2) Open a new file (f2) to write. 3) read the records from f1 in turn, if the record satisfies the condition (for example, a year is more than the specified one), we write the record to f2. 4) close the files and replace f1 with f2. - andy.37
  • Similar question - stackoverflow.com/questions/74601/… - Abyx
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky
  • 2
    Format as it should and stop producing identical questions, do you post there for the whole task group? - StateItPrimitive
  • 3
    Oh yeah, I forgot the most important thing, did you at least add a clarification of the problem to the question description, what exactly does not work in the code? Well, or at least describe in words what the code should do and you will quickly write some solution (just your questions are very vague, to say the least). And, nevertheless, format the code in question. - StateItPrimitive

3 answers 3

 #include <cstdio> #include <iostream> #include <fstream> #include <string> using namespace std; struct avto { string mark; string cost; // Лучше хранить как float или double string year; // Лучше хранить как целое число string cvet; }; bool record_fit_your_condition(avto record) { static string min_year = "2010"; return record.year.compare(min_year) > 0; } int main() { int MAX; cout <<"vvedite col-vo avto\n"; cin>>MAX; avto b[MAX]; for (int i=0;i<MAX;++i) { cout << "Enter mark :" << endl; cin >> b[i].mark; cout << "Enter cost :" << endl; cin >> b[i].cost; cout << "Enter year :" << endl; cin >> b[i].year; cout << "Enter cvet :" << endl; cin >> b[i].cvet; } ofstream outfile; outfile.open("Out.txt"); for (int i=0;i<MAX;++i) { outfile << b[i].mark << " " << b[i].cost << " " << b[i].year << " " << b[i].cvet << endl; } outfile.close(); ifstream infile; infile.open("Out.txt"); ofstream outfile1; outfile1.open("Temp.txt"); avto record; for (int i=0; i<MAX; ++i) { infile >> record.mark >> record.cost >> record.year >> record.color; // от cvet - кровь из глаз, уж извините if (record_fit_your_condition(record)) outfile1 << record.mark << " " << record.cost << " " << record.year << " " << record.color << endl; } infile.close(); outfile.close(); std::remove("Out.txt"); std::rename("Temp.txt", "Out.txt"); system("pause"); } 

Somehow, just check, for a long time did not read anything from the files.

  • I get an error on the brackets - Sofia Koltashova
  • A semicolon placed (I missed it in a couple of places)? What brackets? This code should be inside main, well, your outfile should be closed first. - andy.37
  • on these brackets {} - Sofya Koltashova
  • I added) 0 - Sofya Koltashova
  • How can I enter the code? exceeding 1324 characters here - Sofia Koltashova

Well, the first mistake - the comment begins with // , not \\ :)

And the second and main one - you declared record.year as a string , but compare this field with an int . What is more - "liter" or 15? - something like this you did.

This is the answer to your specific question, but this is not all errors. Hint, for example, that when declaring an array, its size should be known when compiling.

  • and what else ???? - Sophia Koltashova
  • @ Sofia Well, I indicated the second one. third, I think the cost should not be done in a string either. Further, if you try to enter as a brand of car, for example, Volkswagen Passat - as two words - then the second word will be the price. I think that's enough for now. - Harry
  • @Harry, this is actually my mistake. - andy.37

Let's start with the little things - you write to a file that is open for reading. But it is a trifle. See for yourself what your program does in the second part: it reads and prints a bunch of records from the file (I hope that it is and is not empty ...), then tries to check the last one (I don’t remember, formally - doesn’t have the right to damage memory in case of a read error), and if it passes a condition, try to add it to the end of the file opened for reading. "Where is the logic ?!" (c) Joke about Vovochka.

And also - if you write code like this without formatting - you are disturbing yourself. Understand, any indents, etc. - this is not a nagging teacher. Agree that such a code, as shown below, is much easier to understand. And modern editors essentially do it themselves (the question is rather - how do you manage to blunt it like that?)

And yet - this is it, despite the use of cin - not C ++.

 #include <iostream> #include <conio.h> #include <stdio.h> #include <stdlib.h> using namespace std; struct avto { char mark[20]; char color[30]; int year; float cost; }; void main () { avto p; FILE *f; f=fopen("D:\\f44.txt","w"); int n; printf("\n kol-?"); scanf("%d",&n); for (int i=0; i<n; i++) { printf("\n mark: "); scanf("%s",&p.mark); printf("\n color: "); scanf("%s",&p.color); printf("\n Year: "); scanf("%d",&p.year); printf("\n Cost: "); scanf("%f",&p.cost); fwrite(&p,sizeof(avto),1,f); } fclose(f); f=fopen("D:\\f55.txt","r"); while(fread(&p,sizeof(avto),1,f)) { printf("\n%s %s %d %f",p.mark,p.color,p.year,p.cost); } int yearmin; cout<<"введите год"; cin>>yearmin; if(p.year>yearmin) fwrite(&p,sizeof(avto),1,f); fclose(f); getch (); }