Picking up data from a text file

Hi guys,

  I want to write a code to fulfil the below conditions :

and in the command prompt i will be accepting the application name from the user

i have 2 .txt files in unix home directory : namely text1.txt and text2.txt
i have data like application name,purge period  (many application namesP
and in the other text file i have application name,table name and the column names

what i want to achieve is search for the given application name by the user in the text file 1
and if found, search for the table names in the text file 2 and my processsing takes care of deleting the data and taking the backup of the tables selected from the text file

how to acheive this ?

Regards,
Santosh
santoshv25Asked:
Who is Participating?
 
sunnycoderConnect With a Mentor Commented:
>and in the command prompt i will be accepting the application name from the user

>i have 2 .txt files in unix home directory : namely text1.txt and text2.txt
>i have data like application name,purge period  (many application namesP
>and in the other text file i have application name,table name and the column names

>what i want to achieve is search for the given application name by the user in the text file 1
>and if found, search for the table names in the text file 2 and my processsing takes care of
>deleting the data and taking the backup of the tables selected from the text file

fopen to open file1 ... assuming that entries are given one per line, use fgets to read a line and strcmp to compare it with the name of the application as given by asker
If more than one name is specified per line, you should use strstr instead of strcmp ... However, you may require additional checks if name of application can be a substring in another application name ....

At this point, I am not very clear about how table names in file2 relate to application name in file1, but it seems that you already have this partially coded  .... anyway, yet again you need to use strcmp or strstr to locate relevant information ...

Do not forget to close the files (fclose) before exiting
0
 
santoshv25Author Commented:
but
suppose the contents in the file1 is

ocr 100
ocr1 200
ocr3  300

and in the other file

ocr  table1 col1
ocr table1 col2
ocr table2 col1
ocr table2 col2

after searching for ocr in the 2nd text file, how to go to pick up the table names and column names ?
i need coding help
i need the above 4 lines from the 2nd text file for ocr database for example
0
 
sunnycoderCommented:
Can you elaborate a bit ?

In the example that you gave, ocr was the input to be searched - right ? You search in file 1 and list all entried which have ocr ... What next ?
0
Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

 
santoshv25Author Commented:
ok.
what i want to do is
input is ocr and search for ocr in text file 2 and my program logic is take one by one all the tables for the ocr from which the data is to be deleted.

i hope it is clear now.
0
 
sunnycoderCommented:
>input is ocr and search for ocr in text file 2 and my program logic is
>take one by one all the tables
What tables? ... You did not mention any tables in file1 or file2

>for the ocr from which the data is to be deleted.
what data ?

be more specific ... Post sample input files and expected input and output
0
 
santoshv25Author Commented:
suppose the contents in the file1 is

ocr 100
ocr1 200
ocr3  300

and in the other file(file2)

ocr  table1 col1
ocr table1 col2
ocr table2 col1
ocr table2 col2

the first logic is the input..input is ocr that i will search in file1 and i get it
from there i go to file2 and search for ocr.
then in a loop, i want to search for tables from which i have to delete data (table1,table2,table3,table4) will be the tables i have mentioned
0
 
sunnycoderCommented:
>input is ocr that i will search in file1 and i get it
all lines in file1 have substring ocr ... so all lines match ?

>from there i go to file2 and search for ocr.
What was the point of searching file1 ... or is that both are unrelated ... again all lines in file2 match !!!

>then in a loop, i want to search for tables from which i have to delete data
>(table1,table2,table3,table4) will be the tables i have mentioned
The tables are in file1 or file2 or in some other file ?

Either case, the basic strategy remains the same

"fopen to open file1 ... assuming that entries are given one per line, use fgets to read a line and strcmp to compare it with the name of the application as given by asker
If more than one name is specified per line, you should use strstr instead of strcmp ... However, you may require additional checks if name of application can be a substring in another application name ....

for file2, yet again you need to use strcmp or strstr to locate relevant information ...

Do not forget to close the files (fclose) before exiting"
0
 
santoshv25Author Commented:
but my doubt is
how to get the table names ? i mean how to move about in the text file to get the table names ?
0
 
sunnycoderCommented:
>ocr  table1 col1

when you are reding file2

while ( fgets (line, MAX_LENGTH, file2) != NULL )
{
        temp1 = strchr ( line, ' ' );
        temp1++;
        temp2 = strchr ( temp1, ' ' );
        temp2--;
        /* now string between temp1 and temp2 (both inclusive) is your table name */
}
0
 
santoshv25Author Commented:
but suppose temp1 contains 10
and temp2 contains 20, how to access the table name ?
please can u guide  me in this ?

and the length of the file names is not fixed
 can be 10,20 etc
will your logic take care of the spaces after that

ocr table1 col1
ocr table22 col2
ocr table21111 col3

0
 
santoshv25Author Commented:
the code by sunnycoder is not working for me.
the value of temp1 and temp2 are big junk values for me..

could anyone give me the code
on how to get the table names from the file after searching for the database name ?

very urgent
0
 
sunnycoderCommented:
>the code by sunnycoder is not working for me.
>the value of temp1 and temp2 are big junk values for me..

May I see the code that you executed
0
 
santoshv25Author Commented:
here is the code :
please help me out where is the problem and how to get exactly the table name

  FILE *I_fp;                                                                
  int temp1,temp2;                                                          
  char line[21];                                                            
                                                                             
  I_fp = fopen("bkp_inf_file","r");                                          
  while ( fgets (line, 20, I_fp) != NULL )                                  
  {                                                                          
          temp1 = strchr ( line, ' ' );                                      
          temp1++;                                                          
          temp2 = strchr ( temp1, ' ' );                                    
          temp2--;                                                          
          /* now string between temp1 and temp2 is the table name */        
  }                                                                          
0
 
sunnycoderCommented:
>the value of temp1 and temp2 are big junk values for me..
How did you determine this? ... I do not see any extraction of table name
0
 
santoshv25Author Commented:
i mean the value of temp1 and temp2 when i print out the value using sprintf..
 sprintf(gs_info_log,"Check temp1 and temp 2  %d %d",temp1, temp2);  

0
 
sunnycoderCommented:
temp1 and temp2 are char pointers
0
 
santoshv25Author Commented:
ok..
that is fine..
but now how to find out the table names using that ?
could u give me the code for that if u don't mind ?

0
 
sunnycoderCommented:
strncpy ( fn, temp1, temp2-temp1+1 );

make sure you null terminate fn
0
 
santoshv25Author Commented:
fn is a character variable right ?
0
 
sunnycoderCommented:
fn - an array of characters in which you wish to store the filename or a char * with enough memory
0
 
santoshv25Author Commented:
the below code gives 2 times the name of the files
could u correct it ?

{                                                                              
  FILE *I_fp;                                                                  
  char *temp1,*temp2;                                                          
  char line[25];                                                                
  char tbl_name[15];                                                            
                                                                               
  I_fp = fopen("bkp_inf_file","r");                                            
  while ( fgets (line, 24, I_fp) != NULL )                                      
  {                                                                            
          temp1 = strchr ( line, ' ' );                                        
          temp1++;                                                              
          temp2 = strchr ( temp1, ' ' );                                        
          temp2--;                                                              
  strncpy ( tbl_name, temp1, temp2-temp1+1 );                                  
  strcat(tbl_name,'\0');                                                        
  printf("%s\n",tbl_name);                                                      
  getchar();                                                                    
          /* now string between temp1 and temp2 is the table name */            
  }                                                                            
                                                                               
  fclose(I_fp);                                                                
0
 
sunnycoderCommented:
strncpy ( tbl_name, temp1, temp2-temp1+1 );
 tbl_name [temp2-temp1+1] = '\0'
0
 
santoshv25Author Commented:
still the same result..
2 times the table names gets displayed


  char *temp1,*temp2;                                                          
  char line[30];                                                              
  char tbl_name[15];                                                          
                                                                               
  I_fp = fopen("bkp_inf_file","r");                                            
  while ( fgets (line, 29, I_fp) != NULL )                                    
  {                                                                            
          temp1 = strchr ( line, ' ' );                                        
          temp1++;                                                            
          temp2 = strchr ( temp1, ' ' );                                      
          temp2--;                                                            
  strncpy ( tbl_name, temp1, temp2-temp1+1 );                                  
  tbl_name[temp2-temp1+1] ='\0';                                              
  printf("%s\n",tbl_name);                                                    
          /* now string between temp1 and temp2 is the table name */          
  }                                                                            
                                                                               
  fclose(I_fp);                                                                
  return TRUE;                                                                
}                                                                              
0
 
sunnycoderCommented:
>2 times the table names gets displayed
Are you sure you do not have 2 printfs
0
 
santoshv25Author Commented:
only 1 printf exists....
the code i have given to u


0
 
sunnycoderCommented:
works fine for me ... what does strlen(tbl_name) give you
0
 
santoshv25Author Commented:
the output of the program is below

trading_fact_a 14                        
trad_ship_fact_s 16                      
trad_ship_fact_s     16                  
trad_bill_fact_s 16                      
trad_bill_fact_s     16                  
finance_fact_fin 18                       the actual table name is finance_fact_fin_s
finance_fact_fin     16                  
finance_fact_rvn 18                       the actual table name is finance_fact_rvn_s
finance_fact_rvn     16                  

reply as soon as possible. it is urgent
0
 
santoshv25Author Commented:
the output now is

trading_fact_a 14                                  
trad_ship_fact_s 16                                
trad_bill_fact_s 16                                
finance_fact_fin_s 18                              
finance_fact_fin_s 18                              
finance_fact_rvn_s 18                              
finance_fact_rvn_s 18                              

the length is right now
but now the only problem is why it is coming twice ?

the actual code is below :

  FILE *I_fp;                                                                  
  char *temp1,*temp2;                                                          
  char line[31];                                                                
  char tbl_name[18];                                                            
                                                                               
  I_fp = fopen("bkp_inf_file","r");                                            
  while ( fgets (line, 30, I_fp) != NULL )                                      
  {                                                                            
          temp1 = strchr ( line, ' ' );                                        
          temp1++;                                                              
          temp2 = strchr ( temp1, ' ' );                                        
          temp2--;                                                              
  strncpy ( tbl_name, temp1, temp2-temp1+1 );                                  
  tbl_name[temp2-temp1+1] ='\0';                                                
  printf("%s %d\n",tbl_name,strlen(tbl_name));                                  
          /* now string between temp1 and temp2 is the table name */            
  }                                                                            
                                                                               
  fclose(I_fp);                                                                
0
 
sunnycoderCommented:
It either occurs twice in the file or you are printing it twice ... I see only s single print in the code, so check the file
0
 
santoshv25Author Commented:
the contents of the file2 is :

OCR trading_fact_a frag_id          
OCR trad_ship_fact_s frag_id        
OCR trad_bill_fact_s frag_id        
OCR finance_fact_fin_s frag_id      
OCR finance_fact_rvn_s frag_id      
0
 
sunnycoderCommented:
Run it through a debugger
0
All Courses

From novice to tech pro — start learning today.