CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2013
    Posts
    2

    enter the file name by keyboard instead of command line arguments?

    Actually the below program is for Dispersal Algorithm called Rabin-IDA; this algorithm divided the data into N pieces and then recombine it from M pieces (such that M<N).

    Thus, the below program needs command line arguments,which entering by Project properties/Debugging. this argument is file name, where the program performing spitted the file into N files, and then recombine it from M divided files, and put it on another file which should also passing its name as argument .

    Now my question is, How can i make this program enter the file name by keyboard??(i mean enter the files name by user from screen not as command line arguments)
    In another word, How I can exchange
    Code:
    argc == 3
    and
    Code:
    argc == 2
    to enter file name ? i mean what i should do to in
    Code:
    rabin.split(argv[1])
    to pass my file name by use keyboard not Project properties/Debugging?)

    the below code is just the main function of program, and the whole of it in this link (http://www.juancamilocorena.com/home/projects) Information Dispersal Algorithms Rabin-IDA.


    Code:
    #include "include.h"
    
    void __cdecl _tmain(int argc, TCHAR *argv[])
    {
    DWORD ini=GetTickCount();
    try
    {
    if( argc == 3 ) //recombine
    {
    RabinIDA rabin=RabinIDA(17,10);
    long long size=GetFileSize(argv[1]);
    int f[]={0,2,3,5,6,8,9,11,14,15};
    rabin.recombine(argv[1],
    f,
    argv[2],
    size);
    }
    else if(argc == 2)
    {
    RabinIDA rabin=RabinIDA(17,10);
    rabin.split(argv[1]);
    }
    else
    {
    printf("Error. To split a file pass a parameter with the file to be splitted\n");
    printf("To recombine the file give the name of the original file and the output file\n");
    printf("The name of the file is used to get the size of the original file only, in a production\n");
    printf("environment the length of the original file and the id of the share must be stored along with the share");
    return;
    }
    printf("%d\n",GetTickCount()-ini);
    }
    catch (int)
    {
    PrintLastError(_T("MAIN CATCH"));
    }
    }

    i know i should use getline() function but how exchange argv[] ?

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: enter the file name by keyboard instead of command line arguments?

    I'm not entirely sure what you want. But is this what you mean where the user is asked for the information after the program is started?

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    char	opt;
    
    	cout << "Split(S) or Recombine(R): ";
    	cin >> opt;
    	cin.ignore(1000, '\n');
    
    	switch (tolower(opt)){
    		case 'r':
    			{
    			string	orignam,
    				outnam;
    
    			cout << "Name of original file: ";
    			cin >> orignam;
    			cin.ignore(1000, '\n');
    
    			cout << "Name of output file: ";
    			cin >> outnam;
    			cin.ignore(1000, '\n');
    
    			RabinIDA rabin=RabinIDA(17,10);
    			long long size=GetFileSize(orignam.c_str());
    			int f[]={0,2,3,5,6,8,9,11,14,15};
    			rabin.recombine(orignam.c_str(), f, outnam.c_str(), size);
    			}
    			break;
    
    		case 's':
    			{
    			string splitnam;
    
    			cout << "Name of file to split: ";
    			cin >> splitnam;
    			cin.ignore(1000, '\n');
    
    			RabinIDA rabin=RabinIDA(17,10);
    			rabin.split(splitnam.c_str());
    			}
    			break;
    
    		default:
    			printf("Error. To split a file pass a parameter with the file to be splitted\n");
    			printf("To recombine the file give the name of the original file and the output file\n");
    			printf("The name of the file is used to get the size of the original file only, in a production\n");
    			printf("environment the length of the original file and the id of the share must be stored along with the share");
    			return (1);
    	}
    	return (0);
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured