Results 1 to 24 of 24
Thread: C++.
Hybrid View
-
3rd April 2009 10:02 #1
C++.
, 2 . ( ) command line. , send/don't send error.
argc argv , .Code:#include <iostream> #include <fstream> #include <string> using namespace std; void usage(string program_name) { cout << "Use " << program_name << " firstfile secondfile\n"; exit(1); } void open_file_error(string filename) { cout << "Error opening file " << filename << "\n"; exit(1); } int main(int argc, char *argv[]) // declaration of all variables { string line; fstream first_file (argv[1]); fstream second_file (argv[2]); ofstream result (argv[3]); //creates result.txt if (first_file.is_open()) //if first_file opens { while (! first_file.eof() ) //while NOT end of first_file.txt { getline (first_file, line); //get contents of first_file.txt result << line << endl; //output contents of first_file.txt to result.txt } while (! second_file.eof() ) //while NOT end of second_file.txt { getline (second_file, line); //get contents of second_file.txt result << line << endl; //output contents of second_file.txt to result.txt } cout <<"..."; } first_file.close(); second_file.close(); result.close(); system("pause"); return 0; }
Last edited by Darknauss; 3rd April 2009 at 10:10.
-
3rd April 2009 10:28 #2Banned
Join Date: Nov:2007
Location: Sofia
Posts: 1,400
command line 0, .. argv[0], argv[1].
fstream first_file (argv[0]);
fstream second_file (argv[1]);
ofstream result (argv[2]); //creates result.txt
-
3rd April 2009 10:59 #3
-
3rd April 2009 11:19 #4
-
3rd April 2009 12:02 #5
, , Usage .... . :
Code:void usage(string program_name) { cout << "Usage: " << program_name << " file_1.txt file_2.txt outfile.txt\n"; exit(1); }
Code:if (argc == 0) usage(string(argv[0]));
-
3rd April 2009 12:28 #6Banned
Join Date: Nov:2007
Location: Sofia
Posts: 1,400
-
3rd April 2009 12:32 #7
-
3rd April 2009 15:23 #8
?! :
-> Properties->Debugging ->Command Arguments Edit comboboxa /
fstream , , FILE* , , char*, open mod, :
fstream first_file (argv[1], std::ios::in);" , , , , ."
-
3rd April 2009 15:58 #9
Darknauss, ifstream .
, . .
"640K ught to be enough for anybody" - Bill Gates, 1981
::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel
-
3rd April 2009 16:01 #10
-
3rd April 2009 17:24 #11
/ ifstream fstream ? , Dev-Cpp. cmd.exe-
. :
, . ,Code:#include <iostream> #include <fstream> #include <string> using namespace std; int main(int argc, char *argv[]) // declaration of all variables { string line; int num (argc); //number of arguments string program_name (argv[0]); ifstream first_file (argv[1]); ifstream second_file (argv[2]); ofstream result (argv[3]); //creates result.txt if (argc ==1) {cout << "Usage:...";} if (first_file.is_open()) //if first_file opens { while (! first_file.eof() ) //while NOT end of first_file.txt { getline (first_file, line); //get contents of first_file.txt result << line << endl; //output contents of first_file.txt to result.txt } } else cout << "Error opening first file! \n"; if (second_file.is_open()) //if first_file opens { while (! second_file.eof() ) //while NOT end of second_file.txt { getline (second_file, line); //get contents of second_file.txt result << line << endl; //output contents of second_file.txt to result.txt } } else cout << "Error opening second file! \n"; cout << "Done! \n"; cout << "==================== \n"; cout << num << "\n"; cout << argv[0] << "\n";//all arguments are listed here ! cout << argv[1] << "\n"; cout << argv[2] << "\n"; cout << argv[3] << "\n"; first_file.close(); second_file.close(); result.close(); return 0; }
, , usage, . , , !
-
3rd April 2009 17:47 #12
- google, : - . fstream - . std::ios::in/out , . ifsream - , "i" "in" ofsream "" "out".
. breakpoint main F5.
endln, ..
result << line << endl;
:
result << line << " ";
:
argv[1] .Code:if (argc ==1) {cout << "Usage:...";}
..
Btw , ; ; result?Code:#include <iostream> #include <fstream> #include <string> using namespace std; int main(int argc, char *argv[]) // declaration of all variables { string line; int num (argc); //number of arguments if (argc ==1) { string program_name (argv[0]); cout << "Usage:...";// - usage(program_name); } // argv[1], ifstream first_file (argv[1]); // argv[2], ifstream second_file (argv[2]); // argv[3], ofstream result (argv[3]); //creates result.txt
" , , , , ."
-
3rd April 2009 18:12 #13
-
3rd April 2009 18:17 #14
-
4th April 2009 09:07 #15
-
4th April 2009 15:16 #16Registered User
Join Date: Dec:2007
Location: Sofia
Posts: 366
, , , ofstream- .
-
4th April 2009 19:53 #17
, . , . - !
... .
:headscratch:
Last edited by Darknauss; 4th April 2009 at 20:34.
-
4th April 2009 20:21 #18
-
4th April 2009 20:51 #19
-
4th April 2009 20:57 #20
-
6th April 2009 11:04 #21
-
6th April 2009 11:27 #22wtf e vector. , , . , .Code:
ifstream sort (argv[3]); if (sort.is_open()) { while (! sort.eof() ) { sort >> wtf[k]; //put every int to the vector { k++; wtf.push_back(1); } } }
8, : 1, 1, 1163067463, 1330205523Last edited by Darknauss; 6th April 2009 at 11:48.
-
6th April 2009 19:29 #23">>" "<<" /. sort STL- <algorithm>. . , .Code:
#include<iostream> #include<fstream> #include<vector> #include<algorithm> using namespace std; int main(int argc, char *argv[]) { vector<int> vect; int val; ifstream in1 (argv[1]); ifstream in2 (argv[2]); ofstream out (argv[3]); while(!in1.eof()) { in1>>val; vect.push_back(val); } while(!in2.eof()) { in2>>val; vect.push_back(val); } sort(&vect[0],&vect[vect.size()-1]); for(int i=0;i<vect.size()-1;i++) out<<vect[i]<<endl; }
-
6th April 2009 20:38 #24
, ! ( :shy: )! push_back(k), , k. . ... :P
Last edited by Darknauss; 6th April 2009 at 20:45.




Reply With Quote

Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in