Replace strings by another strings in a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agsrinivasan
    New Member
    • Feb 2007
    • 15

    Replace strings by another strings in a file

    hi everyone,
    i have a txt file..for eg...

    1. WA.1048/2005 M/S.R.SIVAKUMAR M/S.PAUL AND PAUL
    S.SURESH V. PARTHIBAN FOR R1
    S.SATHISHKUMAR GOVT PLEADER FOR RR 2 AND 3
    MISCELLANEOUS PETITIONS
    ~~~~~~~~~~~~~~~ ~~~~~~~~
    TO FILE A SINGLE PETITION IN WP.SR.110501/06

    2. MP.1/2007 M/S.R.BALASUBRAMA NIAM VILLUPURAM
    (PIL) S.RAMACHANDRAN
    B.N.SIVAGAMASUN DARI
    TO CONDONE THE DELAY OF 124 DAYS IN FILING OSA.SR.109501/2006

    3. MP.1/2007 M/S.S.SIVASANGARA NE
    A.T. CHANDRASEKARAN
    TO CONDONE THE DELAY OF 23 DAYS IN FILING WA.SR.6181 AND 6185/2007

    4. MP.1/2007 MR.R.SUNILKUMAR M/S.V.G.SURESHKUM AR
    & SUNDAR NARAYAN FOR RAILWAYS
    MP.1/2007
    TO CONDONE THE DELAY OF 170 DAYS IN FILING WA.SR.7072 & 7033/06

    5. MP.2/2006 GOVT. PLEADER MR.K.VASUDEVAN
    MP.2/2006

    6. TO EXTEND THE TIME FOR EXECUTION OF THE BANK GURANTEE IN SA.1638/2004
    CMP.10238/2006 M/S.N. MALA SPL.G.P.(CS)
    REVATHI MURALIDHARAN
    FOR MAINTAINABILITY
    ~~~~~~~~~~~~~~~ ~~~~

    7. LPA.SR 36917/2006 $M/S.V.SELVARAJ$
    D. JAYASINGH
    V.S.MANIMEKALAI

    first i want to replace all 1.,2.,......7. serial nos with dot into any special characters....

    i had tried.. but not yet got final logic...

    pls guide me....



    thanks

    srini
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by agsrinivasan
    hi everyone,
    i have a txt file..for eg...

    1. WA.1048/2005 M/S.R.SIVAKUMAR M/S.PAUL AND PAUL
    S.SURESH V. PARTHIBAN FOR R1
    S.SATHISHKUMAR GOVT PLEADER FOR RR 2 AND 3
    MISCELLANEOUS PETITIONS
    ~~~~~~~~~~~~~~~ ~~~~~~~~
    TO FILE A SINGLE PETITION IN WP.SR.110501/06

    2. MP.1/2007 M/S.R.BALASUBRAMA NIAM VILLUPURAM
    (PIL) S.RAMACHANDRAN
    B.N.SIVAGAMASUN DARI
    TO CONDONE THE DELAY OF 124 DAYS IN FILING OSA.SR.109501/2006

    3. MP.1/2007 M/S.S.SIVASANGARA NE
    A.T. CHANDRASEKARAN
    TO CONDONE THE DELAY OF 23 DAYS IN FILING WA.SR.6181 AND 6185/2007

    4. MP.1/2007 MR.R.SUNILKUMAR M/S.V.G.SURESHKUM AR
    & SUNDAR NARAYAN FOR RAILWAYS
    MP.1/2007
    TO CONDONE THE DELAY OF 170 DAYS IN FILING WA.SR.7072 & 7033/06

    5. MP.2/2006 GOVT. PLEADER MR.K.VASUDEVAN
    MP.2/2006

    6. TO EXTEND THE TIME FOR EXECUTION OF THE BANK GURANTEE IN SA.1638/2004
    CMP.10238/2006 M/S.N. MALA SPL.G.P.(CS)
    REVATHI MURALIDHARAN
    FOR MAINTAINABILITY
    ~~~~~~~~~~~~~~~ ~~~~

    7. LPA.SR 36917/2006 $M/S.V.SELVARAJ$
    D. JAYASINGH
    V.S.MANIMEKALAI

    first i want to replace all 1.,2.,......7. serial nos with dot into any special characters....

    i had tried.. but not yet got final logic...

    pls guide me....



    thanks

    srini
    Sure, what do you have so far?

    Comment

    • agsrinivasan
      New Member
      • Feb 2007
      • 15

      #3
      i had tried with this sourcr...

      But i got ..Sebgmentation fault...

      Code:
      #include <stdio.h>
      #include <stdlib.h>
      #include<string.h>
      int main (void) {
        
      	FILE *Input;
      	
      	long lSize;
      
              size_t result;
              
              char *buffer;
      
              Input = fopen("/home/srinivas/Desktop/cl.txt","r+");
              
               FILE *Output;
               
              Output = fopen("/home/srinivas/Desktop/sreeni.txt","w+");  
      	printf("Input value: %d\n",Input);
      
      	if (Input ==NULL) {fputs ("File error",stderr); exit (1);}
      
      	fseek (Input ,0 , SEEK_END);
       		
      	lSize = ftell (Input); printf(" The file size is = %ld\n",lSize);
              
              rewind (Input);
      
      	  // allocate memory to contain the whole file:
      	  buffer = (char*) malloc (sizeof(char)*lSize);
      	  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
      
      	  // copy the file into the buffer:
      	  result = fread (buffer,1,lSize,Input);
      
      	  long pointer,j,len,len1,z=0,i;
                   int  no=0;
      
                char string1[255];
       
                printf(" Enter the Name you want to search & put Tags\n");
      
                gets(string1);
      
                puts(string1);
      
                len = strlen(string1);
      
                printf("The length of the string = %d ",len);
      
               char string2[255];
              // int s, k = 0,x=0,l =0,y;
               
              
      
              /*char *temp;
               temp = (char*) malloc (sizeof(char)*lSize);*/
       
       	// fseek (Input ,0 , SEEK_SET);       
        
                char d,c;        
      
              
          for (i = 0; i < lSize; i++)
             {
       
      	 printf(" Inside For Loop \n");
               if( string1[i]1 < d >9 && string1[i]== '.')
      	  {
                    printf("\t \n  The value of %c =",string1[i]);
      
      		buffer[i] = '#';
                     buffer[i-1] = '#';          
                  
                  }
      		
                  d =	string1[i] ;
      
                // 
       
             }
      
      	len1 =strlen(buffer);
         fwrite(buffer,1,len1,Output);
         free(buffer);
         fclose(Input);
         fclose(Output);
      
      }
      Regards,

      sreeni
      Last edited by AdrianH; Feb 20 '07, 04:16 AM. Reason: Put [code] tag around code

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by agsrinivasan
        i had tried with this sourcr...

        But i got ..Sebgmentation fault...

        sreeni
        Ok Sreeni,

        You need to determine where the segfault is coming from. You can do this in a few ways. You can put printf's to display where you are in the code and what the variables are, or you can try using a debugger.

        I will describe how to use the debugger. You are on UNIX so you are probably using a gnu compiler. To use the debugger you have to compile with symbols. Add to your compile command line the -g switch. The command line probably starts with cc or gcc. This switch adds symbols to your executable so that you can debug it with a debugger. Now compile your source and then type at the prompt: gdb "executableName "<enter> Where "executableName " is the name of the executable, and <enter> means to press the enter or return key.

        Now we are going to trace through the programme. Here are some of the commands you can do at the gdb command line:
        • b main. This will put a breakpoint in the code, which tell the debugger to stop when it hits the main() function.
        • run. This will run the programme. It will stop excuting and bring back the prompt when it hits a break point, in this case when it enters the main() function.
        • next. This will step over a call to a function. If you are about to execute a function that is part of a library such as fopen() you want to use this. Otherwise you may be greeted with what looks like garbage. Pressing <enter> again will execute the last command you entered, so if you hit <enter> right after you typed next<enter> you will step over again.
        • step. This will step into a call. Pressing <enter> again will execute the last command you entered, so if you hit <enter> right after you typed step<enter> you will step into again.
        • print var to print out variable named var.
        • display var to print out variable named var every time you get the prompt.
        • list linenum to list the current source at that line number.
        • help for help.
        • quit to quit.

        My advice it to use next<enter> and keep pressing enter till you get your segfault. Look at the line that the fault occurred. Rerun the programme by typing run<enter> and do the next thing again up to where the problem occurred, but don't get the fault to occur. Display the variables involved in the line to be executed. If after all of this you still can't figure it out, send the last 10-20 lines you got up to the segfault, including the vars involved.

        These are basic commands used by gdb, and will help you later.

        Good luck and get back to us with your progress.


        Adrian

        Comment

        • agsrinivasan
          New Member
          • Feb 2007
          • 15

          #5
          Thank u Mr adrian...i had done the program using ..Buffers and arrays...


          Thanks for ur guidance...



          Regards,,

          srini

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Originally posted by agsrinivasan
            Thank u Mr adrian...i had done the program using ..Buffers and arrays...


            Thanks for ur guidance...



            Regards,,

            srini
            No problem, glad to help.


            Adrian

            Comment

            • thehkv
              New Member
              • Feb 2007
              • 2

              #7
              Hi, i'm having sort of same problem...
              i want to open an AVI file & replace its EXTENSIONS (as you can do in HEX-EDITOR) .
              i mean .. the extemsions in this AVI case are the 9th 10th & 11th characters.
              So i wanna change it to AAA rather than AVI . ( i need this part in my program)
              But wat program does is that it changes the AVI to AAA but all other characters in the file are blank. !! i mean .. the output file is JUST The letters AAA at the 9th 10th & 11th position.
              here's wat i did :

              void main(){
              ofstream fout("a.avi",io s::binary,ios:: out); // Open an AVI File
              int i;
              char ch;
              fout.seekp(8); // Set the ointer to 8th character ( 9th 10th & 11th
              // chars are 'A' 'V' & 'I' (the extension)
              for(i=9;i<=11;i ++)
              fout<<'A'; // change them to AAA
              getch();
              }


              what hav i done wrong !!
              i kno i suck in programming, but can some1 plzz correct it ???? thnx

              Comment

              • thehkv
                New Member
                • Feb 2007
                • 2

                #8
                i found the problem
                i shud be using

                fstream fout("a.avi",io s::binary|ios:: 0ut|ios::in);

                it works !!!! :^)

                Comment

                Working...