1. for va foreach sikllari


Download 65.67 Kb.
bet3/49
Sana19.06.2023
Hajmi65.67 Kb.
#1625730
1   2   3   4   5   6   7   8   9   ...   49
Bog'liq
дəстурлеу жуаплары100

4. Fayl oqimlari
Fayl oqimlari, fayllarning o'qilish va yozilish jarayonlarini belgilash uchun ishlatiladigan ma'lumotlardir. Fayllar, ma'lumotlarni saqlash uchun foydalaniladi va dasturlashda keng qo'llaniladi.
Quyidagi fayl oqimlari C++ dasturlash tilida ishlatiladi:
1. Fayl ochish (File opening): Faylni ochish uchun, fstream kutubxonasidan foydalanish mumkin. Fayl ochish amalga oshirilgandan so'ng, faylga yozish yoki o'qish amalga oshirilishi mumkin.
cpp
#include
#include
using namespace std;
int main() {
string line;
ifstream myfile("example.txt");
if (myfile.is_open()) {
while (getline(myfile,line)) {
cout << line << '\n';
}
myfile.close();
} else {
cout << "Unable to open file";
}
return 0;
}
2. Fayl yozish (File writing): Faylga yozish uchun, ofstream kutubxonasidan foydalanish mumkin. Fayl yozish amalga oshirilgandan so'ng, fayl ochish va o'qish amalga oshirilishi mumkin.
cpp
#include
#include
using namespace std;
int main() {
ofstream myfile("example.txt");
if (myfile.is_open()) {
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
} else {
cout << "Unable to open file";
}
return 0;
}
3. Faylga yozish davom etish (Appending to a file): Faylga yozish davom etish uchun, ofstream kutubxonasidan foydalanish mumkin. ios::app argumenti orqali, faylga yozish davom etish amalga oshirilishi mumkin.
cpp
#include
#include
using namespace std;
int main() {
ofstream myfile;
myfile.open("example.txt", ios::app);
if (myfile.is_open()) {
myfile << "This is another line.\n";
myfile.close();
} else {
cout << "Unable to open file";
}
return 0;
}
4. Fayldan o'qish va yozish (File reading and writing): Fayldan o'qish va yozish uchun, fstream kutubxonasidan foydalanish mumkin.
cpp
#include
#include
using namespace std;
int main() {
string line;
fstream myfile("example.txt", ios::in | ios::out | ios::app);
if (myfile.is_open()) {
while (getline(myfile,line)) {
cout << line << '\n';
}
myfile << "This is a new line.\n";
myfile.close();
} else {
cout << "Unable to open file";
}
return 0;
}
Fayl oqimlarini o'rganish, siz dasturlashda ma'lumotlarni saqlash va ulardan foydalanishning qulay usullaridan birini o'rganishingizga yordam beradi.

Download 65.67 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   49




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling