Hello All

A question i have.

I have a file which contains 5 million records.

One line of this file may look something like below

Adrian Tompson & Sons Ltd.

If the last word in each line is Ltd, i need to change it to Limited, but i am truly stuck.

What is the best way to find out the last word in this string? Is it strtok?


Any help would be much appreciated.

Best Regards

Recommended Answers

All 5 Replies

Use functions of string class. find() and replace()-Link

This is what i have.

I can successfully loop each line and find if the last word on line is LTD.

But how would i go about updating this file with "Limited", how do i work all this out??

int LRecL=359; /* Chars on each line

/* Read records from stdin */
while ( (RecsRead = fread(Block, LRecL , BlockRecs, stdin)) != 0 )
{
   printf("About to process block of records\n" );
 
   Working_ptr=Block;
 
   while(Working_ptr)
   {
      memcpy(comp_name,Working_ptr, InSchStruct[0].field_length );
 
      memcpy(full_line,Working_ptr,LRecL );
 
      len=strlen(comp_name);
 
      if (len == 0)
         break;
 
      else
         printf("Company Name = %s\n", comp_name );
 
      Working_ptr+=LRecL;
 
      pos+=LRecL;
 
      first_ptr = strtok (comp_name, delims ); 
 
      /* Find out how many words are on first line */
 
      while(first_ptr)
      { 
         word_count++;
 
         second_ptr=first_ptr;
 
         first_ptr=strtok(first_ptr+strlen(first_ptr)+1, delims ); 
 
       } 
       word_count=0;
 
       if (strcmp(second_ptr, "LTD") == NULL )
       {
          printf("Found.... %s\n", second_ptr);
 
       } 
 
   }
 
}

Im using C, are you talking about c++

here is my output

21-08-2006 11:25:37 cdmssjg 6213 template(v1) Allocated "2096919" byte(s) "5841" records(s) for block

InSchStruct[field].field_id = 3027
InSchStruct[field].field_length = 80
About to process block of records
Company Name = MARINE AND GENERAL MUTUAL LIFE ASSURANCE SOCIETY
Company Name = RAILWAY PASSENGERS ASSURANCE COMPANY
Company Name = KENTSTONE PROPERTIES PLC
Company Name = ASHFORD CATTLE MARKET COMPANY LIMITED(THE)
Company Name = ORIENTAL GAS COMPANY, LIMITED(THE)
Company Name = BRITISH INDIA STEAM NAVIGATION COMPANY LIMITED
Company Name = N & C BUILDING PRODUCTS LIMITED
Company Name = UNION MARINE AND GENERAL INSURANCE COMPANY LIMITED,(THE)
Company Name = METHODIST NEWSPAPER COMPANY LIMITED
Company Name = LONDON AND SUBURBAN LAND AND BUILDING COMPANY LIMITED(THE)
Company Name = BORNEO COMPANY LIMITED(THE)
Company Name = MILLBAY HOTEL LIMITED
Company Name = SURREY GROUP LIMITED
Company Name = UK HOUSING SOCIETY LIMITED
Company Name = B.L.HOLDINGS LIMITED
Company Name = REIGATE INVESTMENT COMPANY LIMITED (THE)
Company Name = ABACO INVESTMENTS LIMITED
Company Name = MOUNTLEIGH GROUP PUBLIC LIMITED COMPANY
Company Name = CENTURION SAFETY PRODUCTS LTD
Found.... LTD

Yes, using C++ string member functions: find() and replace() will make your solution easy and elegant. It's a littel bit harder in C.
I would open one file for reading and one file for writing. I would read original file line by line, perform changes if needed and write line to a new file.
Since there is 5 million records in your file I assume file is large and maybe using two files would not be a good solution, but i can't think of anymore else right now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.