C++ 代码将写入文本文件的第一行
下面是我正在使用的代码,它应该从包含姓名、当前薪资和百分比增长的薪资数据中读取信息,然后将新信息写入NewData文件。它读取第一行,然后一遍又一遍地重写到新文件中。它需要能够读取和写入工资文件中的每一行C++ 代码将写入文本文件的第一行,c++,while-loop,infinite-loop,C++,While Loop,Infinite Loop,下面是我正在使用的代码,它应该从包含姓名、当前薪资和百分比增长的薪资数据中读取信息,然后将新信息写入NewData文件。它读取第一行,然后一遍又一遍地重写到新文件中。它需要能够读取和写入工资文件中的每一行 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ This Program does two things, first it reads data from a file, and second it forma
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This Program does two things, first it reads data from a file, and second
it formats number output...
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#include<iostream>
#include<fstream>
#include<string>
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
using namespace std;
int main()
{
// need an input file variable reference
ifstream fin;
ofstream myfile;
string lastName, firstName, str;
double salary, newSalary, percentIncrease;
// Output File
// Place the directory to the file you want to write to here.
myfile.open("C:/Users/Adam/Desktop/NewData.txt");
// What is being wrote to the output file.
myfile << "Writing this to a file.\n";
// Open the file named numbers.txt
fin.open("C:/Users/Adam/Desktop/SalaryData.txt"); // catch, file has
to be in the same folder as the .cpp
// OR
// use a EOF While loop to loop through an input file
// to be safe, always PRIME your EOF While loops
fin >> lastName >> firstName >> salary >> newSalary >> percentIncrease;
// reads first eligible item (in this case, an integer) from the file
while (! fin.eof()) {
newSalary = salary * (percentIncrease / 100) + salary;
cout << lastName << firstName << newSalary;
myfile << lastName << firstName << newSalary;
fin >> lastName >> firstName >> salary >> newSalary;
}
cout << endl;
cin.get();
return 0;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
这个程序做两件事,第一件从文件中读取数据,第二件
它格式化数字输出。。。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#包括
#包括
#包括
#包括//std::cout,std::fixed
#include//std::setprecision
使用名称空间std;
int main()
{
//需要输入文件变量引用
流鳍;
流文件;
字符串lastName,firstName,str;
双薪、新工资、百分比增长;
//输出文件
//将目录放在要写入的文件中。
myfile.open(“C:/Users/Adam/Desktop/NewData.txt”);
//正在写入输出文件的内容。
myfile>lastName>>firstName>>工资>>新闻收入>>百分比增长;
//从文件中读取第一个符合条件的项(在本例中为整数)
而(!fin.eof()){
新工资=工资*(增加百分比/100)+工资;
工资>>新闻汇编;
}
不能而不是
fin>>lastName>>firstName>>薪资>>新薪资>>百分比增长;
而(!fin.eof()){
//做你的事
fin>>姓氏>>姓氏>>薪资>>新闻薪资;
}
尝试将fin>
放入while循环中
while(fin>>lastName>>firstName>>薪资>>新薪资>>百分比增长){
//做你的事
}
因为fin>>可以在一个>>调用中导致eofbit,在下一个>>调用中导致badbit。而(!fin.eof()){