从文件中读取选定的值并将它们写入c ++中的新文件中(Reading selected values from a file and writing them in a new file in c++

编程入门 行业动态 更新时间:2024-10-13 00:37:49
从文件中读取选定的值并将它们写入c ++中的新文件中(Reading selected values from a file and writing them in a new file in c++)

我想从键盘输入用户输入(整数值)并将它们写入文件中。然后,选择18-80中的值并写入另一个文件。我的程序一直运行,直到键盘输入并将它们写入文件。 但我不知道如何在条件下选择一些值并将它们写入另一个文件中。

#include<iostream> #include<fstream> using namespace std; int main() { ofstream age; age.open("age.txt",ios::out); cout<<"Input the ages from keyboard: "<<endl; for(int i=0;i<3;i++) { int n; cin>>n; //value inputted from keyboard age<<n<<endl; } ifstream agein; agein.open("age.txt"); //Reading that existing file ofstream ageout; ageout.open("information.txt"); //writing in another file { int m; if(m>18 && m<=80) //picking value from 18-80 ageout<<m<<endl; } age.close(); agein.close(); ageout.close(); return 0; }

I wanted to take user input(integer value) from keyboard and writing them in a file.Then, to select the values from 18-80 and writing in another file.My program runs till keyboard input and writing them in a file. But I don't know how to select some values under condition and writing them in another file.

#include<iostream> #include<fstream> using namespace std; int main() { ofstream age; age.open("age.txt",ios::out); cout<<"Input the ages from keyboard: "<<endl; for(int i=0;i<3;i++) { int n; cin>>n; //value inputted from keyboard age<<n<<endl; } ifstream agein; agein.open("age.txt"); //Reading that existing file ofstream ageout; ageout.open("information.txt"); //writing in another file { int m; if(m>18 && m<=80) //picking value from 18-80 ageout<<m<<endl; } age.close(); agein.close(); ageout.close(); return 0; }

最满意答案

不要忘记在重新打开文件之前关闭文件,即使您从读/写切换。 干得好:

using namespace std; int main() { ofstream age; age.open("age.txt", ios::out); cout << "Input the ages from keyboard: " << endl; for (int i = 0;i<3;i++) { int n; cin >> n; //value inputted from keyboard age << n << endl; } age.close();// Do NOT forget that before reopening it ifstream agein; agein.open("age.txt"); //Reading that existing file ofstream ageout; ageout.open("information.txt");//writing in another file if(ageout)//Checks if the file exists { string str; int m; while (agein >> m) //Get an int from a file if (m>18 && m <= 80) //picking value from 18-80 ageout << m << endl; } agein.close(); ageout.close(); return 0; }

Don't forget to close your file before reopening it, even if you switch from read/write. Here you go:

using namespace std; int main() { ofstream age; age.open("age.txt", ios::out); cout << "Input the ages from keyboard: " << endl; for (int i = 0;i<3;i++) { int n; cin >> n; //value inputted from keyboard age << n << endl; } age.close();// Do NOT forget that before reopening it ifstream agein; agein.open("age.txt"); //Reading that existing file ofstream ageout; ageout.open("information.txt");//writing in another file if(ageout)//Checks if the file exists { string str; int m; while (agein >> m) //Get an int from a file if (m>18 && m <= 80) //picking value from 18-80 ageout << m << endl; } agein.close(); ageout.close(); return 0; }

更多推荐

本文发布于:2023-04-29 02:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将   新文件   文件   Reading   writing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!