You are on page 1of 3

LAB 6

BÀI 1
#include <iostream>
#include <fstream>

int main()
{
std::ofstream file("file_numbers.txt");

if (file.is_open())
{
for (int i = 1; i <= 100; ++i)
{
file << i << '\n';
}
file.close();

std::cout << "Da tao tep tin thanh cong." << std::endl;
}
else
{
std::cerr << "Khong the mo tep tin de ghi." << std::endl;
}

return 0;
}

BÀI 2
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>

using namespace std;


int arr[1001];
ofstream output("1.txt");
int main() {
int N;
cin >> N;
//cau a
for (int i = 0; i < N; i++) {
cin >> arr[i];
output << arr[i] << " ";
}
output << endl;
output.close();
//cau b
sort(arr, arr + N);
output.open("2.txt");
for (int i = 0; i < N; i++) {
output << arr[i] << " ";
}
output.close();
remove("1.txt");
rename("2.txt", "sort.txt");
//cau c
output.open("sort.txt", std::ios::app);
output << 2015 << endl;
output.close();
}

BÀI 3
#include<iostream>
#include<fstream>
#include<cstring>

using namespace std;


ifstream input("sv.txt");
ofstream output("svtren7.txt");

int main() {
string name;
while (getline(input, name)) {
int score = 0;
input >> score;
if (score > 7) {
output << name << endl << score << endl;
}
}
system("pause");
}

BÀI 6
#include<fstream>
#include<iostream>

using namespace std;


ifstream input("input.txt");
ofstream output("output.txt");

int main() {
int N, M;
input >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
int num;
input >> num;
output << num << " ";
}
output << endl;
}
}

You might also like