I hope you can now , I think the problem in getting the line , when I'm using
bin_file.getline(temp,30);
the finction elemenate the '['
I''m using Visual C++ 6.0
sorry for this .. just copy and past
Code:
#include < iostream.h>
#include < fstream.h >
#include < string.h >
//using namespace std;
ifstream input("c:\\compiler_test\\Data_file.txt"); // the code text file
ifstream bin_file("c:\\compiler_test\\bin.txt"); // compiler information file
//this for RESERVED_WORDS
const int reserved_size=40;
typedef char string[20] ; // array holds the reserved word
string reserved[reserved_size]; // holds reserved_size reserved words
int reserved_ID[reserved_size]; // holds the corsponding ID for each reserved word
//this for SYMBOLS
const int symbols_size=40;
typedef char symbol[3]; //max symbol length is 2 characters
symbol symbols_list[symbols_size];
int symbols_ID[symbols_size];
//the following ID list set by the functio set_ID()
int Names_ID; // the ID of the names
int Reserved_ID; // the starting ID for reserved_ID list
int Symbols_ID; // the starting ID for the symbol_ID list
// 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 get_Symbols();
void get_Tokens();
void scan_bin();
void set_ID();
//
//
//
int main(int argc, char* argv[])
{
if(argc > 1)
{cout << "You need to pass in a file name to the program via the command line\n";
cin.get();
return 1;}
else{
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 < reserved_size ; i++)
cout << symbols_list[i] << " - " << symbols_ID[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 information inside
int row=0;
int col=0;
char ch;
char temp[20];
/*while(!bin_file.eof())
{ bin_file.get(ch);
col=0;
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
continue;}
if(!strcmp(temp , "ID]"))
set_ID();
}*/
while(!bin_file.eof())
{ bin_file.getline(temp ,40);
if(!strcmp(temp , "[RESERVED_WORDS]"))
get_Reserved(); // calling the reserved words scanning function
if(!strcmp(temp , "[SYMBOLS]"))
get_Symbols(); // calling the reserved symbols scanning function
if(!strcmp(temp , "ID]"))
set_ID(); // this function sets all the data related to the IDs
}
}
void get_Symbols()
{// the symbols scanned here should be under SYMBOLS tag in bin_file
// and saved the corsponding ID for each word in reserved_id array
// if new tag bracket faced , the function will exit
int row=0;
int col=0;
char ch; // temporary char for scanned character
char temp[3]; // temporary array for scanned symbol
while(!bin_file.eof())
{ bin_file.get(ch);
while ( ch!=10 & !input.eof() & ch!=-1)
{ if( ch=='[' | ch==']')
return;
temp[col++]=ch;
bin_file.get(ch); }
temp[col] = '\0';
strcpy(symbols_list[row] , temp); // copying the word into the list
col=0;
row++;
}
}
//----------------------------------
void get_Reserved()
{// the tokens scanned here should be under RESERVED_WORDS tag in bin_file
// and saved the corsponding ID for each word in reserved_id array
// if new tag bracket faced , the function will exit
int row=0;
int col=0;
char ch; // temporary for scanned character
char temp[20]; // temporary for scanned word
while(!bin_file.eof())
{ bin_file.get(ch);
while ( ch!=10 & !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++;
}
}
//----------------------------------
void get_Tokens()
{int row=0;
int col=0;
char ch;
char temp[20];
while(!bin_file.eof())
{ bin_file.get(ch);
while (ch!=-1 & ch!=' ' & ch!='+' & ch!='.' & ch!=10 & !input.eof())
{ temp[col++]=ch;
bin_file.get(ch);
}
temp[col] = '\0';
strcpy(reserved[row] , temp); // copy the word to 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
}
//-----------------
void set_ID()
{//here we get the ID for each tokens type that under [ID] tag
// if you want to add a tag , you should add it in the ID list
int col=0 ;
int row= 0;
int index;
char temp[20];
int ID_value=0;
char ch;
char line[51];
while(!bin_file.eof())
{ bin_file.get(ch);
while ( !input.eof() & ch!=-1 &ch!=10)
{ if( ch=='[' | ch==']')
break;
line[col++]=ch;
bin_file.get(ch); }
line[col] = '\0';
for(index = 0 ;line[index]!='=' ; index++)
temp[index] = line[index];
temp[index++]='\0';
ID_value=0;
for(; line[index]!='\0' ; index++)
{ID_value*=10;
ID_value+=line[index] - 48;}
col=0;
row++;
// assigning values to ID list
if(!strcmp(temp,"RESERVED_WORDS"))
Reserved_ID = ID_value;
else if(!strcmp(temp,"SYMBOLS"))
Symbols_ID=ID_value;
else if(!strcmp(temp,"NAMES"))
Names_ID=ID_value;
}
// setting the corrospnding reserved_ID values
for(index =0 ; index <reserved_size ; index++)
{reserved_ID[index] = Reserved_ID + index;
symbols_ID[index] = Symbols_ID + index;
}
}
thanks for interest