Download presentation
Presentation is loading. Please wait.
Published bySimon Edwards Modified over 9 years ago
1
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba
2
Starting Out with C++, 3 rd Edition 2 12.1 What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
3
Starting Out with C++, 3 rd Edition 3 12.2 File Names All files are assigned a name that is used for identification purposes by the operating system and the user.
4
Starting Out with C++, 3 rd Edition 4 Table 12-1
5
Starting Out with C++, 3 rd Edition 5 12.3 Focus on Software Engineering: The Process of Using a File Using a file in a program is a simple three- step process –The file must be opened. If the file does not yet exits, opening it means creating it. –Information is then saved to the file, read from the file, or both. –When the program is finished using the file, the file must be closed.
6
Starting Out with C++, 3 rd Edition 6 Figure 12-1
7
Starting Out with C++, 3 rd Edition 7 Figure 12-2
8
Starting Out with C++, 3 rd Edition 8 12.4 Setting Up a Program for File Input/Output Before file I/O can be performed, a C++ program must be set up properly. File access requires the inclusion of< # include
9
Starting Out with C++, 3 rd Edition Input &Output with files Input / Output with files C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files.
10
Starting Out with C++, 3 rd Edition 10 12.5 Opening a File Before data can be written to or read from a file, the file must be opened. ifstream inputFile; inputFile.open(“customer.dat”); // Path of file using : ifstream fin("D:\\data.txt ");
11
Starting Out with C++, 3 rd Edition 11 Program Output with Example Input Enter the name of a file you wish to open or create: mystuff.dat [Enter] The file mystuff.dat was opened.
12
Starting Out with C++, 3 rd Edition 12 Table 12-3
13
Starting Out with C++, 3 rd Edition 13 Table 12-4
14
Starting Out with C++, 3 rd Edition 14 Table 12-4 continued
15
Starting Out with C++, 3 rd Edition 15 Opening a File at Declaration fstream dataFile(“names.dat”, ios::in | ios::out);
16
Starting Out with C++, 3 rd Edition FLAGES IN FILE
17
Starting Out with C++, 3 rd Edition // writing in file & screen #include //divide by 7 #include using namespace std; int main() { ofstream fout("data.txt"); for(int i=4;i<=400;i++) { if(i%7==0) //fout << i <<",“ << d<<",“ << ' ' << i ; fout<<i<<" : divide by Two (7)."<<"\n"; fout.close(); cout<<i<<" : divide by Two (7)."<<"\n";} } 17
18
Starting Out with C++, 3 rd Edition #include // !x=x*(x-1)*(x-2)…. #include using namespace std; int main() { ofstream fout("factial.txt"); // Variable Declaration int counter, n, fact = 1; // Get Input Value cout<<"Enter the Number :"; cin>>n; //for Loop Block for (int counter = 1; counter <= n; counter++) { fact = fact * counter; } fout<<n<<" Factorial Value Is "<<fact; fout.close(); cout<<n<<" Factorial Value Is "<<fact; return 0; } 18
19
Starting Out with C++, 3 rd Edition 19 Program 12-2 // This program demonstrates the opening of a file at the // time the file stream object is declared. #include using namespace std; int main( ) { fstream dataFile("names.txt", ios::in | ios::out); cout << "The file names.dat was opened.\n"; //system("PAUSE"); }
20
Starting Out with C++, 3 rd Edition 20 Testing for Open Errors dataFile.open(“cust.txt”, ios::in); if (!dataFile) { cout << “Error opening file.\n”; }
21
Starting Out with C++, 3 rd Edition 21 Another way to Test for Open Errors dataFile.open(“cust.dat”, ios::in); if (dataFile.fail()) { cout << “Error opening file.\n”; }
22
Starting Out with C++, 3 rd Edition 22 12.6 Closing a File A file should be closed when a program is finished using it.
23
Starting Out with C++, 3 rd Edition // writing on a text file #include using namespace std; int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a new file created in cpp.\n"; myfile << "This is another line.\n"; myfile.close(); } else cout << "Unable to open file"; system("pause"); return 0; }
24
Starting Out with C++, 3 rd Edition // reading a text file #include using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( getline (myfile,line) ) { cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; system("pause"); return 0; }
25
Starting Out with C++, 3 rd Edition file operations in C++ #include using namespace std; int main () { ofstream myfile; myfile.open ("example22.txt"); myfile << "Writing this to a file.\n"; myfile<<"Hope fine”" << endl; myfile<<"this frist file ”" << endl; myfile.close(); return 0; } Slide 6- 25
26
Starting Out with C++, 3 rd Edition #include using namespace std; int main() { char FirstName[30], LastName[30]; int Age; char FileName[20]; cout << "Enter First Name: "; cin >> FirstName; cout << "Enter Last Name: "; cin >> LastName; cout << "Enter Age: "; cin >> Age; cout << "\nEnter the name of the file you want to create:..txt "; cin >> FileName; ofstream Students(FileName, ios::out); Students << FirstName << "\n" << LastName << "\n" << Age; cout << "\n\n"; return 0; } Slide 6- 26
27
Starting Out with C++, 3 rd Edition // basic file operations #include using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; } Slide 6- 27
28
Starting Out with C++, 3 rd Edition Write to file #include using namespace std; int main() { ofstream fout; fout.open("FFFF.txt"); fout << "Asalamualikom hope fine.\n" << "WELCOME TO GE 211 FILE SECTION PROGRAM\n" << "HOPE TO DO WELL IN SECOND MEDTERM EXAM\n"; fout.close(); }
29
Starting Out with C++, 3 rd Edition //Write the odd number in file #include using namespace std; int main() { ofstream fout; fout.open("E:\\firsteee.txt"); int i,j; for(i=2;i<30;i+=2) { fout<<i<<"\t"; cout<<i<<"\t";} fout.close(); }
30
Starting Out with C++, 3 rd Edition // print in file Even number 1:30 #include using namespace std; int main() { ofstream fout; fout.open("D:\\firstExa.txt"); int i,j; for(i=1;i<30;i+=2) fout<<i<<"\t"; fout.close(); }
31
Starting Out with C++, 3 rd Edition #include using namespace std; int main() {ofstream fout; fout.open("D:\\ffffff.txt"); float i,j,m,sum; j=3; sum=0; for(i=1;i<=100;i++){ m=(i*i)/(j*j); j=j+2; sum=sum+m;} cout<<"seque="<<sum<<endl; fout<<"seque="<<sum<<endl; fout.close(); }
32
Starting Out with C++, 3 rd Edition //write equation for this series #include using namespace std; int main() { int i,j,m,n,sum; sum=0; cin>>n; for(i=1 ;i<=n ;i++) { m=i*i; sum=sum+m; } cout<< "seque="<<sum<< endl; }
33
Starting Out with C++, 3 rd Edition #include // using both file and screen #include using namespace std; int main() { ofstream fout; fout.open("xxxx.txt"); int i,j,m,n,sum; sum=0; cout<<"equation for this series ="<<endl; cin>>n; for(i=1 ;i<=n ;i++) { m=i*i; sum=sum+m; } cout<< "seque="<<sum<< endl; fout<< "seque= written in file is="<<sum<< endl; fout.close(); } 33
34
Starting Out with C++, 3 rd Edition #include using namespace std; int main() { float i,m,n,b,a,y,x,s; y=0; b=2; cout<<"enter the last power of\n"; cin>>n; cout<<"enter the volue of(x)\n"; cin>>s; for(i=1;i<=n;i++){ x=pow(s,i); a=pow(b,i); m=x/a; y=y+m; } cout<<"y= "<<y; }
35
Starting Out with C++, 3 rd Edition #include using namespace std; main() {ofstream fout; fout.open("D:\\UUUU.txt"); float i,m,n,b,a,y,x,s; y=0; b=2; cout<<"enter the last power of\n"; cin>>n; cout<<"enter the volue of(x)\n"; cin>>s; for(i=1;i<=n;i++){ x=pow(s,i); a=pow(b,i); m=x/a; y=y+m; } cout<<"y= "<<y; fout<<"y= "<<y; fout.close(); }
36
Starting Out with C++, 3 rd Edition //Fenonci seris #include using namespace std; main() {ofstream fout; fout.open("D:\\finoncy.txt"); int i,s,b,d ; s=0; d=1; for(i=0;i<20;i++) { b=s; s=d; d=s+b; fout<<d<<"\t"; } fout.close(); }
37
Starting Out with C++, 3 rd Edition //write to file #include using namespace std; int main() { ofstream fout; fout.open("D:\\Seris.txt"); int i,j,m,n,sum; sum=0; cin>>n; for(i=1 ;i<=n ;i++) { m=i*i; sum=sum+m; } fout<< "seque="<<sum<< endl; fout.close(); }
38
Starting Out with C++, 3 rd Edition Read from file #include using namespace std; int main() { char array [80]; ifstream fin; fin.open("firstExa.txt"); while(!fin.eof()) {fin.getline(array,80); cout<<array<<endl;} fin.close(); }
39
Starting Out with C++, 3 rd Edition Write in binary file ofstream fout ; fout.open("file path",iostream family fout.write((char*)& data,sizeo(data);
40
Starting Out with C++, 3 rd Edition Binary form #include main() { int Array[80],i; for(i=0;i<10;i++) cin>> Array[i]; ofstream fout; fout.open("D:\\ahmed.bin",ios::binary); fout.write((char *) & Array, sizeof(Array)); fout.close(); system("pause"); }
41
Starting Out with C++, 3 rd Edition // writing on a text file #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; } Slide 6- 41
42
Starting Out with C++, 3 rd Edition // reading a text file #include using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; } Slide 6- 42
43
Starting Out with C++, 3 rd Edition Using fout Slide 6- 43 #include using namespace std; int main (int aaa,char *avg[]) { ofstream fout ("my_out.txt"); fout<<" Assalamualikom Hope fine" << endl; fout<<"Please tray good in second exam this frist file " << endl; return 0;}
44
Starting Out with C++, 3 rd Edition Write in file #include using namespace std; int main( ) { //cout<<"Exam1\n"; // ifstream ;// read // ofstream ;// write // fstream;//read &write ofstream myfile; myfile.open("kkkk.txt"); myfile<< "Exam1 is ok\n"; myfile<< "please tray good in second exam"; myfile.close(); return 0; }
45
Starting Out with C++, 3 rd Edition Using app #include using namespace std; int main( ) { /*cout<<"Exam1\n"; ifstream ;// read ofstream ;// write fstream;//read &write*/ fstream myfile; myfile.open("kkkk.txt",ios ::out|ios::app); //myfile<< "Exam1 is ok\n"; myfile<< "please tray good in second exam****************\n"; //cout<<myfile.rdbuf(); myfile.close(); return 0; }
46
Starting Out with C++, 3 rd Edition Read from file #include using namespace std; int main( ) { /*cout<<"Exam1\n"; ifstream ;// read ofstream ;// write fstream;//read &write*/ ifstream myfile; myfile.open("kkkk.txt"); //myfile<< "Exam1 is ok\n"; //myfile<< "please tray good in second exam"; cout<<myfile.rdbuf(); myfile.close(); system("pause"); return 0; }
47
Starting Out with C++, 3 rd Edition Input &Output with files Input / Output with files C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files.
48
Starting Out with C++, 3 rd Edition // basic file operations #include using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; }
49
Starting Out with C++, 3 rd Edition classdefault mode parameter ofstreamios::out ifstreamios::in fstreamios::in | ios::out ofstream myfile; myfile.open ("example.bin", ios::out | ios::app | ios::binary);
50
Starting Out with C++, 3 rd Edition Basic file operations // basic file operations #include //#include using namespace std; int main () { ofstream myfile; myfile.open ("XXX.txt"); myfile << "**Writing this to a file.\n"; myfile << "****Writing this to a file.\n"; myfile << "*********Writing this to a file.\n"; myfile.close(); system("PAUSE"); return 0; }
51
Starting Out with C++, 3 rd Edition Basic file operations #include using namespace std; int main (int argc, char *arvg[ ]) { ofstream fout("XXX.txt"); fout << "**Writing this to a file.\n"; fout << "****Writing this to a file.\n"; fout << "*********Writing this to a file.\n"; system("PAUSE"); return EXIT_SUCCESS; }
52
Starting Out with C++, 3 rd Edition FILE IN C++ ios::inOpen for input operations. ios::outOpen for output operations. ios::binaryOpen in binary mode. ios::ate Set the initial position at the end of the file. If this flag is not set to any value, the initial position is the beginning of the file. ios::app All output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output-only operations. ios::trunc If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one.
53
Starting Out with C++, 3 rd Edition // writing on a text file #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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.