By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 818 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

Replace strings by another strings in a file

agsrinivasan
P: 15
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.BALASUBRAMANIAM VILLUPURAM
(PIL) S.RAMACHANDRAN
B.N.SIVAGAMASUNDARI
TO CONDONE THE DELAY OF 124 DAYS IN FILING OSA.SR.109501/2006

3. MP.1/2007 M/S.S.SIVASANGARANE
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.SURESHKUMAR
& 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
Feb 19 '07 #1
Share this Question
Share on Google+
7 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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.BALASUBRAMANIAM VILLUPURAM
(PIL) S.RAMACHANDRAN
B.N.SIVAGAMASUNDARI
TO CONDONE THE DELAY OF 124 DAYS IN FILING OSA.SR.109501/2006

3. MP.1/2007 M/S.S.SIVASANGARANE
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.SURESHKUMAR
& 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?
Feb 19 '07 #2

agsrinivasan
P: 15
i had tried with this sourcr...

But i got ..Sebgmentation fault...

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4. int main (void) {
  5.  
  6.     FILE *Input;
  7.  
  8.     long lSize;
  9.  
  10.         size_t result;
  11.  
  12.         char *buffer;
  13.  
  14.         Input = fopen("/home/srinivas/Desktop/cl.txt","r+");
  15.  
  16.          FILE *Output;
  17.  
  18.         Output = fopen("/home/srinivas/Desktop/sreeni.txt","w+");  
  19.     printf("Input value: %d\n",Input);
  20.  
  21.     if (Input ==NULL) {fputs ("File error",stderr); exit (1);}
  22.  
  23.     fseek (Input ,0 , SEEK_END);
  24.  
  25.     lSize = ftell (Input); printf(" The file size is = %ld\n",lSize);
  26.  
  27.         rewind (Input);
  28.  
  29.       // allocate memory to contain the whole file:
  30.       buffer = (char*) malloc (sizeof(char)*lSize);
  31.       if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
  32.  
  33.       // copy the file into the buffer:
  34.       result = fread (buffer,1,lSize,Input);
  35.  
  36.       long pointer,j,len,len1,z=0,i;
  37.              int  no=0;
  38.  
  39.           char string1[255];
  40.  
  41.           printf(" Enter the Name you want to search & put Tags\n");
  42.  
  43.           gets(string1);
  44.  
  45.           puts(string1);
  46.  
  47.           len = strlen(string1);
  48.  
  49.           printf("The length of the string = %d ",len);
  50.  
  51.          char string2[255];
  52.         // int s, k = 0,x=0,l =0,y;
  53.  
  54.  
  55.  
  56.         /*char *temp;
  57.          temp = (char*) malloc (sizeof(char)*lSize);*/
  58.  
  59.      // fseek (Input ,0 , SEEK_SET);       
  60.  
  61.           char d,c;        
  62.  
  63.  
  64.     for (i = 0; i < lSize; i++)
  65.        {
  66.  
  67.      printf(" Inside For Loop \n");
  68.          if( string1[i]1 < d >9 && string1[i]== '.')
  69.       {
  70.               printf("\t \n  The value of %c =",string1[i]);
  71.  
  72.         buffer[i] = '#';
  73.                buffer[i-1] = '#';          
  74.  
  75.             }
  76.  
  77.             d =    string1[i] ;
  78.  
  79.           // 
  80.  
  81.        }
  82.  
  83.     len1 =strlen(buffer);
  84.    fwrite(buffer,1,len1,Output);
  85.    free(buffer);
  86.    fclose(Input);
  87.    fclose(Output);
  88.  
  89. }
  90.  
Regards,

sreeni
Feb 20 '07 #3

AdrianH
Expert 100+
P: 1,251
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
Feb 20 '07 #4

agsrinivasan
P: 15
Thank u Mr adrian...i had done the program using ..Buffers and arrays...


Thanks for ur guidance...



Regards,,

srini
Feb 22 '07 #5

AdrianH
Expert 100+
P: 1,251
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
Feb 23 '07 #6

P: 2
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",ios::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
Feb 28 '07 #7

P: 2
i found the problem
i shud be using

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

it works !!!! :^)
Mar 1 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.