180 likes | 288 Vues
This article provides a complete guide on how to save and retrieve data using file streams in C++. It covers the essential libraries, primarily `<iostream.h>` and `<fstream.h>`, and demonstrates various methods for writing to and reading from files. The examples include creating files, appending data, reading data into character arrays, and searching for specific entries. With practical code snippets, you'll learn to manage file I/O operations effectively in your applications. Ideal for beginners and intermediate programmers looking to enhance their skills.
E N D
/ ذخيره و بازيابي
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() } fstream f1; f1.open("d:\\aaa.txt",ios::out); f1<<"Ali Reza”; {
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; f1.open("d:\\aaa.txt",ios::app); f1<<"Ali Reza"<<endl; }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; f1.open("d:\\aaa.txt",ios::out); f1<<"Ali Reza"<<endl; f1<<"Hamid"; }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; char a; f1.open("d:\\aaa.txt",ios::in); f1>>a; cout<<a; }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; char a[5]; f1.open("d:\\aaa.txt",ios::in); f1>>a; cout<<a; }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; char a; f1.open("d:\\aaa.txt",ios::in); f1>>a; cout<<a; f1>>a; cout<<a; f1>>a; cout<<a; f1>>a; cout<<a; f1>>a; cout<<a; }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; char a; f1.open("d:\\aaa.txt",ios::in); f1.unsetf(ios::skipws); f1>>a; cout<<a; f1>>a; cout<<a; f1>>a; cout<<a; f1>>a; cout<<a; f1>>a; cout<<a; }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; char a; f1.open("d:\\aaa.txt",ios::in); f1.unsetf(ios::skipws); for(int c=0;c<=20;c++) { f1>>a; cout<<a; } }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; char a; f1.open("d:\\aaa.txt",ios::in); f1.unsetf(ios::skipws); for(int c=0;c<=20;c++) { f1>>a; cout<<a; cout<<f1.eof(); } }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; char a; f1.open("d:\\aaa.txt",ios::in); f1.unsetf(ios::skipws); while(!f1.eof()) { f1>>a; cout<<a; } }
ذخیره و بازیابی در C++ main() { fstream f1,f2; char a,b; f1.open("d:\\aaa.txt",ios::in); f2.open("d:\\bbb.txt",ios::out); f1.unsetf(ios::skipws); while(!f1.eof()) { f1>>a; if(f1.eof()) b=' '; else f1>>b; f2<<b<<a; } f1.close(); f2.close(); }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; char a[20]; cin>>a; f1.open("d:\\aaa.txt",ios::app); f1<<a<<endl; }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; char Name[20]; char Fam[30]; char Tell[20]; cout<<"Enter Name = ";cin>>Name; cout<<"Enter Fam = ";cin>>Fam; cout<<"Enter Tell = ";cin>>Tell; f1.open("d:\\aaa.txt",ios::app); f1<< Name<<" "<<Fam<<" "<<Tell <<endl; cout<<"Saved"<<endl; }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> main() { fstream f1; int n; char Name[20]; char Fam[30]; char Tell[20]; cout<<" Name "<<'\t'<<" Fam "<<'\t'<<" Tell "<<endl; cout<<"-------"<<'\t'<<"-------"<<'\t'<<"-------"<<endl; f1.open("d:\\aaa.txt",ios::in); while(!f1.eof()) { f1>>Name>>Fam>>Tell; cout<<Name<<'\t'<<Fam<<'\t'<<Tell<<endl; } }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> #include<string.h> main() { fstream f1; char Name[20]; char Fam[30]; char Tell[20]; char SearchFam[30]; cout<<"Enter fam for search = "; cin>>SearchFam; cout<<endl; cout<<" Name "<<'\t'<<" Fam "<<'\t'<<" Tell "<<endl; cout<<"-------"<<'\t'<<"-------"<<'\t'<<"-------"<<endl; f1.open("d:\\aaa.txt",ios::in); while(!f1.eof()) { f1>>Name>>Fam>>Tell; if(!strcmp(SearchFam,Fam)) cout<<Name<<'\t'<<Fam<<'\t'<<Tell<<endl; } }
ذخیره و بازیابی در C++ #include<iostream.h> #include<fstream.h> #include<string.h> main() { fstream f1; int Menu; char Name[20]; char Fam[30]; char Tell[20]; char Search[30]; cout <<"Enter Choise:"<<endl; cout <<"1-Name"<<endl; cout <<"2-Fam"<<endl; cout <<"3-Tell"<<endl; cout <<"Select Menu:";cin >> Menu; cout<<"Enter Item for search = "; cin>>Search; f1.open("d:\\aaa.txt",ios::in); cout<<endl; cout<<" Name "<<'\t'<<" Fam "<<'\t'<<" Tell "<<endl; cout<<"-------"<<'\t'<<"-------"<<'\t'<<"-------"<<endl; while(!f1.eof()) { f1>>Name>>Fam>>Tell; if(Menu==1) if(!strcmp(Search,Name)) cout<<Name<<'\t'<<Fam<<'\t'<<Tell<<endl; if(Menu==2) if(!strcmp(Search,Fam)) cout<<Name<<'\t'<<Fam<<'\t'<<Tell<<endl; if(Menu==3) if(!strcmp(Search,Tell)) cout<<Name<<'\t'<<Fam<<'\t'<<Tell<<endl; } }