473,395 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Replace strings by another strings in a file

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.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
7 3711
sicarie
4,677 Expert Mod 4TB
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
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
1,251 Expert 1GB
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
Thank u Mr adrian...i had done the program using ..Buffers and arrays...


Thanks for ur guidance...



Regards,,

srini
Feb 22 '07 #5
AdrianH
1,251 Expert 1GB
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
thehkv
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
thehkv
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

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

Similar topics

1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
1
by: Marco Leist | last post by:
hello, i“d like to replace certain strings in a XML-Document with other ones. Thereby the structure (tags)of the source-xml should be preserved completly, the strings that should be replaced are...
4
by: spam | last post by:
Is there a well-known algorithm for replacing many substrings in a string? For example, I'd like to take the string "abc def ghi jkl mno pqr" and replace, say, every instance of "abc", "ghi", and...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
6
by: Peter.Laganis | last post by:
Hi to all, I apologize in advance if a similar question was already posted, but I didn't find it. Here is my problem: I would like to replace all the special characters ' ¾' with another...
5
by: peter | last post by:
Hello all, I'm looking for an advice. Example (one block in ascii file): $------------------------ NAME='ALFA' CODE='x' $------------------------
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
5
by: neeludhiman | last post by:
Hi All, Can someone please help me with the code in C / C++ to find a string in an input text file and replace it with another string in output text file. The catch is that white spaces in the...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.