Results 1 to 24 of 24

Thread: C++.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Darknauss's Avatar
    Join Date: Mar:2005
    Location: Sofia/Vienna
    Posts: 2,151

    Question C++.

    , 2 . ( ) command line. , send/don't send error.
    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;
    }
    argc argv , .

    Last edited by Darknauss; 3rd April 2009 at 10:10.

  2. #2
    Banned
    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

  3. #3
    Darknauss's Avatar
    Join Date: Mar:2005
    Location: Sofia/Vienna
    Posts: 2,151
    argv[0] ?

  4. #4
    Banned
    Join Date: Nov:2007
    Location: Sofia
    Posts: 1,400
    Quote Originally Posted by Darknauss View Post
    argv[0] ?
    , . .

  5. #5
    Darknauss's Avatar
    Join Date: Mar:2005
    Location: Sofia/Vienna
    Posts: 2,151
    , , 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]));

  6. #6
    Banned
    Join Date: Nov:2007
    Location: Sofia
    Posts: 1,400
    Quote Originally Posted by Darknauss View Post
    , , 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]));
    , argc == 1, argc e 1.

    , . , ..

  7. #7
    Darknauss's Avatar
    Join Date: Mar:2005
    Location: Sofia/Vienna
    Posts: 2,151
    . , .

  8. #8
    Defender Kaspirtov's Avatar
    Join Date: Jun:2006
    Location: Sf
    Posts: 7,414
    ?! :
    -> Properties->Debugging ->Command Arguments Edit comboboxa /

    fstream , , FILE* , , char*, open mod, :
    fstream first_file (argv[1], std::ios::in);
    " , , , , ."

  9. #9
    ɐ-əpoɔᴉu⋂ ɐ ə anrieff's Avatar
    Join Date: Apr:2004
    Location: Sofia
    Posts: 8,448
    Darknauss, ifstream .
    , . .
    "640K ught to be enough for anybody" - Bill Gates, 1981
    ::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel

  10. #10
    Defender Kaspirtov's Avatar
    Join Date: Jun:2006
    Location: Sf
    Posts: 7,414
    Quote Originally Posted by anrieff View Post
    Darknauss, ifstream .
    - : fstream first_file (argv[1], std::ios::in); argv[1].
    " , , , , ."

  11. #11
    Darknauss's Avatar
    Join Date: Mar:2005
    Location: Sofia/Vienna
    Posts: 2,151
    / 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, . , , !

  12. #12
    Defender Kaspirtov's Avatar
    Join Date: Jun:2006
    Location: Sf
    Posts: 7,414
    Quote Originally Posted by Darknauss View Post
    / ifstream fstream ?
    - google, : - . fstream - . std::ios::in/out , . ifsream - , "i" "in" ofsream "" "out".
    Quote Originally Posted by Darknauss View Post
    , Dev-Cpp. cmd.exe- . !
    . breakpoint main F5.
    Quote Originally Posted by Darknauss View Post
    , , !
    endln, ..
    result << line << endl;
    :
    result << line << " ";

    Quote Originally Posted by Darknauss View Post
    , , usage, . !
    :
    Code:
    if (argc ==1) {cout << "Usage:...";}
    argv[1] .

    ..
    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
    Btw , ; ; result?
    " , , , , ."

  13. #13
    Darknauss's Avatar
    Join Date: Mar:2005
    Location: Sofia/Vienna
    Posts: 2,151
    , . . , . - .

    , !!

  14. #14
    Defender Kaspirtov's Avatar
    Join Date: Jun:2006
    Location: Sf
    Posts: 7,414
    Quote Originally Posted by Darknauss View Post
    , . . , . - .

    , !!
    A... - - , 1,2 3 - .
    " , , , , ."

  15. #15
    Darknauss's Avatar
    Join Date: Mar:2005
    Location: Sofia/Vienna
    Posts: 2,151
    return 1; cout . , . 1 2 , , .

  16. #16
    Registered User
    Join Date: Dec:2007
    Location: Sofia
    Posts: 366
    , , , ofstream- .

  17. #17
    Darknauss's Avatar
    Join Date: Mar:2005
    Location: Sofia/Vienna
    Posts: 2,151
    , . , . - !

    ... .

    :headscratch:
    Last edited by Darknauss; 4th April 2009 at 20:34.

  18. #18
    Pesho's Avatar
    Join Date: Nov:2001
    Location: Sofia
    Posts: 5,169
    Quote Originally Posted by Darknauss View Post
    .

    . ifstream-, getline.
    , !

  19. #19
    Darknauss's Avatar
    Join Date: Mar:2005
    Location: Sofia/Vienna
    Posts: 2,151
    .get ?

  20. #20
    Pesho's Avatar
    Join Date: Nov:2001
    Location: Sofia
    Posts: 5,169
    >>
    , !

  21. #21
    Defender Kaspirtov's Avatar
    Join Date: Jun:2006
    Location: Sf
    Posts: 7,414
    Quote Originally Posted by Darknauss View Post
    .get ?
    Quote Originally Posted by Pesho View Post
    >>

    @Darknauss, "result" "result <<", first_file second_file "first_file >> , second_file >>"
    " , , , , ."

  22. #22
    Darknauss's Avatar
    Join Date: Mar:2005
    Location: Sofia/Vienna
    Posts: 2,151
    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);
              }   
           }
        }
    wtf e vector. , , . , .

    8, : 1, 1, 1163067463, 1330205523
    Last edited by Darknauss; 6th April 2009 at 11:48.

  23. #23
    Registered User Mechkov's Avatar
    Join Date: Jan:2009
    Location: internet
    Posts: 531
    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;
    }
    ">>" "<<" /. sort STL- <algorithm>. . , .

  24. #24
    Darknauss's Avatar
    Join Date: Mar:2005
    Location: Sofia/Vienna
    Posts: 2,151
    , ! ( :shy: )! push_back(k), , k. . ... :P
    Last edited by Darknauss; 6th April 2009 at 20:45.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |