How to replace a line in a file with a range of lines found after the replacement line using Perl. Thanks in advance,
Faez Shingeri
Posts
-
Replace a line with a range of lines in a file using Perl -
Video Display hangs in Windows after unlockingMy code is in C++.
-
Video Display hangs in Windows after unlockingHi, Can anyone please help me fix the issue of video display hanging on Windows? The issue occurs after both manual and automatic locking of Windows. When I unlock, the video display hangs, but sound is heard and video packets are sent and recieved properly(confirmed from wireshark) Code works completely fine on Linux. Thanks in advance, Faez
-
How to insert a "#" to some lines ?while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1))
{
buff_ptr = buffer;
strcpy(buffer2,buffer);
//Remove Spaces from start for comparision
i = 0;
while( i++ < strlen(buffer))
{
if(isspace(buffer[i]))
continue;
strcpy(buffer2,&(buffer[i]));
break;
}
if(strncmp(buffer2,"READ",4) == 0)
{
found = true;
sprintf(buffer1,"# %s",buffer);
fputs(buffer1,fp2);
}
else if(found == false)
{
fputs(buffer,fp2);
}
else
{
sprintf(buffer1,"# %s",buffer);
fputs(buffer1,fp2);
// Search if "." is found.
if(strstr(buffer,".") != NULL)
found = false;
}
}Alot of thanks to Chandrakantt also in helping me in this one..
-
How to insert a "#" to some lines ?It's working great now .. :-D Thnx alot for help
-
How to insert a "#" to some lines ?Nothing helped... Tried alot of trial and error but the o/p refuses to roll out as expected.. :^)
-
How to insert a "#" to some lines ?There are no changes in the new file at all :| Also, there are multiple instance of READ such as END-READ etc whose line should not be precceded with # I aint understanding how the current file pointer can be incremented till a "\n" or "." :confused:
-
How to insert a "#" to some lines ?I shall henceforth post the code instantly while asking a new question next time:) BTW here is my code and I think am lost... :sigh:
#include #include #define MAX_LEN_SINGLE_LINE 120
#define bufsize 1024int main()
{char fileOrig\[32\] = "test.txt"; char fileRepl\[32\] = "myReplacedFile.txt"; const char text2find\[80\] = " READ "; const char text2repl\[80\] = "# READ "; int c, line=0; char buffer\[MAX\_LEN\_SINGLE\_LINE+2\]; char \*buff\_ptr, \*find\_ptr, \*tok; FILE \*fp1, \*fp2; size\_t find\_len = strlen(text2find); fp1 = fopen(fileOrig,"r+"); fp2 = fopen(fileRepl,"w+"); int i; while(fgets(buffer,MAX\_LEN\_SINGLE\_LINE+2,fp1)) { buff\_ptr = buffer; while ((find\_ptr = strstr(buff\_ptr,text2find))) { while ((c = fgetc(fp1)) == '.') { if (c == '\\n') fputc("#",fp2); else ; } while(buff\_ptr < find\_ptr) fputc((int)\*buff\_ptr++,fp2); fputs(text2repl,fp2); buff\_ptr += find\_len; } fputs(buff\_ptr,fp2); } fclose(fp2); fclose(fp1); return 0;
}
Thanks in advance, Faez
-
How to insert a "#" to some lines ?Need to code it in C :)
-
How to insert a "#" to some lines ?Hi, Is there a built-in string function to insert a # to all the lines which starts with string READ and ends with .(period) Eg:abc.txt
asadf
sdf
READ abc asdj ads //This lines needs to be preeded with #
asdkl asdj //This lines needs to be preeded with #
djshf . //This lines needs to be preeded with #EOF
followed by substituion of a string.. Thanks in advance
-
How to retieve files from a folder and pass it in a functio called in a loopThat worked :-) BTW I came up another method as below
system("copy input/*.txt "); // This line copies all the text files to pwd
/* After manipulations with all the required files.... */
system("del *.txt");
Anyways, thanks for the nice logic :) Regards, Faez
-
How to retieve files from a folder and pass it in a functio called in a loopHi, I have an array of strings which contains the file names and I call a function in a loop.
char myFiles [20][50];
for(i=0;i<20;i++)
myFunc( myFiles[i]);The challenge I am facing is that, I have to store all the files in a directory, say "input" folder.... and then call
myFunc( myFiles[i]);
which I am unable to do. I used
mkdir("input");
to create a directory and move all my files into it also using
system("move *gen.txt input");
But now if I give that path in my function it has to be hardcoded which eventually cannot be run in a loop. ie
for(i=0;i<=20;i++)
myFunc("C:\\input\\myFiles[i]"); //which is not possible....I am using eclipse IDE. Thanks in advance, Faez
-
How to re-use the replace() function to replace different strings in the same file ?Sample File to be parsed and replace different strings..
*************************************************************************
* API TEMPLATE PROGRAM *
* THIS PROGRAM SERVES AS A TEMPLATE TO GENERATE THE ACTUAL API PROGRAM *
* *
* MAPPING RULES: *
* -------------------------------------------------------------------- *
* Variable Name Replace with *
* -------------------------------------------------------------------- *
* &apiname API Name from Mapping Repository *
* &dclgen DCLGEN name from Mapping Repository *
* &keyinfo Key field column name from file layout *
* &hostvarprimary Key field host var name from DCLGEN *
* &tablecols Table column name from DCLGEN *
* &tablehostvars Host variable names from DCLGEN *
* &tblprimary Key field column name from DCLGEN *
* &tablehostvars01 01 level of host var from DCLGEN *
*************************************************************************
/* strings to be replaced if present in the above block should be ignored */
IDENTIFICATION DIVISION.
PROGRAM-ID. &apiname. /* &apiname string shud be replaced with another string */EXEC SQL INCLUDE &dclgen END-EXEC. EXEC SQL INCLUDE SQLCA END-EXEC. 2100-SELECT-PARA. MOVE &keyinfo TO &hostvarprimary EXEC SQL SELECT &tablecols /\* &tablecols to be replaced with another string \*/ INTO &tablehostvars /\*&tablehostvars to be replaced with another string \*/ FROM &tblname WHERE &tblprimary = &hostvarprimary END-EXEC IF SQL-CODE EQUAL ZERO SET FUNC-SUCCESS TO TRUE MOVE &tablehostvars01 TO VSAM-REC-BLOCK ELSE PERFORM 9999-ABEND-PARA END-IF. 2100-SELECT-PARA-EXIT. EXIT. 2200-INSERT-PARA. MOVE VSAM-REC-BLOCK TO &tablehostvars01 /\* This &tablehostvars01 string SHOULD NOT be replaced.. \*/ EXEC SQL INSERT INTO &tblname ( &tablecols ) VALUES ( &tablehostvars ) END-EXEC IF SQL-CODE EQUAL ZERO SET FUNC-SUCCESS TO TRUE ELSE PERFORM 9999-ABEND-PARA END-IF. 2200-INSERT-PARA-EXIT. EXIT. 2300-UPDATE-PARA. MOVE VSAM-REC-BLOCK TO &tablehostvars01 /\* shud not be replaced \*/ EXEC SQL
-
How to re-use the replace() function to replace different strings in the same file ?I thought rewind will help me during the reuse of the file.. :~ The replace function doesnot check for the entire string :^) I mean.. it replaces all the strings like &tabhostvars and &tabhostvars01 (I do not want this string to be replaced..) I only want &tabhostvars strings to be replaced Can I do this..?? #vaguequestionButlearning :)
while ((find_ptr = strstr(buff_ptr,text2find)) && find_ptr+find_len+1 == NULL)
Thanks, Faez
-
How to re-use the replace() function to replace different strings in the same file ?Hi, I have a replace function which replaces all the occurences of the string into another file. But when I use the replace function multiple times... it replaces the string which was passed in the last call only and all the previous replacement does not happen. Below is the code
replace(char text2find[80],char text2repl[80])
{char fileOrig\[32\] = "OrigFile.txt"; char fileRepl\[32\] = "ReplacedFile.txt"; char buffer\[MAX\_LEN\_SINGLE\_LINE+2\]; char \*buff\_ptr, \*find\_ptr, \*tok; FILE \*fp1, \*fp2; size\_t find\_len = strlen(text2find); fp1 = fopen(fileOrig,"r"); fp2 = fopen(fileRepl,"w+"); while(fgets(buffer,MAX\_LEN\_SINGLE\_LINE+2,fp1)) { buff\_ptr = buffer; tok = strtok(buff\_ptr,"\*");//ignores the string occurence after \* if(tok != NULL) { while ((find\_ptr = strstr(buff\_ptr,text2find))) { while(buff\_ptr < find\_ptr) fputc((int)\*buff\_ptr++,fp2); fputs(text2repl,fp2); buff\_ptr += find\_len; } fputs(buff\_ptr,fp2); } } rewind(fp1); rewind(fp2); fclose(fp2); fclose(fp1);
}
I wud actually wanto replace all the string in the same file, but am finding it tough and also reuse of the replace function multiple times wud be difficult. Hence went with two files... Thanks, Faez
-
How to parse a file and store few strings in an arrayFinally I have come up witha Not-so-good code... But it works.. :) I created another similar situation to parse from END-EXEC to End of File as below...
if (k == 0)
{
if (strcmp(tok, "END-EXEC") == 0)
k = 1;
}
else
{
if (strcmp(tok, "THE") == 0)
{
k = 0;
}
else
{
strcpy(tabRow[m],tok);m++;
fprintf(fp3,"Token: %s\n", tok);}
}
}
}
// Out of all the loops and condition now....j=0,n=0; for(j=0;j<=m;j++) { if(strcmp(tabCol\[n\],tabRow\[j\])==0) { k=j; strcpy(tabRow\[n\],tabRow\[k+1\]); n++; } else ; } for(k=0;k
It wud be nice to get ur comments on this... :-D
Thanks,
Faez -
How to parse a file and store few strings in an arrayNo no... It ain't always preceeded with "CN-" string ... The strings which needs to be stored in TabRow array are followed by the second occurrence of each tableCol values... Initially I also thought of using strcat () and append CN- to the tabCol values... But den I realized that it was for this file only... Other files are completely different. Regards, Faez
-
How to parse a file and store few strings in an arrayThat code was a beauty again .... :-D Actually... the final file after strtok() looks like the one below... :|
Token: DCLGEN
Token: LIBRARY
Token: ACTION
Token: LANGUAGE
Token: NAMES
Token: QUOTE
Token: COLSUFFIX
Token: IS
Token: EXEC
Token: POLICY_NO
Token: REG_NO
Token: EFFECTIVE_DATE
Token: EXPIRY_DATE
Token: CAN_EFF_DATE
Token: CAN_PRO_DATE
Token: RETURN_PREMIUM
Token: CAN_PROCESSED
Token: END-EXEC
Token: COBOL
Token: DCLCANCL
Token: POLICY_NO
Token: CN-POLICY-NO //I need this string too in diff array name tableRow[6][10]
Token: REG_NO
Token: CN-REG-NO //I need this string too in diff array name tableRow[6][10]
Token: EFFECTIVE_DATE
Token: CN-EFFECTIVE-DATE //I need this string too in diff array name tableRow[6][10]
Token: EXPIRY_DATE
Token: CN-EXPIRY-DATE //I need this string too in diff array name tableRow[6][10]
Token: CAN_EFF_DATE
Token: CN-CAN-EFF-DATE //I need this string too in diff array name tableRow[6][10]
Token: CAN_PRO_DATE
Token: CN-CAN-PRO-DATE //I need this string too in diff array name tableRow[6][10]
Token: RETURN_PREMIUM
Token: CN-RETURN-PREMIUM //I need this string too in diff array name tableRow[6][10]
Token: CAN_PROCESSED
Token: CN-CAN-PROCESSED //I need this string too in diff array name tableRow[6][10]
Token: THEYour code cleverly extracted the strings within EXEC and END-EXEC which I stored in
char tableCol[6][10]
But now I am finding it really difficult to store the strings which are immediatedly followed by the "tabCol" string from the file... :( ie CN-POLICY-NO, CN-REG-NO, CN-EFFECTIVE-DATE, CN-EXPIRY-DATE, CN-CAN-EFF-DATE, CN-CAN-PRO-DATE, CN-RETURN-PREMIUM & CN-CAN-PROCESSED which are to be stored in
char tableRow[6][10]
Apologies ...:( Thanks a ton, Faez
-
How to parse a file and store few strings in an arrayLovely..! This program works like a charm....:-D But now I am getting some extra unwanted strings too till the point I wanto extract the needed string.. My new o/p file now look like Token: DCLGEN Token: LIBRARY Token: ACTION Token: LANGUAGE Token: NAMES Token: QUOTE Token: COLSUFFIX Token: IS Token: EXEC Token: POLICY_NO Token: REG_NO Token: EFFECTIVE_DATE Token: EXPIRY_DATE Token: CAN_EFF_DATE Token: CAN_PRO_DATE Token: RETURN_PREMIUM Token: CAN_PROCESSED Token: END-EXEC But I want the strings only after EXEC and before END-EXEC... ie from POLICY_NO to CAN_PROCESSED... I tried using
while(strcmp("EXEC",tok))
but it ain't working .. :-/ Thanks in advance, Faez
-
How to parse a file and store few strings in an arrayint main()
{
char *buf[1024], *tok;
FILE *fp1, *fp2;
fp1=fopen("test.txt","r+");
fp2=fopen("newtest.txt","w+");while(fgets(buf, bufsize, fp1) != NULL)
{
for(tok = strtok(buf,"{");tok !="}";)
{
fprintf(fp2, "%s",buf);
}
}
fclose(fp2);
fclose(fp1);
}