Mình có 1 file INPUT.txt nội dung như sau:
Barack Hussein Obama - 04/08/1961 - 10 - 9 - 8
Mình muốn xuất ra file OUTPUT.txt có định dạng:
Information:
Name: Barack Hussein Obama
Day of birth: 4
Month of birth: 8
Year of birth: 1961
Mark of Math, Physics, Chemistry: 10 9 8
mà có sử dụng lớp seekp() cho file OUTPUT để quay về đầu file in ra từ Information:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream File("Date.txt", ios::in);
string name;
int day, month, year, Math, Physics, Chemistry;
getline(File, name, '-');
name.erase(name.begin() + name.size() - 1);
File >> day;
File.seekg(1, ios_base::cur); // .tellg();
File >> month;
File.seekg(1, ios_base::cur);
File >> year;
File.seekg(3, ios_base::cur);
File >> Math;
File.seekg(3, ios_base::cur);
File >> Physics;
File.seekg(3, ios_base::cur);
File >> Chemistry;
File.close();
File.open("Result.txt", ios::out);
File << "Name: " << name << endl;
File << "Day of birth: " << day << endl;
File << "Month of birth: " << month << endl;
File << "Year of birth: " << year << endl;
File << "Mark of Math, Physics, Chemistry: " << Math << " " << Physics << " " << Chemistry;
File.seekp(0, ios_base::beg); // .tellp();
File << "Information: " << endl;
File.close();
return 0;
}
Và đây là kết quả thực tế trong file OUTPUT.txt:
Information:
ssein Obama
Day of birth: 4
Month of birth: 8
Year of birth: 1961
Mark of Math, Physics, Chemistry: 10 9 8
Như các bạn đã thấy là sau khi quay về đầu file OUTPUT bằng seekp() thì một số ký tự đầu file ban đầu bị mất đi. Mình không biết sửa lỗi này thế nào nên bạn nào biết chỉ giúp mình với ạ !
Cảm ơn nhiều!!
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?