473,549 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replace strings by another strings in a file

agsrinivasan
15 New Member
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
Feb 19 '07 #1
7 3722
sicarie
4,677 Recognized Expert Moderator Specialist
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?
Feb 19 '07 #2
agsrinivasan
15 New Member
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 Recognized Expert Top Contributor
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
15 New Member
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 Recognized Expert Top Contributor
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 New Member
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
Feb 28 '07 #7
thehkv
2 New Member
i found the problem
i shud be using

fstream fout("a.avi",io s::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
3711
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
5223
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 values of different non-pre-defined tags. What i try to say is that the whole thing should work in a generic way, independant from the DTD of...
4
4653
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 "mno" with another value. Of course the brute-force approach is straight forward. Just iterate over the full string N times (once for "abc",...
5
2621
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* replace){ char* result; size_t l = strlen(source), r = strlen(replace), i; int number_of_replaces = 0; for(i = 0; i < l; i++){ if(source == search)
6
14624
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 special character 'ž' in a string (retreived by an SQL). After that I would like to update the field with the corrected string.
5
3387
by: peter | last post by:
Hello all, I'm looking for an advice. Example (one block in ascii file): $------------------------ NAME='ALFA' CODE='x' $------------------------
3
16909
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 with: import fileinput, string, sys fileQuery = "Text.txt" sourceText = '''SOURCE'''
5
8333
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 input file should be retained in output file. And, if possible we should be able to replace multiple search strings with multiple replace strings. e.g;...
10
18613
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 parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The...
0
7532
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7461
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7730
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7491
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3509
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1068
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.