how to pass a file name to my program ?

This is a discussion on how to pass a file name to my program ? within the C++ Programming forums, part of the General Programming Boards category; Dear all , I need to pass the file name to my program to get the tokens in it, my ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    40

    how to pass a file name to my program ?

    Dear all , I need to pass the file name to my program to get the tokens in it, my program still in early stage , have a look and coment please

    I have the 'code_file' . which should be passed
    And the 'bin' file which holds the reserved words and the grammars as the following

    bin.txt
    Code:
    [RESERVED_WORDS]
    NEWJOB
    CONST
    VARIABLE
    BEGINB
    ENDB
    EXIT
    
    
    [GRAMMAR]

    Code:
     #include < iostream.h>
      #include < fstream.h >
      #include < string.h  >
     
    
    //using namespace std;
    
    
     ifstream input("c:\\compiler_test\\code_file.txt"); // the code text file
     ifstream bin_file("c:\\compiler_test\\bin.txt"); // compiler information file
    
    
    // array holds the reserved words
    typedef char string[20] ;
    string reserved[20];
    
    
    
    // separators list
    // 10 the end line , 32 the space char
    // you can add new separator symbol here
    char sep_list[] = { 10,32, '.', ',', ';', ',', '+', '-', '='};
    const int is_Sep(char) ;  // returns 1 if in the sep_list ,0 if not
    
    
    
    
    // scanning functions
    void get_Reserved();
    void scan_bin();
    
    
    
    // 
    //
    //
    int main()
    {  
    	char d;
       if ( !bin_file.is_open() ) 
    	     cout << "the file couldn't be opened !" ;
    	else cout << "the file is opend successfully !" ;
    
     
    scan_bin();  // get all the information needed
    
    
      
    // printing the RESERVED_WORDS list
    cout << endl;
    for(int i =0 ; i < 20 ; i++)
    cout << reserved[i] << endl;
    
    	cout << "\n \n press any key to exit";
    	cin.get();
    
    	return 0; // main end
    }
    //----------------------------------------------------------
    //__________________________________________________________
    
    void scan_bin(){ 
    // this function scans the bin file to get all the informations
     int row=0;
     int col=0;
     char ch;
     char temp[20];
      
    
      while(!bin_file.eof())
      { bin_file.get(ch);
    
    	while ( !is_Sep(ch) & !input.eof() & ch!=-1)
    	{   temp[col++]=ch;		  
    		 bin_file.get(ch); }
    	     temp[col] = '\0';
    
    		if(!strcmp(temp , "[RESERVED_WORDS]"))
    		   get_Reserved();  // calling the reserved words scanning function
      }
    
    }
    
    
    
    void get_Reserved()
    {// the tokens scanned here should be under RESERVED_WORDS tag
     // if new tag bracket faced , the function will exit	
     int row=0;
     int col=0;
     char ch;    // temporary for scanned symbol
     char temp[20]; // temporary for scanned word
      
      while(!bin_file.eof())
      { bin_file.get(ch);
    		while ( !is_Sep(ch) & !input.eof() & ch!=-1)
    				{  if( ch=='[' | ch==']')
    						return;
    					temp[col++]=ch;		  
    					bin_file.get(ch); }
    
    		temp[col] = '\0';
    		strcpy(reserved[row] , temp);  // copying the word into the list
    		col=0;
    		row++;
    				    
      
      }
    
    }
    
    
    //--------------
    const int is_Sep(char c) 
    {  //This function checks the symbol c if in the separator 
       // using sizeof for flexible add in the array
    	for(int i=0 ; i<sizeof(sep_list) ; i++)
    		if(c==sep_list[i])
    			return 1; // in the list
     return 0; // not in the list
    }
    thanks
    Last edited by GSalah; 11-05-2006 at 08:50 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Pass the file name into main() via a command line argument.

    Code:
    int main(int argc, char* argv[])
    {
        if(argc > 1)
            std::cout << "File name: " << argv[1] << std::endl;
        else
            std::cout << "You need to pass in a file name to the program via the command line\n";
    }

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    thanks , but if did this , I can't change the file name in the input object in the run time , I dont know ??

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but if did this , I can't change the file name in the input object in the run time
    Why not?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    How can the program open many files , in many paths ?

    please note that the input object should be global , and the path should be assigned in runtime
    Last edited by GSalah; 11-07-2006 at 01:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. counting program worked, but test file CRASHES IT.. WHY?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 03:29 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21