NIM : 195150307111007
Kelas : Teknik Komputer B
1. Buatlah sebuah program yang menampilkan masukan berupa Nama, Nim, Alamat
yang mempunyai tipe data String.
Tulis File.cpp
1 #include <iostream>
2 #include <fstream>
3 using namespace std;
4 int main () {
5 string isinim, isinama, isialamat;
6 cout << "PENDATAAN MAHASISWA AKTIF FILKOM TAHUN 2020\n";
7 cout << "-------------------------------------------\n";
8 cout << "NIM \t: ";
9 cin >> isinim;
10 cout << "Nama \t: ";
11 cin >> isinama;
12 cout << "Alamat \t: ";
13 cin >> isialamat;
14 cout << "\nSELAMAT DATA ANDA SUDAH MASUK DALAM SISTEM
15 KAMI\n";
16 ofstream myfile ("Wisnumurti.txt", ios::app);
17 if (myfile.is_open())
18 {
19 myfile << "NIM \t: " << isinim << endl;
20 myfile << "Nama \t: " << isinama<< endl;
21 myfile << "Alamat \t: " << isialamat << endl << endl;
22 myfile.close();
23 }
24 else
25 {
26 cout << "Unable to open file";
27 }
28 return 0;
}
Screenshot
2. Simpan hasil masukan user ke dalam sebuah file dan tampilkan isi dari file.
Baca File.cpp
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 using namespace std;
5 int main () {
6 string line;
7 ifstream myfile ("Ibrahim Kumail.txt");
8 if (myfile.is_open())
9 {
10 cout << "DATA MAHASISWA AKTIF FILKOM TAHUN 2020\n";
11 cout << "--------------------------------------\n";
12 while ( getline (myfile,line) )
13 {
14 cout << endl;
15 cout << line;
16 }
17 myfile.close();
18 }
19 else {
20 cout << "Unable to open file";
21 }
22 return 0;
23 }
Screenshot
3. Simpan hasil beberapa kali masukan user ke dalam sebuah file. File yang sudah ada
isinya akan ditambah di bagian bawah dengan isi yang baru.
Screenshot
Screenshot
Screenshot