如何从文件中读取数值,分钟和第二个值,同时将值“精确地给定”写入其他文件

问题描述 投票:0回答:1

首先,有可能吗?

推断问题:我想从文件中读取一些整数,这对我来说比较简单。但是,当我读取以下格式的值时:例如“ 123 17 24 55 04 30 09”,我的书面输出显示为“ 123 17 24 55 4 30 9”,其中“ 0”在4之前不存在,并且9.这从格式方面困扰我。如何在输出中保留4和9前面的0?

附上我当前的代码

int main() {

ifstream inf;
ofstream of;
float timeSmin, timeSsec, timeBmin, timeBsec=01.00, timeRmin=01, timeRsec=01.00;
int ID, totaltime;


inf.open ("triath.txt");
of.open ("output.txt");

if (!inf.is_open()){
  cout << "Triath.txt cannot be opened, error..." << endl;
}

inf >> ID >> timeSmin >> timeSsec >> timeBmin >> timeBsec >> timeRmin >> timeRsec; //writing values to input object.

of << ID << " " << timeSmin << " " << timeSsec << " " << timeBmin << " " << timeBsec << " " << timeRmin << " " << timeRsec;


inf.close();
of.close();
return 0;
} ```
c++ fstream ifstream ofstream
1个回答
0
投票

我将以字符串形式读取文件并解析该字符串。这样就为您提供了用于输出的原始字符串。

您需要一种将输入字符串拆分为标记的方法。如果您不知道该怎么办,可以使用google搜索“用空格将c ++分隔字符串”,并获得一堆不错的点击率。

然后您需要将子字符串转换为整数。再次,用于“ C ++将字符串转换为整数”的Google会提供一些线索。

然后,您仅按原样输出原始字符串,它与您最初阅读的内容相同。

© www.soinside.com 2019 - 2024. All rights reserved.