Ahmad Yoosofan
University of Kashan
1 #include<fstream> 2 #include<iostream> 3 using namespace std; 4 int main(){ 5 int i; 6 ofstream f1("2.out.txt"); 7 for(i = 0; i < 20; i++) 8 f1 << i << endl; 9 f1 << "salam" << endl; 10 f1.close(); 11 }
1 #include<iostream> 2 #include<fstream> 3 using namespace std; 4 int main(){ 5 ifstream f1("input.integers.txt"); 6 if(!f1){ 7 cout << "The file could not be opened " << endl; 8 throw 1; 9 } 10 int i; //cin>>i; 11 f1 >> i; 12 cout << i << endl; 13 f1 >> i; 14 cout << i << endl; 15 f1.close(); 16 }
1 #include<iostream> 2 #include<fstream> 3 using namespace std; 4 int main(){ 5 ofstream f1("2.out.txt"); 6 if(!f1){ 7 cout << "The file could not be opened " << endl; 8 throw 1; 9 } 10 int i = 819; //cin>>i; 11 f1 << i << " "; 12 cout << i << endl; 13 i = 4765; 14 f1 << i; 15 cout << i << endl; 16 f1.close(); 17 }
1 #include<iostream> 2 #include<fstream> 3 using namespace std; 4 int main(){ 5 ifstream f1("input.integers.txt"); 6 //ifstream f1("folder2/folder3/file1.txt"); 7 //ifstream f1("../../folder2/folder3/file1.txt"); 8 /* 9 Relative addressing: 10 آدرس دهی نسبی در این جا یعنی از پوشهی کنونی یکی بالاتر برو 11 سپس در آن پوشه به داخل پوشهی folder2 برو سپس از آنجا به پوشهی folder3 برو 12 در آن پوشهی آخری پروندهای به نام file1.txt باید وجود داشته باشد 13 ../folder2/folder3/file1.txt 14 Absolute addressing 15 آدرس دهی مطلق، مسیر را کامل از / مشخص میکند و دقیق مسیر مشخص میشود. 16 /home/ahmad/folder2/file1.txt 17 یا در ویندوز به صورت 18 D:\\folder2\\file1.txt 19 توضیح بیشتر دربارهی آن را در کلاس بپرسید تا توضیح دهم 20 */ 21 if(!f1){ 22 cout<<"The file could not be opened "<<endl; 23 throw 1; 24 } 25 int i; //cin>>i; 26 f1 >> i; 27 cout << i << endl; 28 f1 >> i; 29 cout << i << endl; 30 f1.close(); 31 }
1 #include<fstream> 2 #include<iostream> 3 #include<iomanip> 4 using namespace std; 5 const int MAX_STR = 200; 6 int main(){ 7 char st[MAX_STR]; 8 string st2; 9 ifstream f1("input.students.txt"); 10 if(!f1){ 11 cout << "couldn't open" << endl; 12 throw "Cannot open file"; 13 } 14 f1.read(st, MAX_STR - 1); // 199 charactes + \0 15 while(!f1.eof()){ 16 //~ while(getline(f1, st2)){ 17 //~ st[9]= 0; 18 cout << st << endl; 19 //~ cout << st2 << endl; 20 //~ f1.seekg(9, ios::cur); 21 f1.read(st, MAX_STR - 1); 22 } 23 cout << st << endl; 24 f1.close(); 25 cout << std::hex << int('9') << endl; 26 //~ cout << std::hex << int('7') << endl; 27 //~ cout << std::hex << int('4') << endl; 28 //~ cout << std::hex << int('3') << endl; 29 }
1 #include<fstream> 2 #include<iostream> 3 using namespace std; 4 const int MAX_STR = 200; 5 int main(){ 6 char st[MAX_STR]; 7 ifstream f1; 8 f1.open("input.students.txt"); 9 if(!f1) 10 cout << "couldn't open" << endl; 11 else { 12 f1.getline(st, MAX_STR - 1); 13 while(! f1.eof()){ 14 cout << st << endl; 15 f1.getline(st, MAX_STR - 1); 16 } 17 f1.close(); 18 } 19 } 20 /* Rererence for more study 21 * http://www.cplusplus.com/reference/fstream/fstream/open/ 22 */
1 #include<fstream> 2 #include<iostream> 3 using namespace std; 4 const int MAX_STR = 200; 5 int main(){ 6 char st[MAX_STR]; 7 ifstream f1; 8 f1.open("input.students.txt"); 9 if(!f1.is_open() ) 10 cout << "couldn't open" << endl; 11 else { 12 f1.getline(st, MAX_STR - 1); 13 while(! f1.eof()){ 14 cout << st << endl; 15 f1.getline(st, MAX_STR - 1); 16 } 17 f1.close(); 18 } 19 return 0; 20 } 21 /* Rererence for more study 22 * http://www.cplusplus.com/reference/fstream/fstream/is_open/ 23 */
1 #include<fstream> 2 #include<iostream> 3 using namespace std; 4 const int MAX_STR = 200; 5 int main(){ 6 char st[MAX_STR]; 7 ifstream f1("input.students.txt"); 8 if(! f1){ 9 cout << "couldn't open" << endl; 10 throw 1; 11 } 12 //cout<<f1.getline(st,200).gcount()<<endl; 13 while(f1.getline(st, MAX_STR - 1).gcount() > 0 ) 14 cout << st << endl; 15 f1.close(); 16 }
1 #include <fstream> 2 #include <iostream> //#include <cstdlib> 3 using namespace std; 4 int main(){ 5 string st= "ali"; 6 ofstream f1("2.out.txt"); 7 fstream f2("3.out.txt", ios::out); //ofstream 8 if(!f1){ 9 cout << "Can not open f1" << endl; 10 throw 1; 11 } 12 if(!f2){ 13 cout << "Can not open f2" << endl; 14 f1.close(); 15 throw 2; 16 } 17 f1 << st << ' ' << "Hamed" << endl; 18 f2 << "Sadegh" << " Salehi " << endl; 19 f2.close(); 20 f1.close(); 21 fstream f3("2.out.txt", ios::in); 22 if(!f3){ 23 cout << "Can not open f3" << endl; 24 throw 0; 25 } 26 f3 >> st; 27 cout << st << endl; 28 f3.close(); 29 ifstream f4("3.out.txt"); 30 char cst[50]; 31 f4.getline(cst,50); 32 cout << cst << endl; 33 f4.close(); 34 }
1 #include <fstream> 2 #include<iostream> 3 using namespace std; 4 int main(){ 5 string st = "ali"; 6 ofstream f1("2.out.txt"); 7 fstream f2("3.out.txt", ios::out); //ofstream 8 if(!f1) 9 cout << "Can not open f1" << endl; 10 if(!f2) 11 cout << "Can not open f2" << endl; 12 f1 << st << endl << "Hamed" << endl; 13 f2 << "Sadegh" << " Salehi " << endl; 14 f2 << "ali" << " Seifi " << endl; 15 f2 << "Sahar" << " Safi " << endl; 16 f2.close(); 17 f1.close(); 18 fstream f3("2.out.txt",ios::in); 19 if(!f3){ 20 cout << "Can not open f3" << endl; 21 return 0; 22 } 23 f3 >> st; 24 cout << st << endl; 25 f3.close(); 26 ifstream f4("3.out.txt"); //f4>>st; cout<<st<<endl; 27 char cst[50]; //while(f4.getline(cst,50).gcount()>0) 28 while(f4.getline(cst, 50)) 29 cout<< cst << endl; 30 f4.close(); 31 }
1 #include<fstream> 2 #include<iostream> 3 using namespace std; 4 struct student{ 5 int std_no; 6 char name[100]; 7 double avg; 8 char course[100]; 9 int m, n; 10 void readFromFile(ifstream& f1){ 11 f1 >> std_no; 12 f1 >> name; 13 f1 >> avg; 14 f1 >> course; 15 f1 >> m; 16 f1 >> n; 17 } 18 void print(void){ 19 cout << std_no <<'\t'; 20 cout << name << '\t'; 21 cout << avg << '\t'; 22 cout << course << '\t'; 23 cout << m << '\t'; 24 cout << n << endl; 25 } 26 }; 27 int main(){ 28 int i; 29 double x; 30 char st[100]; 31 student m1; 32 ifstream f1("input.students.txt"); // ofstream , ifstream, fstream 33 m1.readFromFile(f1); 34 while(!f1.eof()){ 35 m1.print(); 36 m1.readFromFile(f1); 37 } 38 //while(f1>>i) cout<<i<<'\t';cout<<endl; 39 //f1.close(); f1.open("1.inp.txt"); 40 //f1.seekg(0,ios::beg); 41 //while(!f1.eof()){ cout<<i<<'\t';f1>>i;} cout<<endl; 42 f1.close(); 43 } 44 /* 45 void read2(ifstream&f1){ 46 constexpr auto max_string=1000; 47 constexpr auto max_stringMinus1=1000-1; 48 char st[max_string]; 49 f1.getline(st,max_stringMinus1); 50 sscanf(st,"%d%s%lf%s%d%d",&std_no,name, &avg, course, &m, &n); 51 } 52 */
1 #include<iostream> 2 #include<fstream> 3 using namespace std; 4 class myIntArray{ 5 static const int N = 2000; 6 int a[N]; 7 ifstream f1; 8 int n; 9 public: 10 myIntArray(const char* fileName = "input.integers.txt"){ 11 int i; 12 f1.open(fileName); 13 if(!f1){ 14 cout << "file "; 15 throw "cannot open file"; // throw 16 } 17 for(i = 0 ; f1 >> a[i] && i < N; i++) 18 ; 19 n = i; 20 f1.close(); 21 } 22 void sort(void){ 23 int i, j; 24 for(i = 0; i < n-1; i++) 25 for(j = i+1; j < n; j++) 26 if(a[i] > a[j]){ 27 int temp = a[i]; 28 a[i] = a[j]; 29 a[j]=temp; 30 } 31 } 32 int search(int key){ 33 for(auto i = 0; i < n; i++) 34 if(a[i] == key) 35 return i; 36 return -1; 37 } 38 void print(void){ 39 for(auto i = 0; i < n; i++) 40 cout << a[i] << endl; 41 } 42 }; 43 int main(){ 44 myIntArray m1("input.integers.txt"); 45 m1.sort(); 46 cout << m1.search(12) << endl; 47 m1.print(); 48 }
1 #include<iostream> 2 #include<fstream> 3 using namespace std; 4 class myIntArray{ 5 const static int N = 2000; 6 int a[N]; 7 ifstream f1; 8 int n; 9 public: 10 myIntArray(const char* fileName = "input.integers.txt"){ 11 int i; 12 f1.open(fileName); 13 if(!f1){ 14 cout<<"file "; 15 throw "cannot open file"; 16 } 17 for(i=0; f1 >> a[i] && i<N; i++) 18 ; 19 n = i; 20 f1.close(); 21 } 22 void sort(void){ 23 int i,j, temp; 24 for(i=0;i<n-1;i++) 25 for(j=i+1;j<n;j++) 26 if(a[i]>a[j]){ 27 temp = a[i]; 28 a[i] = a[j]; 29 a[j] = temp; 30 } 31 } 32 int search(int key){ 33 for(int i=0; i < n; i++) 34 if(a[i] == key) 35 return i; 36 return -1; 37 } 38 void print(void){ 39 for(int i=0; i<n; i++) 40 cout << a[i] << endl; 41 } 42 }; 43 int main(){ 44 myIntArray m1("input.integers.txt"); 45 m1.sort(); 46 cout << m1.search(12) << endl; 47 m1.print(); 48 return 0; 49 }
1 #include<iostream> 2 #include<fstream> 3 using namespace std; 4 class myIntArray{ 5 int* a; 6 fstream f1; 7 int n; 8 public: 9 myIntArray(const char* fileName="input.integers.txt"){ 10 int i; 11 f1.open(fileName); 12 if(!f1){ 13 cout << "file "; 14 throw "cannot open file"; 15 } 16 a = new int[2000]; 17 int k = 2000; 18 for(i=0; f1 >> a[i]; i++) 19 if(i > k-2){ 20 int* b = new int[k *= 2]; 21 for(int j=0; j < i; j++) 22 b[j] = a[j]; 23 delete[] a; 24 a = b; 25 } 26 n = i; 27 f1.close(); 28 } 29 ~myIntArray(){delete[] a;} 30 void sort(void){ 31 int i, j, temp; 32 for(i=0; i<n-1; i++) 33 for(j=i+1; j<n; j++) 34 if(a[i] > a[j]){ 35 temp = a[i]; 36 a[i] = a[j]; 37 a[j] = temp; 38 } 39 } 40 int search(int key){ 41 for(int i=0; i < n; i++) 42 if(a[i] == key) 43 return i; 44 return -1; 45 } 46 void print(void){ 47 for(int i=0; i<n; i++) 48 cout << a[i] << endl; 49 } 50 }; 51 int main(){ 52 myIntArray m1("input.integers.txt"); 53 m1.sort(); 54 cout << m1.search(12) << endl; 55 m1.print(); 56 return 0; 57 }
1 // https://www.systutorials.com/how-to-process-a-file-line-by-line-in-c/ 2 #include <iostream> 3 #include <fstream> 4 #include <string> 5 int main (){ 6 std::ifstream file("input.txt"); 7 std::string str; 8 while (std::getline(file, str)) 9 std::cout << str << "\n"; 10 }
1 // https://codereview.stackexchange.com/a/38954 2 // https://codereview.stackexchange.com/questions/38879/parsing-text-file-in-c 3 // https://www.codespeedy.com/cpp-program-to-create-a-text-file-open-and-read-a-particular-line/ 4 // http://www.cplusplus.com/forum/beginner/11304/ 5 // https://www.tutorialspoint.com/read-file-line-by-line-using-cplusplus 6 // https://www.geeksforgeeks.org/encrypt-and-decrypt-text-file-using-cpp/ 7 // https://coderedirect.com/questions/119182/how-to-read-a-file-line-by-line-or-a-whole-text-file-at-once 8 // https://www.codegrepper.com/code-examples/cpp/getline+from+file+c%2B%2B 9 // https://www.educba.com/c-plus-plus-getline/ 10 // https://www.codegrepper.com/code-examples/cpp/getline+from+file+c%2B%2B 11 // https://stackoverflow.com/questions/43573658/c-how-to-read-a-line-of-text-from-file-to-char-array 12 // https://www.geeksforgeeks.org/getline-string-c/ 13 14 struct Person 15 { 16 std::string name; 17 std::string age; 18 std::string salary; 19 std::string hoursWorked; 20 std::string randomText; 21 22 friend std::istream& operator>>(std::istream& str, Person& data) 23 { 24 std::string line; 25 Person tmp; 26 if (std::getline(str,line)) 27 { 28 std::stringstream iss(line); 29 if ( std::getline(iss, tmp.name, ':') && 30 std::getline(iss, tmp.age, '-') && 31 std::getline(iss, tmp.salary, ',') && 32 std::getline(iss, tmp.hoursWorked, '[') && 33 std::getline(iss, tmp.randomText, ']')) 34 { 35 /* OK: All read operations worked */ 36 data.swap(tmp); // C++03 as this answer was written a long time ago. 37 } 38 else 39 { 40 // One operation failed. 41 // So set the state on the main stream 42 // to indicate failure. 43 str.setstate(std::ios::failbit); 44 } 45 } 46 return str; 47 } 48 void swap(Person& other) throws() // C++03 as this answer was written a long time ago. 49 { 50 swap(name, other.name); 51 swap(age, other.age); 52 swap(salary, other.salary); 53 swap(hoursWorked, other.hoursWorked); 54 swap(randomText, other.randomText) 55 } 56 };
1 #include <fstream> 2 #include<iostream> 3 using namespace std; 4 int main(){ 5 int i = 12; 6 ofstream f1("a1.txt", ios::binary); 7 if(!f1) 8 throw "Cannot Open File"; 9 f1.write((char*) &i, sizeof(int)); 10 const char* st1="ABC"; 11 const char st2[]{'A', 'B', 'C', '\0'}; 12 const char st3[]{65, 66, 67, 0}; 13 const char st4[]{0x41, 0x42, 0x43, 0x0}; 14 f1.write((char*) st1, 4 * sizeof(char)); 15 f1.write((char*) st2, 4 * sizeof(char)); 16 f1.write((char*) st3, 4 * sizeof(char)); 17 f1.write((char*) st4, 4 * sizeof(char)); 18 f1.write((char*) &(++i), sizeof(int)); 19 // f1.write((char *) &(i++),sizeof(int)); 20 // f1.write((char *) &(i+1),sizeof(int)); 21 f1.write((char*) &(++i), sizeof(int)); 22 f1.write((char*) &(i += 234234), sizeof(int)); 23 f1.close(); 24 }
1 #include <fstream> 2 #include<iostream> 3 using namespace std; 4 int main(){ 5 int i = 12, j; 6 ofstream f1("a1.txt", ios::binary); 7 if(!f1) 8 throw "Cannot Open File"; 9 f1.write((char*) &i, sizeof(int)); 10 const char* st1="ABC"; 11 const char st2[]{'A', 'B', 'C', '\0'}; 12 const char st3[]{65, 66, 67, 0}; 13 const char st4[]{0x41, 0x42, 0x43, 0x0}; 14 f1.write((char*) st1, 4 * sizeof(char)); 15 f1.write((char*) st2, 4 * sizeof(char)); 16 f1.write((char*) st3, 4 * sizeof(char)); 17 f1.write((char*) st4, 4 * sizeof(char)); 18 f1.write((char*) &(++i), sizeof(int)); 19 // f1.write((char *) &(i++),sizeof(int)); 20 // f1.write((char *) &(i+1),sizeof(int)); 21 f1.write((char*) &(++i), sizeof(int)); 22 f1.write((char*) &(i += 234234), sizeof(int)); 23 f1.close(); 24 fstream f3("a1.txt", ios::in | ios::binary); 25 if(!f3) 26 throw "Cannot Open File"; 27 f3.read((char*) &j, sizeof(int)); 28 char ast[20]; 29 f3.read(ast, 4 * sizeof(char)); 30 f3.read(ast+4, 4 * sizeof(char)); 31 f3.read(ast+8, 4 * sizeof(char)); 32 f3.read(ast+12, 4 * sizeof(char)); 33 cout << j << endl; 34 cout << ast << endl; 35 cout << ast + 4 << endl; 36 cout << ast + 8 << endl; 37 cout << ast + 12 << endl; 38 cout << "number of read character: " 39 << f3.read((char*) &j, sizeof(int)).gcount() << endl; 40 while(f3.read((char*) &j, sizeof(int))) 41 cout << j << endl; 42 f3.close(); 43 }
1 #include <fstream> 2 #include<iostream> 3 using namespace std; 4 void write2file(const char* fname); 5 void readFromFile(const char* fname); 6 int main(){ 7 write2file("a1.bin"); 8 readFromFile("a1.bin"); 9 } 10 void write2file(const char* fname = "a1.bin"){ 11 ofstream f1(fname, ios::binary); 12 // fstream f1(fname, ios::out | ios::binary); 13 if(!f1) throw "Cannot Open File"; 14 int i = 6, j = 1; 15 f1.write((char*) &i, sizeof(int)); 16 for(; i > 0; i--) 17 f1.write((char*) &(j += 3), sizeof(int)); 18 f1.close(); 19 } 20 void readFromFile(const char* fname = "a1.bin"){ 21 //fstream f3(fname, ios::in | ios::binary); 22 ifstream f3(fname, ios::binary); 23 if(!f3) throw "Cannot Open File"; 24 int n, j; 25 f3.read((char*) &n, sizeof(int)); 26 for(; n > 0; n--){ 27 f3.read((char*) &j, sizeof(int)); 28 cout << j << endl; 29 } 30 f3.close(); 31 }
1 #include <fstream> 2 #include <iostream> 3 using namespace std; 4 class myArray{ 5 fstream f1; 6 public: 7 myArray(const char* fname = "temp1.in"){ 8 f1.open(fname, ios::in | ios::out | ios::binary); 9 if(!f1){ 10 f1.open(fname, ios::out | ios::binary); 11 if(!f1){ 12 cout << "Can not open file" << endl; 13 throw "Cannot Open File"; 14 }else{ 15 f1.close(); 16 f1.open(fname, ios::in | ios::out | ios::binary); 17 } 18 } 19 } 20 ~myArray(){f1.close();} 21 int read(const int index){ 22 int j; 23 f1.seekg(index * sizeof(int), ios::beg); // ios::end ios::cur 24 f1.read((char*) &j, sizeof(int)); 25 return j; 26 } 27 void write(int data, int index){ 28 f1.seekp(index * sizeof(int), ios::beg); 29 f1.write((char*) &data, sizeof(int)); 30 } 31 int readNext(void){ 32 int j; 33 f1.read((char*) &j, sizeof(int)); 34 return j; 35 } 36 void writeNext(int j) 37 {f1.write((char*) &j, sizeof(int));} 38 void rewind(void){ 39 f1.seekg(0, ios::beg); 40 f1.seekp(0, ios::beg); 41 } 42 }; 43 int main(){ 44 myArray myi; 45 myi.writeNext(12); 46 myi.writeNext(14); 47 myi.rewind(); 48 cout << myi.readNext() << endl; 49 cout << myi.readNext() << endl; 50 cout << myi.read(0) << endl; 51 cout << myi.read(1) << endl; 52 myi.write(13, 10); 53 cout<<myi.read(25)<<endl<<endl; 54 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 using namespace std; 5 template<typename T>class myArray{ 6 fstream f1; 7 public: 8 myArray(const char* fname = "temp1.in"){ 9 f1.open(fname, ios::in | ios::out | ios::binary); 10 if(!f1){ 11 f1.open(fname, ios::out | ios::binary); 12 if(!f1){ 13 cout << "Can not open file" << endl; 14 exit(0); 15 }else{ 16 f1.close(); 17 f1.open(fname, ios::in |ios::out |ios::binary); 18 } 19 } 20 } 21 ~myArray(){ f1.close();} 22 T read(int index){ T j; 23 f1.seekg(index * sizeof(T), ios::beg); 24 f1.read((char*) &j, sizeof(T)); 25 return j; 26 } 27 void write(const T j, const int index){ 28 f1.seekp(index * sizeof(T), ios::beg); 29 f1.write((char*) &j , sizeof(T)); 30 } 31 T readNext(void){ T j; 32 f1.read((char*) &j , sizeof(T)); 33 return j; 34 } 35 void writeNext(T j){f1.write((char*) &j, sizeof(T));} 36 void rewind(void){ 37 f1.seekg(0, ios::beg); 38 f1.seekp(0, ios::beg); 39 } 40 }; 41 int main(){ 42 myArray<int> myi("temp4.in"); 43 myi.writeNext(16); myi.writeNext(19); myi.rewind(); 44 cout << myi.readNext() << endl; 45 cout << myi.readNext() << endl; 46 cout << myi.read(0) << endl; 47 cout << myi.read(1) << endl; 48 myArray<double> my2; my2.writeNext(4.23); 49 my2.writeNext(5.34); my2.writeNext(67.43); my2.rewind(); 50 cout << my2.readNext() << endl << my2.readNext() << endl; 51 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 #include<cstring> 5 using namespace std; 6 template<typename T> class myArray{ 7 fstream f1; 8 public: 9 myArray(const char* fname = "temp1.in"){ 10 f1.open(fname, ios::in | ios::out | ios::binary); 11 if(!f1){ 12 f1.open(fname, ios::out | ios::binary); 13 if(!f1){ 14 cout << "Can not open file" << endl; 15 throw "Cannot Open File"; 16 }else{ 17 f1.close(); 18 f1.open(fname, ios::in | ios::out | ios::binary); 19 } 20 } 21 } 22 ~myArray(){ 23 f1.close(); 24 } 25 T read(const int index){ 26 T data; 27 f1.seekg(index * sizeof(T), ios::beg); 28 // ios::end ios::cur 29 f1.read((char*) &data, sizeof(T)); 30 return data; 31 } 32 void write(const T& data, const int index){ 33 f1.seekp(index * sizeof(T), ios::beg); 34 f1.write((char*) &data, sizeof(T)); 35 } 36 T readNext(void){ 37 T data; 38 f1.read((char*) &data, sizeof(T)); 39 return data; 40 } 41 void writeNext(T data){ 42 f1.write((char*) &data, sizeof(T)); 43 } 44 void rewind(void){ 45 f1.seekg(0, ios::beg); 46 f1.seekp(0, ios::beg); 47 } 48 }; 49 struct student{ 50 char name[20]; 51 char stdno[14]; 52 double avg; 53 void print(void){ 54 cout << "name:" << name << endl << "stdno:" << stdno 55 << endl << "avg:" << avg << endl; 56 } 57 }; 58 int main(){ 59 myArray<student> myi("temp6.in"); 60 student st1{"Ali", "933424", 15.1}; 61 myi.writeNext(st1); 62 strcpy(st1.name, "Reza"); 63 strcpy(st1.stdno, "923434"); 64 st1.avg = 14.2; 65 myi.writeNext(st1); 66 myi.rewind(); 67 myi.readNext().print(); 68 myi.readNext().print(); 69 myi.read(0).print(); 70 myi.read(1).print(); 71 return 0; 72 }
1 // Error 2 #include <fstream> 3 #include<iostream> 4 #include <cstdlib> 5 #include<cstring> 6 using namespace std; 7 template<typename T> class myArray{ 8 fstream f1; 9 public: 10 myArray(const char* fname = "temp1.in"){ 11 f1.open(fname, ios::in | ios::out | ios::binary); 12 if(!f1){ 13 f1.open(fname, ios::out | ios::binary); 14 if(!f1){ 15 cout << "Can not open file" << endl; 16 throw "Cannot Open File"; 17 }else{ 18 f1.close(); 19 f1.open(fname, ios::in | ios::out | ios::binary); 20 } 21 } 22 } 23 ~myArray(){ 24 f1.close(); 25 } 26 T read(int index){ 27 T data; 28 f1.seekg(index * sizeof(T), ios::beg); // ios::end ios::cur 29 f1.read((char*) &data, sizeof(T)); 30 return data; 31 } 32 void write(const T data, int index){ 33 f1.seekp(index * sizeof(T), ios::beg); 34 f1.write((char*) &data, sizeof(T)); 35 } 36 T readNext(void){ 37 T data; 38 f1.read((char*) &data , sizeof(T)); 39 return data; 40 } 41 void writeNext(T data){ 42 f1.write((char*) &data, sizeof(T)); 43 } 44 void rewind(void){ 45 f1.seekg(0, ios::beg); 46 f1.seekp(0, ios::beg); 47 } 48 }; 49 struct student{ 50 char name[20]; 51 char stdno[14]; 52 double avg; 53 friend ostream & operator<<(ostream & o1, student m1){ 54 o1 << "( " << m1.name << " , " << m1.stdno 55 << " , " << m1.avg << " ) "; 56 return o1; 57 } 58 char& operator[](int& index); 59 }; 60 int mystrlen(char* s){ 61 int i; 62 for(i = 0; s[i]; i++); 63 return i; 64 } 65 char& student::operator[](int& index){ 66 if(index < mystrlen(stdno)) 67 return stdno[index]; 68 return stdno[0]; 69 } 70 int main(){ 71 myArray<student> myi("temp6.in"); 72 student st1 {"Ali", "933424", 15.1}; 73 cout << st1[1] << endl; 74 st1[1] = '2'; 75 myi.writeNext(st1); 76 strcpy(st1.name, "Reza"); 77 strcpy(st1.stdno, "923434"); 78 st1.avg = 14.2; 79 myi.writeNext(st1); 80 myi.rewind(); 81 cout << myi.readNext() << endl; 82 //operator<<(cout,myi.readNext()); 83 //student *ps= & myi.readNext(); Error 84 cout << myi.readNext() << endl; 85 cout << myi.read(0) << endl; 86 cout << myi.read(1) << endl; 87 //myi.write(54,10); 88 //cout<<myi.read(25)<<endl<<endl; 89 return 0; 90 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 #include<cstring> 5 using namespace std; 6 template<typename T> class myArray{ 7 fstream f1; 8 public: 9 myArray(const string fname = "temp1.in"){ 10 f1.open(fname, ios::in | ios::out | ios::binary); 11 if(!f1){ 12 f1.open(fname, ios::out | ios::binary); 13 if(!f1) 14 throw fname + "Can not open file" ; 15 else{ 16 f1.close(); 17 f1.open(fname, ios::in | ios::out | ios::binary); 18 } 19 } 20 } 21 ~myArray(){ f1.close();} 22 T read(int index){ 23 T j; 24 f1.seekg(index * sizeof(T), ios::beg); 25 f1.read((char*) &j , sizeof(T)); 26 return j; 27 } 28 void write(T j, int index){ 29 f1.seekp(index * sizeof(T), ios::beg); 30 f1.write((char*) &j, sizeof(T)); 31 } 32 T readNext(void){ 33 T j; 34 f1.read((char*) &j, sizeof(T)); 35 return j; 36 } 37 void writeNext(T j){ 38 f1.write((char*) &j, sizeof(T)); 39 f1.flush(); 40 } 41 void rewind(void){ 42 f1.seekg(0, ios::beg); 43 f1.seekp(0, ios::beg);} 44 }; 45 struct student{ 46 char name[20]; 47 char stdno[14]; 48 double avg; 49 friend ostream& operator<<(ostream & o1, student m1){ 50 o1<<"( "<<m1.name<<" , "<<m1.stdno<< 51 " , "<<m1.avg<<" ) "; 52 return o1; 53 } 54 char& operator[](const int& index){ 55 if(unsigned(index) < strlen(stdno)) 56 return stdno[index]; 57 return stdno[0]; 58 } 59 }; 60 int main(){ 61 myArray<student> myi("temp6.in"); 62 student st1 = {"Ali", "933424", 15.1}; 63 int a = 1; 64 cout << st1[a] << endl; 65 st1[1] = '2'; 66 myi.writeNext(st1); 67 strcpy(st1.name, "Reza"); 68 strcpy(st1.stdno, "923434"); 69 st1.avg = 14.2; 70 myi.writeNext(st1); 71 myi.rewind(); 72 cout << myi.readNext() << endl; 73 cout << myi.readNext() << endl; 74 cout << myi.read(0) << endl; 75 cout << myi.read(1) << endl; 76 return 0; 77 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 #include<cstring> 5 using namespace std; 6 template<typename T>class myArray{ 7 fstream f1; 8 public: 9 myArray(const char* fname = "temp1.in"){ 10 f1.open(fname, ios::in | ios::out | ios::binary); 11 if(!f1){ 12 f1.open(fname, ios::out | ios::binary); 13 if(!f1){ 14 cout << "Can not open file" << endl; 15 exit(0); 16 }else{ 17 f1.close(); 18 f1.open(fname, ios::in | ios::out | ios::binary); 19 } 20 } 21 } 22 ~myArray(){ f1.close();} 23 T read(const int index){ 24 T j; 25 f1.seekg(index * sizeof(T), ios::beg); 26 // ios::end ios::cur 27 f1.read((char*) &j , sizeof(T)); 28 return j; 29 } 30 void write(T j, int index){ 31 f1.seekp(index * sizeof(T), ios::beg); 32 f1.write((char*) &j , sizeof(T)); 33 } 34 T& readNext(void){ 35 static T j; 36 f1.read((char*) &j , 37 sizeof(T)); 38 return j; 39 } 40 void writeNext(T j){ 41 f1.write((char*) &j, sizeof(T)); 42 f1.flush(); 43 } 44 void rewind(void){ 45 f1.seekg(0, ios::beg); 46 f1.seekp(0, ios::beg); 47 } 48 }; 49 struct student{ 50 char name[20]; 51 char stdno[14]; 52 double avg; 53 friend ostream& operator<<(ostream& o1, const student& m1){ 54 o1 << "( " << m1.name << " , " << m1.stdno 55 << " , " << m1.avg << " ) "; 56 return o1; 57 } 58 char& operator[](const int& index); 59 }; 60 inline char& student::operator[](const int& index){ 61 if(unsigned(index) < strlen(stdno)) 62 return stdno[index]; 63 return stdno[0]; 64 } 65 int main(){ 66 myArray<student> myi("temp6.in"); 67 //~ unsigned aa =1; 68 //~ unsigned int ddd = 33; 69 //~ unsigned char dddrr = 'c'; 70 student st1 = {"Ali", "933424", 15.1}; 71 int a = 1; 72 cout << st1[a] << endl; 73 st1[1] = '2'; 74 myi.writeNext(st1); 75 strcpy(st1.name, "Reza"); 76 strcpy(st1.stdno, "923434"); 77 st1.avg=14.2; 78 myi.writeNext(st1); 79 myi.rewind(); 80 cout << myi.readNext() << endl; 81 student* ps = &myi.readNext(); //Error 82 cout << *ps <<endl; 83 cout << myi.readNext() <<endl; 84 cout << myi.read(0) << endl; 85 cout << myi.read(1) << endl; 86 return 0; 87 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 #include<cstring> 5 using namespace std; 6 template<typename T> class myArray{ 7 fstream f1; 8 public: 9 myArray(const char* fname = "temp1.in"){ 10 f1.open(fname, ios::in | ios::out | ios::binary); 11 if(!f1){ 12 f1.open(fname, ios::out | ios::binary); 13 if(!f1){ 14 cout << "Can not open file" << endl; 15 exit(0); 16 }else{ 17 f1.close(); 18 f1.open(fname, ios::in | ios::out | ios::binary); 19 } 20 } 21 } 22 ~myArray(){ f1.close();} 23 T read(const int index){ 24 T j; 25 f1.seekg(index * sizeof(T), ios::beg); 26 f1.read((char*) &j, sizeof(T)); 27 return j; 28 } 29 void write(T j, int index){ 30 f1.seekp(index * sizeof(T), ios::beg); 31 f1.write((char*) &j, sizeof(T)); 32 } 33 T& readNext(void){ 34 static T j; 35 f1.read((char*) &j, sizeof(T)); 36 return j; 37 } 38 void writeNext(T j){ 39 f1.write((char*) &j, sizeof(T)); 40 f1.flush(); 41 } 42 void rewind(void){ 43 f1.seekg(0, ios::beg); 44 f1.seekp(0, ios::beg); 45 } 46 }; 47 struct student{ 48 char name[20]; 49 char stdno[14]; 50 double avg; 51 friend ostream& operator<<(ostream& o1, const student& m1){ 52 o1 << "( " << m1.name << " , " << m1.stdno 53 << " , " << m1.avg << " ) "; 54 return o1; 55 } 56 char& operator[](const int& index){ 57 if(unsigned(index) < strlen(stdno)) 58 return stdno[index]; 59 return stdno[0]; 60 } 61 }; 62 int main(){ 63 myArray<student> myi("temp6.in"); 64 student st1 = {"Ali", "933424", 15.1}; 65 int a = 1; 66 cout << st1[a] << endl; 67 st1[1] = '2'; 68 myi.writeNext(st1); 69 strcpy(st1.name, "Reza"); 70 strcpy(st1.stdno, "923434"); 71 st1.avg = 14.2; 72 myi.writeNext(st1); 73 myi.rewind(); 74 cout << myi.readNext() << endl; 75 student* ps = &myi.readNext(); //Error 76 cout << *ps << endl; 77 cout << myi.readNext() << endl; 78 cout << myi.read(0) << endl; 79 cout << myi.read(1) << endl; 80 return 0; 81 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 #include<cstring> 5 using namespace std; 6 template<typename T> class myArray{ 7 fstream f1; 8 public: 9 myArray(const string fname = "temp1.in"){ 10 f1.open(fname, ios::in | ios::out | ios::binary); 11 if(!f1){ 12 f1.open(fname, ios::out | ios::binary); 13 if(!f1) 14 throw fname + "Can not open file"; 15 else{ 16 f1.close(); 17 f1.open(fname, ios::in | ios::out | ios::binary); 18 } 19 } 20 } 21 ~myArray(){ f1.close();} 22 T read(int index){ 23 T j; 24 f1.seekg(index * sizeof(T), ios::beg); 25 f1.read((char*) &j , sizeof(T)); 26 return j; 27 } 28 void write(T j, int index){ 29 f1.seekp(index * sizeof(T), ios::beg); 30 f1.write((char*) &j, sizeof(T)); 31 } 32 T readNext(void){ 33 T j; 34 f1.read((char*) &j, sizeof(T)); 35 return j; 36 } 37 void writeNext(T j){ 38 f1.write((char*) &j, sizeof(T)); 39 f1.flush(); 40 } 41 void rewind(void){ 42 f1.seekg(0, ios::beg); 43 f1.seekp(0, ios::beg); 44 } 45 }; 46 struct student{ 47 char name[20]; 48 char stdno[14]; 49 double avg; 50 friend ostream& operator<<(ostream & o1, student m1){ 51 o1<<"( "<<m1.name<<" , "<<m1.stdno<< 52 " , "<<m1.avg<<" ) "; 53 return o1; 54 } 55 char& operator[](const int& index){ 56 if(unsigned(index) < strlen(stdno)) 57 return stdno[index]; 58 return stdno[0]; 59 } 60 }; 61 int main(){ 62 myArray<student> myi("temp6.in"); 63 student st1 = {"Ali", "933424", 15.1}; 64 int a = 1; 65 cout << st1[a] << endl; 66 st1[a] = '2'; 67 myi.writeNext(st1); 68 strcpy(st1.name, "Reza"); 69 strcpy(st1.stdno, "923434"); 70 st1.avg = 14.2; 71 myi.writeNext(st1); 72 myi.rewind(); 73 cout << myi.readNext() << endl; 74 cout << myi.readNext() << endl; 75 cout << myi.read(0) << endl; 76 cout << myi.read(1) << endl; 77 return 0; 78 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 #include<cstring> 5 using namespace std; 6 template<typename T> class myArray{ 7 fstream f1; 8 public: 9 myArray(const string fname = "temp1.in"){ 10 f1.open(fname, ios::in | ios::out | ios::binary); 11 if(!f1){ 12 f1.open(fname, ios::out | ios::binary); 13 if(!f1) 14 throw fname + "Can not open file"; 15 else{ 16 f1.close(); 17 f1.open(fname, ios::in | ios::out | ios::binary); 18 } 19 } 20 } 21 ~myArray(){ f1.close();} 22 T read(int index){ 23 T j; 24 f1.seekg(index * sizeof(T), ios::beg); 25 f1.read((char*) &j, sizeof(T)); 26 return j; 27 } 28 void write(T j, int index){ 29 f1.seekp(index * sizeof(T), ios::beg); 30 f1.write((char*) &j, sizeof(T)); 31 } 32 T readNext(void){ 33 T j; 34 f1.read((char *) &j, sizeof(T)); 35 return j; 36 } 37 void writeNext(T j){ 38 f1.write((char *) &j, sizeof(T)); 39 f1.flush(); 40 } 41 void rewind(void){ 42 f1.seekg(0, ios::beg); 43 f1.seekp(0, ios::beg); 44 } 45 }; 46 struct student{ 47 char name[20]; 48 char stdno[14]; 49 double avg; 50 friend ostream& operator<<(ostream & o1, student m1){ 51 o1<<"( "<<m1.name<<" , "<<m1.stdno<< 52 " , "<<m1.avg<<" ) "; 53 return o1; 54 } 55 char& operator[](int index){ 56 if(unsigned(index) < strlen(stdno)) 57 return stdno[index]; 58 return stdno[0]; 59 } 60 }; 61 int main(){ 62 myArray<student> myi("temp6.in"); 63 student st1 = {"Ali", "933424", 15.1}; 64 int a = 1; 65 cout << st1[a] << endl; 66 st1[1] = '2'; 67 myi.writeNext(st1); 68 strcpy(st1.name, "Reza"); 69 strcpy(st1.stdno, "923434"); 70 st1.avg = 14.2; 71 myi.writeNext(st1); 72 myi.rewind(); 73 cout << myi.readNext() << endl; 74 cout << myi.readNext() << endl; 75 cout << myi.read(0) << endl; 76 cout << myi.read(1) << endl; 77 return 0; 78 }
1 // Error 2 #include <fstream> 3 #include<iostream> 4 #include<cstring> 5 using namespace std; 6 template<typename T> class myArray{ 7 fstream f1; 8 public: 9 myArray(const string fname = "temp1.in"){ 10 f1.open(fname, ios::in | ios::out | ios::binary); 11 if(!f1){ 12 f1.open(fname, ios::out | ios::binary); 13 if(!f1) 14 throw fname + "Can not open file"; 15 else{ 16 f1.close(); 17 f1.open(fname, ios::in | ios::out | ios::binary); 18 } 19 } 20 } 21 ~myArray(){ f1.close();} 22 T read(int index){ 23 T j; 24 f1.seekg(index * sizeof(T), ios::beg); 25 f1.read((char*) &j, sizeof(T)); 26 return j; 27 } 28 void write(T j, int index){ 29 f1.seekp(index * sizeof(T), ios::beg); 30 f1.write((char*) &j, sizeof(T)); 31 } 32 T readNext(void){ 33 T j; 34 f1.read((char*) &j, sizeof(T)); 35 return j; 36 } 37 void writeNext(T j){ 38 f1.write((char*) &j, sizeof(T)); 39 f1.flush(); 40 } 41 void rewind(void){ 42 f1.seekg(0, ios::beg); 43 f1.seekp(0, ios::beg); 44 } 45 }; 46 struct student{ 47 char name[20]; 48 char stdno[14]; 49 double avg; 50 friend ostream& operator<<(ostream& o1, student m1){ 51 o1<<"( "<<m1.name<<" , "<<m1.stdno<< 52 " , "<<m1.avg<<" ) "; 53 return o1; 54 } 55 char operator[](int index){ 56 if(unsigned(index) < strlen(stdno)) 57 return stdno[index]; 58 return stdno[0]; 59 } 60 }; 61 int main(){ 62 myArray<student> myi("temp6.in"); 63 student st1 = {"Ali", "933424", 15.1}; 64 int a = 1; 65 cout << st1[a] << endl; 66 myi.writeNext(st1); 67 strcpy(st1.name, "Reza"); 68 strcpy(st1.stdno, "923434"); 69 st1.avg = 14.2; 70 myi.writeNext(st1); 71 myi.rewind(); 72 cout << myi.readNext() << endl; 73 cout << myi.readNext() << endl; 74 cout << myi.read(0) << endl; 75 cout << myi.read(1) << endl; 76 return 0; 77 }
1 // Error 2 #include <fstream> 3 #include<iostream> 4 #include <cstdlib> 5 #include<cstring> 6 using namespace std; 7 template<typename T> class myArray{ 8 fstream f1; 9 public: 10 myArray(const string fname="temp1.in"){ 11 f1.open(fname,ios::in|ios::out|ios::binary); 12 if(!f1){ 13 f1.open(fname, ios::out | ios::binary); 14 if(!f1) 15 throw fname + "Can not open file"; 16 else{ 17 f1.close(); 18 f1.open(fname, ios::in | ios::out | ios::binary); 19 } 20 } 21 } 22 ~myArray(){ f1.close();} 23 T read(int index){ 24 T j; 25 f1.seekg(index * sizeof(T), ios::beg); 26 f1.read((char*) &j, sizeof(T)); 27 return j; 28 } 29 void write(T j, int index){ 30 f1.seekp(index * sizeof(T), ios::beg); 31 f1.write((char*) &j, sizeof(T)); 32 } 33 T readNext(void){ 34 T j; 35 f1.read((char*) &j, sizeof(T)); 36 return j; 37 } 38 void writeNext(T j){ 39 f1.write((char*) &j, sizeof(T)); 40 f1.flush(); 41 } 42 void rewind(void){ 43 f1.seekg(0, ios::beg); 44 f1.seekp(0, ios::beg);} 45 }; 46 struct student{ 47 char name[20]; 48 char stdno[14]; 49 double avg; 50 friend ostream& operator<<(ostream& o1, student m1){ 51 o1<<"( "<<m1.name<<" , "<<m1.stdno<< 52 " , "<<m1.avg<<" ) "; 53 return o1; 54 } 55 char& operator[](int index){ 56 if(unsigned(index) < strlen(stdno)) 57 return stdno[index]; 58 return stdno[0]; 59 } 60 }; 61 int main(){ 62 myArray<student> myi("temp6.in"); 63 student st1 = {"Ali", "933424", 15.1}; 64 int a = 1; 65 cout << st1[a] << endl; 66 //st1[1]='2'; 67 st1.operator[](1) = '2'; 68 myi.writeNext(st1); 69 strcpy(st1.name, "Reza"); 70 strcpy(st1.stdno, "923434"); 71 st1.avg = 14.2; 72 myi.writeNext(st1); 73 myi.rewind(); 74 cout << myi.readNext() << endl; 75 cout << myi.readNext() << endl; 76 cout << myi.read(0) << endl; 77 cout << myi.read(1) << endl; 78 return 0; 79 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 #include<cstring> 5 using namespace std; 6 template<typename T> class myArray{ 7 fstream f1; 8 public: 9 myArray(const string fname = "temp1.in"){ 10 f1.open(fname, ios::in | ios::out | ios::binary); 11 if(!f1){ 12 f1.open(fname, ios::out | ios::binary); 13 if(!f1) 14 throw fname + "Can not open file"; 15 else{ 16 f1.close(); 17 f1.open(fname, ios::in | ios::out | ios::binary); 18 } 19 } 20 } 21 ~myArray(){f1.close();} 22 T read(int index){ 23 T j; 24 f1.seekg(index * sizeof(T), ios::beg); 25 f1.read((char*) &j, sizeof(T)); 26 return j; 27 } 28 void write(T j, int index){ 29 f1.seekp(index * sizeof(T), ios::beg); 30 f1.write((char*) &j, sizeof(T)); 31 } 32 T readNext(void){ 33 T j; 34 f1.read((char*) &j, sizeof(T)); 35 return j; 36 } 37 void writeNext(T j){ 38 f1.write((char*) &j, sizeof(T)); 39 f1.flush(); 40 } 41 void rewind(void){ 42 f1.seekg(0, ios::beg); 43 f1.seekp(0, ios::beg); 44 } 45 }; 46 struct student{ 47 char name[20]; 48 char stdno[14]; 49 double avg; 50 friend ostream& operator<<(ostream& o1, const student& m1){ 51 o1 << "( " << m1.name << " , " << m1.stdno 52 << " , " << m1.avg << " ) "; 53 return o1; 54 } 55 char& operator[](int index){ 56 if(index >= 0 && unsigned(index) < strlen(stdno)) 57 return stdno[index]; 58 return stdno[0]; 59 } 60 }; 61 int main(){ 62 myArray<student> myi("temp6.in"); 63 student st1={"Ali", "933424", 15.1}; 64 int a=1; 65 cout << st1[a] << endl; 66 st1[1] = '2'; 67 st1.operator[](1) = '2'; 68 myi.writeNext(st1); 69 strcpy(st1.name, "Reza"); 70 strcpy(st1.stdno, "923434"); 71 st1.avg = 14.2; 72 myi.writeNext(st1); 73 myi.rewind(); 74 cout << myi.readNext() << endl; 75 cout << myi.readNext() << endl; 76 cout <<myi.read(0) << endl; 77 cout << myi.read(1) << endl; 78 return 0; 79 }
1 #include <fstream> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 template<typename T> class myArray{ 6 fstream f1; 7 public: 8 myArray(const string fname = "temp1.in"){ 9 f1.open(fname, ios::in | ios::out | ios::binary); 10 if(!f1){ 11 f1.open(fname, ios::out | ios::binary); 12 if(!f1) 13 throw fname + " Can not open file"; 14 else{ 15 f1.close(); 16 f1.open(fname, ios::in | ios::out | ios::binary); 17 } 18 } 19 } 20 ~myArray(){f1.close();} 21 T read(int index){ 22 T j; 23 f1.seekg(index * sizeof(T), ios::beg); 24 f1.read((char*) &j, sizeof(T)); 25 return j; 26 } 27 void write(T j, int index){ 28 f1.seekp(index * sizeof(T), ios::beg); 29 f1.write((char*) &j, sizeof(T)); 30 } 31 T readNext(void){ 32 T j; 33 f1.read((char*) &j, sizeof(T)); 34 return j; 35 } 36 void writeNext(T j){ 37 f1.write((char*) &j, sizeof(T)); 38 f1.flush(); 39 } 40 void rewind(void){ 41 f1.seekg(0, ios::beg); 42 f1.seekp(0, ios::beg); 43 } 44 }; 45 struct student{ 46 char name[20]; 47 char stdno[14]; 48 double avg; 49 friend ostream& operator<<(ostream & o1, student m1){ 50 o1 << "( " << m1.name << " , " << m1.stdno 51 << " , " << m1.avg << " ) "; 52 return o1; 53 } 54 char& operator[](int index){ 55 if(index >= 0 && unsigned(index) < strlen(stdno)) 56 return stdno[index]; 57 return stdno[0]; 58 } 59 }; 60 int main(){ 61 myArray<student> myi("temp6.in"); 62 student st1 = {"Ali", "933424", 15.1}; 63 int a=1; 64 cout << st1 << endl; 65 cout << st1[a] << endl; 66 st1[1] = '2'; 67 st1.operator[](1) = '2'; 68 myi.writeNext(st1); 69 strcpy(st1.name, "Reza"); 70 strcpy(st1.stdno, "923434"); 71 st1.avg = 14.2; 72 myi.writeNext(st1); 73 myi.rewind(); 74 cout << myi.readNext() << endl; 75 cout << myi.readNext() << endl; 76 cout << myi.read(0) << endl; 77 cout << myi.read(1) << endl; 78 return 0; 79 }
1 // Error 2 #include <fstream> 3 #include<iostream> 4 #include <cstdlib> 5 #include<cstring> 6 using namespace std; 7 //void * 8 //char * 9 //int ff3(void * hh); 10 //int ff4(char * hh); 11 template<typename T> 12 class myArray{ 13 fstream f1; 14 public: 15 myArray(const char *fname="temp1.in"){ 16 f1.open(fname,ios::in|ios::out|ios::binary); 17 if(!f1){ 18 f1.open(fname,ios::out|ios::binary); 19 if(!f1){ cout<<"Can not open file"<<endl; exit(0);} 20 else{f1.close(); f1.open(fname,ios::in|ios::out|ios::binary);} 21 } 22 } 23 ~myArray(){ f1.close();} 24 T read(int index){ 25 T j; 26 f1.seekg(index*sizeof(T),ios::beg); // ios::end ios::cur 27 f1.read((char *)&j , sizeof(T)); 28 return j; 29 } 30 void write(T j, int index){ 31 f1.seekp(index*sizeof(T),ios::beg); 32 f1.write((char *) &j , sizeof(T)); 33 } 34 T readNext(void){ 35 T j; 36 f1.read((char *)&j , sizeof(T)); 37 return j; 38 } 39 void writeNext(T j){ 40 f1.write((char *)&j, sizeof(T)); 41 f1.flush(); 42 } 43 void rewind(void){ f1.seekg(0,ios::beg);f1.seekp(0,ios::beg);} 44 }; 45 struct student{ 46 char name[20]; 47 char stdno[14]; 48 double avg; 49 // Error 50 //friend ostream& operator<<(ostream & o1, student &); 51 friend ostream& operator<<(ostream & o1, student &); 52 char & operator[](int index); 53 }; 54 int mystrlen(char *s){int i;for(i=0;s[i];i++); return i;} 55 //if(unsigned(index)<strlen(stdno)) return stdno[index]; 56 char & student::operator[](int index){ 57 if(index<mystrlen(stdno)) return stdno[index]; 58 return stdno[0]; 59 } 60 // Error 61 //ostream& operator<<(ostream & o1, student & m1){ 62 ostream& operator<<(ostream & o1, student & m1){ 63 o1<<"( "<<m1.name<<" , "<<m1.stdno<< 64 " , "<<m1.avg<<" ) "; 65 return o1; 66 } 67 int main(){//myArray<student> myi("temp6.in"); 68 student st1={"Ali","933424",15.1};int a=1; 69 cout<<st1<<endl;operator<<(cout,st1); 70 cout<<st1[a]<<endl; st1[1]='2'; st1.operator[](1) = '2'; 71 cout<<st1; 72 /*myi.writeNext(st1); 73 strcpy(st1.name,"Reza");strcpy(st1.stdno,"923434"); 74 st1.avg=14.2; myi.writeNext(st1); myi.rewind(); 75 cout<<myi.readNext()<<endl; 76 //operator<<(cout,myi.readNext()); 77 //student *ps= & myi.readNext(); Error 78 cout<<myi.readNext()<<endl<<endl; 79 cout<<myi.read(0)<<endl; 80 cout<<myi.read(1)<<endl; 81 //myi.write(54,10); 82 //cout<<myi.read(25)<<endl<<endl; 83 */return 0; 84 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 #include<cstring> 5 using namespace std; 6 //void * 7 //char * 8 //int ff3(void * hh); 9 //int ff4(char * hh); 10 template<typename T> 11 class myArray{ 12 fstream f1; 13 public: 14 myArray(const char *fname="temp1.in"){ 15 f1.open(fname,ios::in|ios::out|ios::binary); 16 if(!f1){ 17 f1.open(fname,ios::out|ios::binary); 18 if(!f1){ cout<<"Can not open file"<<endl; exit(0);} 19 else{f1.close(); f1.open(fname,ios::in|ios::out|ios::binary);} 20 } 21 } 22 ~myArray(){ f1.close();} 23 T read(int index){ 24 T j; 25 f1.seekg(index*sizeof(T),ios::beg); // ios::end ios::cur 26 f1.read((char *)&j , sizeof(T)); 27 return j; 28 } 29 void write(T j, int index){ 30 f1.seekp(index*sizeof(T),ios::beg); 31 f1.write((char *) &j , sizeof(T)); 32 } 33 T readNext(void){ 34 T j; 35 f1.read((char *)&j , sizeof(T)); 36 return j; 37 } 38 void writeNext(T j){ 39 f1.write((char *)&j, sizeof(T)); 40 f1.flush(); 41 } 42 void rewind(void){ f1.seekg(0,ios::beg);f1.seekp(0,ios::beg);} 43 }; 44 struct student{ 45 char name[20]; 46 char stdno[14]; 47 double avg; 48 // Error 49 //friend ostream& operator<<(ostream & o1, student &); 50 friend ostream& operator<<(ostream & o1, student ); 51 char & operator[](int index); 52 }; 53 int mystrlen(char *s){int i;for(i=0;s[i];i++); return i;} 54 //if(unsigned(index)<strlen(stdno)) return stdno[index]; 55 char & student::operator[](int index){ 56 if(index<mystrlen(stdno)) return stdno[index]; 57 return stdno[0]; 58 } 59 // Error 60 //ostream& operator<<(ostream & o1, student & m1){ 61 ostream& operator<<(ostream & o1, student m1){ 62 o1<<"( "<<m1.name<<" , "<<m1.stdno<< 63 " , "<<m1.avg<<" ) "; 64 return o1; 65 } 66 int main(){//myArray<student> myi("temp6.in"); 67 student st1={"Ali","933424",15.1};int a=1; 68 cout<<st1<<endl;operator<<(cout,st1); 69 cout<<st1[a]<<endl; st1[1]='2'; st1.operator[](1) = '2'; 70 myi.writeNext(st1); 71 strcpy(st1.name,"Reza");strcpy(st1.stdno,"923434"); 72 st1.avg=14.2; myi.writeNext(st1); myi.rewind(); 73 cout<<myi.readNext()<<endl; 74 //operator<<(cout,myi.readNext()); 75 //student *ps= & myi.readNext(); Error 76 cout<<myi.readNext()<<endl<<endl; 77 cout<<myi.read(0)<<endl; 78 cout<<myi.read(1)<<endl; 79 //myi.write(54,10); 80 //cout<<myi.read(25)<<endl<<endl; 81 return 0; 82 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 #include<cstring> 5 using namespace std; 6 template<typename T>class myFileArray{fstream f1; 7 public: 8 myFileArray(const char *fname="temp1.in"){ 9 f1.open(fname,ios::in|ios::out|ios::binary); 10 if(!f1){ 11 f1.open(fname,ios::out|ios::binary); 12 if(!f1){ cout<<"Can not open file"<<endl; exit(0);} 13 else{f1.close(); f1.open(fname,ios::in|ios::out|ios::binary);} 14 } 15 } 16 ~myFileArray(){ f1.close();} 17 T read(int index){ 18 T j; 19 f1.seekg(0,ios::end); 20 long long int l1=f1.tellg(); 21 l1 /= sizeof(T); 22 if(index>=l1){cout<<"Index is out of range\n";exit(0);} 23 f1.seekg(index*sizeof(T),ios::beg); // ios::end ios::cur 24 f1.read((char *)&j , sizeof(T)); 25 return j; 26 } 27 void write(T j, int index){ 28 f1.seekg(0,ios::end); 29 long long int l1=f1.tellg(); 30 l1 /= sizeof(T); 31 if(index>l1){cout<<"Index is out of range\n";exit(0);} 32 f1.seekp(index*sizeof(T),ios::beg); 33 f1.write((char *) &j , sizeof(T)); 34 } 35 T readNext(void){ 36 T j; 37 long long int pos=f1.tellg(); 38 f1.seekg(0,ios::end); 39 long long int l1=f1.tellg(); 40 if(pos == l1) 41 {cout<<"Index is out of range\n";exit(0);} 42 f1.seekg(pos,ios::beg); 43 f1.read((char *)&j , sizeof(T)); 44 return j; 45 } 46 void writeNext(T j){ 47 f1.write((char *)&j, sizeof(T)); 48 f1.flush(); 49 } 50 void rewind(void){ f1.seekg(0,ios::beg);f1.seekp(0,ios::beg);} 51 }; 52 struct student{ 53 char name[20]; 54 char stdno[14]; 55 double avg; 56 // Error 57 //friend ostream& operator<<(ostream & o1, student &); 58 friend ostream& operator<<(ostream & o1, student ); 59 char & operator[](int index); 60 }; 61 int mystrlen(char *s){int i;for(i=0;s[i];i++); return i;} 62 //if(unsigned(index)<strlen(stdno)) return stdno[index]; 63 char & student::operator[](int index){ 64 if(index<mystrlen(stdno)) return stdno[index]; 65 return stdno[0]; 66 } 67 // Error 68 //ostream& operator<<(ostream & o1, student & m1){ 69 ostream& operator<<(ostream & o1, student m1){ 70 o1<<"( "<<m1.name<<" , "<<m1.stdno<< 71 " , "<<m1.avg<<" ) "; 72 return o1; 73 } 74 int main(){myFileArray<student> myi("temp6.in"); 75 student st1={"Ali","933424",15.1};int a=1; 76 cout<<st1<<endl;operator<<(cout,st1); 77 cout<<st1[a]<<endl; st1[1]='2'; st1.operator[](1) = '2'; 78 myi.writeNext(st1); 79 strcpy(st1.name,"Reza");strcpy(st1.stdno,"923434"); 80 st1.avg=14.2; myi.writeNext(st1); myi.rewind(); 81 cout<<myi.readNext()<<endl; 82 //operator<<(cout,myi.readNext()); 83 //student *ps= & myi.readNext(); Error 84 cout<<myi.readNext()<<endl<<endl; 85 cout<<myi.read(0)<<endl; 86 cout<<myi.read(1)<<endl; 87 myi.write(st1,10); 88 cout<<myi.read(25)<<endl<<endl; 89 return 0; 90 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 #include<cstring> 5 using namespace std; 6 template<typename T> struct write_type{T data;int index;}; 7 template<typename T>class myFileArray{ 8 fstream f1; 9 public: 10 myFileArray(const char *fname="temp1.in"){ 11 f1.open(fname,ios::in|ios::out|ios::binary); 12 if(!f1){ 13 f1.open(fname,ios::out|ios::binary); 14 if(!f1){ cout<<"Can not open file"<<endl; exit(0);} 15 else{f1.close(); f1.open(fname,ios::in|ios::out|ios::binary);} 16 } 17 } 18 ~myFileArray(){ f1.close();} 19 T read(int index){ 20 T j; 21 f1.seekg(0,ios::end); 22 long long int l1=f1.tellg(); 23 l1 /= sizeof(T); 24 if(index>=l1){cout<<"Index is out of range\n"; throw 2;} 25 f1.seekg(index*sizeof(T),ios::beg); // ios::end ios::cur 26 f1.read((char *)&j , sizeof(T)); 27 return j; 28 } 29 void write(T j, int index){ 30 f1.seekg(0,ios::end); 31 long long int l1=f1.tellg(); 32 l1 /= sizeof(T); 33 if(index>l1){cout<<"Index is out of range\n";exit(0);} 34 f1.seekp(index*sizeof(T),ios::beg); 35 f1.write((char *) &j , sizeof(T)); 36 } 37 T readNext(void){ 38 T j; 39 long long int pos=f1.tellg(); 40 f1.seekg(0,ios::end); 41 long long int l1=f1.tellg(); 42 if(pos == l1) 43 {cout<<"Index is out of range\n";exit(0);} 44 f1.seekg(pos,ios::beg); 45 f1.read((char *)&j , sizeof(T)); 46 return j; 47 } 48 T& operator[](int index){ 49 static T j; 50 f1.seekg(0,ios::end); 51 long long int l1=f1.tellg(); 52 l1 /= sizeof(T); 53 if(index>=l1){cout<<"Index is out of range\n";exit(0);} 54 f1.seekg(index*sizeof(T),ios::beg); // ios::end ios::cur 55 f1.read((char *)&j , sizeof(T)); 56 return j; 57 } 58 const T& operator[](const write_type<T> &idx){ // a[complexCls(2,3)] 59 f1.seekg(0,ios::end); 60 long long int l1=f1.tellg(); 61 l1 /= sizeof(T); 62 if(idx.index>l1){cout<<"Index is out of range\n";exit(0);} 63 f1.seekp(idx.index*sizeof(T),ios::beg); 64 f1.write((char *) &idx.data , sizeof(T)); 65 return idx.data; 66 } 67 void writeNext(T j){ 68 f1.write((char *)&j, sizeof(T)); 69 f1.flush(); 70 } 71 void rewind(void){ f1.seekg(0,ios::beg);f1.seekp(0,ios::beg);} 72 }; 73 struct student{ 74 char name[20]; 75 char stdno[14]; 76 double avg; 77 // Error 78 //friend ostream& operator<<(ostream & o1, student &); 79 friend ostream& operator<<(ostream & o1, student ); 80 char & operator[](int index); 81 }; 82 int mystrlen(char *s){int i;for(i=0;s[i];i++); return i;} 83 //if(unsigned(index)<strlen(stdno)) return stdno[index]; 84 char & student::operator[](int index){ 85 if(index<mystrlen(stdno)) return stdno[index]; 86 return stdno[0]; 87 } 88 // Error 89 //ostream& operator<<(ostream & o1, student & m1){ 90 ostream& operator<<(ostream & o1, student m1){ 91 o1<<"( "<<m1.name<<" , "<<m1.stdno<< 92 " , "<<m1.avg<<" ) "; 93 return o1; 94 } 95 int main(){myFileArray<student> myi("temp6.in"); 96 student st1={"Ali","933424",15.1};int a=1; 97 student st2={"Kamran","68588",12.1}; 98 write_type<student> wt1{st2,0}; 99 cout<<st1<<endl;operator<<(cout,st1); 100 cout<<st1[a]<<endl; st1[1]='2'; st1.operator[](1) = '2'; 101 myi.writeNext(st1); 102 strcpy(st1.name,"Reza");strcpy(st1.stdno,"923434"); 103 st1.avg=14.2; myi.writeNext(st1); myi.rewind(); 104 cout<<myi.readNext()<<endl; 105 //operator<<(cout,myi.readNext()); 106 //student *ps= & myi.readNext(); Error 107 cout<<myi.readNext()<<endl<<endl; 108 cout<<myi.read(0)<<endl; 109 cout<<"myi[0]"<<endl; 110 cout<<myi[0]<<endl; 111 cout<<myi.read(1)<<endl; 112 cout<<"myi[1]"<<endl; 113 cout<<myi[1]<<endl; 114 cout<<myi[wt1]<<endl; 115 cout<<myi.read(0)<<endl<<endl; 116 return 0; 117 }
1 #include <fstream> 2 #include<iostream> 3 #include <cstdlib> 4 #include<cstring> 5 using namespace std; 6 template<typename T>class myFileArray{fstream f1; 7 public: 8 myFileArray(const char *fname="temp1.in"){ 9 f1.open(fname,ios::in|ios::out|ios::binary); 10 if(!f1){ 11 f1.open(fname,ios::out|ios::binary); 12 if(!f1){ cout<<"Can not open file"<<endl; exit(0);} 13 else{f1.close(); f1.open(fname,ios::in|ios::out|ios::binary);} 14 } 15 } 16 ~myFileArray(){ f1.close();} 17 T read(int index){ 18 T j; 19 f1.seekg(0,ios::end); 20 long long int l1=f1.tellg(); 21 l1 /= sizeof(T); 22 if(index>=l1){cout<<"Index is out of range\n";exit(0);} 23 f1.seekg(index*sizeof(T),ios::beg); // ios::end ios::cur 24 f1.read((char *)&j , sizeof(T)); 25 return j; 26 } 27 void write(T j, int index){ 28 f1.seekg(0,ios::end); 29 long long int l1=f1.tellg(); 30 l1 /= sizeof(T); 31 if(index>l1){cout<<"Index is out of range\n";exit(0);} 32 f1.seekp(index*sizeof(T),ios::beg); 33 f1.write((char *) &j , sizeof(T)); 34 } 35 T readNext(void){ 36 T j; 37 long long int pos=f1.tellg(); 38 f1.seekg(0,ios::end); 39 long long int l1=f1.tellg(); 40 if(pos == l1) 41 {cout<<"Index is out of range\n";exit(0);} 42 f1.seekg(pos,ios::beg); 43 f1.read((char *)&j , sizeof(T)); 44 return j; 45 } 46 T& operator[](int index){ 47 static T j; 48 f1.seekg(0,ios::end); 49 long long int l1=f1.tellg(); 50 l1 /= sizeof(T); 51 if(index>=l1){cout<<"Index is out of range\n";exit(0);} 52 f1.seekg(index*sizeof(T),ios::beg); // ios::end ios::cur 53 f1.read((char *)&j , sizeof(T)); 54 return j; 55 } 56 const T& operator[](int index){ 57 static T j; 58 f1.seekg(0,ios::end); 59 long long int l1=f1.tellg(); 60 l1 /= sizeof(T); 61 if(index>l1){cout<<"Index is out of range\n";exit(0);} 62 f1.seekp(index*sizeof(T),ios::beg); 63 f1.write((char *) &j , sizeof(T)); 64 } 65 void writeNext(T j){ 66 f1.write((char *)&j, sizeof(T)); 67 f1.flush(); 68 } 69 void rewind(void){ f1.seekg(0,ios::beg);f1.seekp(0,ios::beg);} 70 }; 71 struct student{ 72 char name[20]; 73 char stdno[14]; 74 double avg; 75 // Error 76 //friend ostream& operator<<(ostream & o1, student &); 77 friend ostream& operator<<(ostream & o1, student ); 78 char & operator[](int index); 79 }; 80 int mystrlen(char *s){int i;for(i=0;s[i];i++); return i;} 81 //if(unsigned(index)<strlen(stdno)) return stdno[index]; 82 char & student::operator[](int index){ 83 if(index<mystrlen(stdno)) return stdno[index]; 84 return stdno[0]; 85 } 86 // Error 87 //ostream& operator<<(ostream & o1, student & m1){ 88 ostream& operator<<(ostream & o1, student m1){ 89 o1<<"( "<<m1.name<<" , "<<m1.stdno<< 90 " , "<<m1.avg<<" ) "; 91 return o1; 92 } 93 int main(){myFileArray<student> myi("temp6.in"); 94 student st1={"Ali","933424",15.1};int a=1; 95 cout<<st1<<endl;operator<<(cout,st1); 96 cout<<st1[a]<<endl; st1[1]='2'; st1.operator[](1) = '2'; 97 myi.writeNext(st1); 98 strcpy(st1.name,"Reza");strcpy(st1.stdno,"923434"); 99 st1.avg=14.2; myi.writeNext(st1); myi.rewind(); 100 cout<<myi.readNext()<<endl; 101 //operator<<(cout,myi.readNext()); 102 //student *ps= & myi.readNext(); Error 103 cout<<myi.readNext()<<endl<<endl; 104 cout<<myi.read(0)<<endl; 105 cout<<"myi[0]"<<endl; 106 cout<<myi[0]<<endl; 107 cout<<myi.read(1)<<endl; 108 cout<<"myi[1]"<<endl; 109 cout<<myi[1]<<endl; 110 myi.write(st1,10); 111 myi[10]=st1; 112 cout<<myi.read(25)<<endl<<endl; 113 return 0; 114 }
1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <string> 5 #include <cstdint> 6 int main(){ // https://en.cppreference.com/w/cpp/io/basic_istream/read 7 // read() is often used for binary I/O 8 std::string bin = {'\x12', '\x12', '\x12', '\x12'}; 9 std::istringstream raw(bin); 10 std::uint32_t n; 11 if(raw.read(reinterpret_cast<char*>(&n), sizeof n)) 12 std::cout << std::hex << std::showbase << n << '\n'; 13 14 // prepare file for next snippet 15 std::ofstream("test.txt", std::ios::binary) << "abcd1\nabcd2\nabcd3"; 16 17 // read entire file into string 18 if(std::ifstream is{"test.txt", std::ios::binary | std::ios::ate}) { 19 auto size = is.tellg(); 20 std::string str(size, '\0'); // construct string to stream size 21 is.seekg(0); 22 if(is.read(&str[0], size)) 23 std::cout << str << '\n'; 24 } 25 } 26 /* https://en.cppreference.com/w/cpp/io/basic_ostream/write 27 #include <iostream> 28 int main(){ 29 int n = 0x41424344; 30 std::cout.write(reinterpret_cast<char*>(&n), sizeof n) << '\n'; 31 32 char c[]="This is sample text."; 33 std::cout.write(c, 4).write("!\n", 2); 34 } 35 */ 36 /* https://en.cppreference.com/w/cpp/io/basic_ifstream 37 #include <iostream> 38 #include <fstream> 39 #include <string> 40 int main() { 41 std::string filename = "Test.b"; 42 // prepare a file to read 43 double d = 3.14; 44 std::ofstream(filename, std::ios::binary).write(reinterpret_cast<char*>(&d), sizeof d) 45 << 123 << "abc"; 46 // open file for reading 47 std::ifstream istrm(filename, std::ios::binary); 48 if (!istrm.is_open()) { 49 std::cout << "failed to open " << filename << '\n'; 50 } else { 51 double d; 52 istrm.read(reinterpret_cast<char*>(&d), sizeof d); // binary input 53 int n; 54 std::string s; 55 if (istrm >> n >> s) // text input 56 std::cout << "read back from file: " << d << ' ' << n << ' ' << s << '\n'; 57 } 58 } 59 */
1 #include<iostream> 2 #include<fstream> 3 using namespace std; 4 struct myMat{int n; double**p;}; 5 myMat read(const char *name){ 6 myMat m;m.n=0;m.p=0;double *pd=0; 7 ifstream f(name); 8 if(!f){cout<<"cannot open file"<<endl;return m;} 9 f.read((char*)&m.n,sizeof(int)); 10 pd=new double[m.n*m.n]; 11 f.read((char*)pd,sizeof(double)*m.n*m.n); 12 f.close(); 13 m.p=new double*[m.n]; 14 for(int i=0;i<m.n;i++){ 15 m.p[i]=new double[m.n]; 16 for(int j=0;j<m.n;j++) 17 m.p[i][j]=pd[i*m.n+j]; 18 } 19 delete[]pd; 20 f.close(); 21 return m; 22 } 23 bool write(const char*name,myMat m){ 24 ofstream f1(name); 25 if(!f1){cout<<"cannot open file"<<endl;return false;} 26 double*pd=new double[m.n*m.n]; 27 for(int i=0;i<m.n;i++) 28 for(int j=0;j<m.n;j++) 29 pd[i*m.n+j]=m.p[i][j]; 30 f1.write((char*)&m.n,sizeof(int)); 31 f1.write((char*)pd,sizeof(double)*m.n*m.n); 32 f1.close(); 33 return true; 34 } 35 struct mat2{int n; double p[100][100];}; 36 mat2 read2(const char*name){ 37 mat2 m; m.n=0; 38 ifstream f(name); 39 if(!f){cout<<""<<endl;exit(0);} 40 f.read((char*)&m.n,sizeof(int)); 41 for(int i=0;i<n;i++) 42 for(int j=0;j<n;j++) 43 f.read((char*)&m.p[i][j],sizeof(double)); 44 f.close(); 45 } 46 bool write2(const char*name,mat2 m){ 47 ofstream f1(name); 48 if(!f1){cout<<"cannot open file"<<endl;return false;} 49 f1.write((char*)&m.n,sizeof(int)); 50 for(int i=0;i<m.n;i++) 51 for(int j=0;j<m.n;j++) 52 f1.write((char*)&m.p[i][j],sizeof(double)); 53 f1.close(); 54 return true; 55 } 56 myMat input(void){ 57 int i,j;myMat m; 58 cout<<"enter n "; cin>>m.n; 59 m.p=new double*[m.n]; 60 for(i=0;i<m.n;i++){ 61 m.p[i]=new double[m.n]; 62 for(j=0;j<m.n;j++){ 63 cout<<"enter a["<<i<<"]["<<j<<"]:"; 64 cin>>m.p[i][j]; 65 } 66 } 67 return m; 68 } 69 void print(myMat m){ 70 int i,j;cout<<m.n<<endl; 71 for(i=0;i<m.n;i++){ 72 for(j=0;j<m.n;j++) cout<<m.p[i][j]<<'\t'; 73 cout<<endl; 74 } 75 } 76 myMat mult(myMat m1, myMat m2){ 77 myMat m3;m3.n=m1.n;m3.p=new double*[m1.n]; 78 for(int i=0;i<m1.n;i++){ 79 m3.p[i]=new double[m1.n]; 80 for(int j=0;j<m1.n;j++){ 81 double sum=0; 82 for(int k=0;k<m1.n;k++) sum+=m1.p[i][k]*m2.p[k][j]; 83 m3.p[i][j]=sum; 84 } 85 } 86 return m3; 87 } 88 int main(){ 89 //myMat m1,m2; m1=input();m2=input();write("mat1.in",m1);write("mat2.in",m2); 90 myMat m1=read("mat1.in"),m2=read("mat2.in"),m3; 91 print(m1);print(m2); 92 m3=mult(m1,m2); 93 write("mat3.in",m3); 94 print(m3); 95 return 0; 96 }
END
rst2html file.rst file.html