Solved

Picking up data from a text file

Posted on 2004-04-18
34
207 Views
Last Modified: 2010-07-27
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
0
Comment
Question by:santoshv25
  • 16
  • 15
34 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 20 total points
ID: 10857123
>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
 

Author Comment

by:santoshv25
ID: 10865175
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10865828
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:santoshv25
ID: 10876575
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10876637
>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
 

Author Comment

by:santoshv25
ID: 10876693
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10876743
>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
 

Author Comment

by:santoshv25
ID: 10884844
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10885815
>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
 

Author Comment

by:santoshv25
ID: 10886266
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
 

Author Comment

by:santoshv25
ID: 10886462
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10886557
>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
 

Author Comment

by:santoshv25
ID: 10886615
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10886646
>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
 

Author Comment

by:santoshv25
ID: 10886668
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10886686
temp1 and temp2 are char pointers
0
 

Author Comment

by:santoshv25
ID: 10886698
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10886748
strncpy ( fn, temp1, temp2-temp1+1 );

make sure you null terminate fn
0
 

Author Comment

by:santoshv25
ID: 10886779
fn is a character variable right ?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10886819
fn - an array of characters in which you wish to store the filename or a char * with enough memory
0
 

Author Comment

by:santoshv25
ID: 10886915
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10886957
strncpy ( tbl_name, temp1, temp2-temp1+1 );
 tbl_name [temp2-temp1+1] = '\0'
0
 

Author Comment

by:santoshv25
ID: 10887085
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10887141
>2 times the table names gets displayed
Are you sure you do not have 2 printfs
0
 

Author Comment

by:santoshv25
ID: 10887153
only 1 printf exists....
the code i have given to u


0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10887182
works fine for me ... what does strlen(tbl_name) give you
0
 

Author Comment

by:santoshv25
ID: 10895479
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
 

Author Comment

by:santoshv25
ID: 10895518
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10896795
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
 

Author Comment

by:santoshv25
ID: 10896816
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10896834
Run it through a debugger
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question