Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have to insert a "#" to start of each lines if a string "READ" is detected at the start of a line till a "." is encounterd.....

Eg: test.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
***************************************



Thanks in advance

OP posted as a solution:
I am able to add a # only one line after that... not till the "." Below is my code..

C++
int main()
{
	 char fileOrig[32] = "test.txt";
	 char fileRepl[32] = "myReplacedFile.txt";
 
	 const char text2find[80] = " READ ";
	 const char text3find[80] = "\n";
	 char text4find[80] = ".";
 
	 const char text2repl[80] = "# READ ";
	 const char text3repl[80] = "#";
 
	    char buffer[MAX_LEN_SINGLE_LINE+2];
	    char *buff_ptr, *find_ptr;
	    FILE *fp1, *fp2,*fp3;
	    size_t find_len = strlen(text2find);
 
	    fp1 = fopen(fileOrig,"r+");
	    fp2 = fopen(fileRepl,"w+");
 
	    int i;
	    char *p, *q;
	    char line[80];
	    int flag=0;
	    int c = '.';
	    int result;
 

	    while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1))
	 //   while(fgets(buffer, sizeof(buffer), fp1))

	    {
	        buff_ptr = buffer;
 
	        while ((find_ptr = strstr(buff_ptr,text2find)))
	       	{
 
	       		find_len = strlen(text2find);
				while(buff_ptr < find_ptr)
	       		 fputc((int)*buff_ptr++,fp2);
 
	       		fputs(text2repl,fp2);
	            buff_ptr += find_len;
 
	            flag=1;
	       	}
 
       	fputs(buff_ptr,fp2);
 
			if(flag == 1)
				{
					//fputc('\n',fp2);
					fputc('#',fp2);
					flag=0;
				}
 

		 }
 

 
		   fclose(fp2);
	   fclose(fp1);
	   return 0;
 
}


[edit]OP update moved from solution into the question - OriginalGriff[/edit]

[edit]

Extremely sorry .. my mistake... [/edit]
Posted
Updated 5-Mar-12 1:21am
v3
Comments
OriginalGriff 5-Mar-12 7:05am    
And? What is the problem?
Faez Shingeri 5-Mar-12 7:32am    

//This my further addition to the code
if(flag == 1)
{
ch = fgetc(fp1);
while ( (ch == '\n' ) || (ch =='.'))
{
if (ch == '.')
{
fputc('#',fp2);
flag = 0;

}
fputc('#',fp2);
}

}
Prasad_Kulkarni 5-Mar-12 7:23am    
what is this??
Faez Shingeri 5-Mar-12 7:26am    
Its basically a cobol file.... I have to preceed each line with a "#" to all those lines starting from the READ string encounter till the first "."

Inserting "#" before read if file is indented.
int main()
{
	bool found = false;
	char fileOrig[MAX_PATH] = "F:\\Test\\test.txt";
	char fileRepl[MAX_PATH] = "F:\\Test\\myReplacedFile.txt";

	const char text2find[80] = "READ ";
	const char text3find[80] = "\n";
	char text4find[80] = ".";

	const char text2repl[80] = "# READ ";
	const char text3repl[80] = "#";

	char buffer[MAX_LEN_SINGLE_LINE+2];
	char buffer1[MAX_LEN_SINGLE_LINE+2];
	char buffer2[MAX_LEN_SINGLE_LINE+2];
	char *buff_ptr, *find_ptr;
	FILE *fp1, *fp2,*fp3;
	size_t find_len = strlen(text2find);

	fp1 = fopen(fileOrig,"r+");
	fp2 = fopen(fileRepl,"w+");

	int i=0;
	char *p, *q;
	char line[80];
	int c = '.';
	int result;

	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;
		}
	}
	fclose(fp2);
	fclose(fp1);
	return 0;
}
 
Share this answer
 
Comments
Faez Shingeri 6-Mar-12 0:28am    
That was lovely..!! :-D Thnx a ton :)
I have updated your code and made some changes.. Now below given code is working as you wanted.. :)

#define MAX_LEN_SINGLE_LINE 80
using namespace std;

int main()
{
	bool found = false;
	char fileOrig[MAX_PATH] = "F:\\Test\\test.txt";
	char fileRepl[MAX_PATH] = "F:\\Test\\myReplacedFile.txt";

	const char text2find[80] = "READ ";
	const char text3find[80] = "\n";
	char text4find[80] = ".";

	const char text2repl[80] = "# READ ";
	const char text3repl[80] = "#";

	char buffer[MAX_LEN_SINGLE_LINE+2];
	char buffer1[MAX_LEN_SINGLE_LINE+2];
	char *buff_ptr, *find_ptr;
	FILE *fp1, *fp2,*fp3;
	size_t find_len = strlen(text2find);

	fp1 = fopen(fileOrig,"r+");
	fp2 = fopen(fileRepl,"w+");

	int i;
	char *p, *q;
	char line[80];
	int c = '.';
	int result;

	while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1))
	{
		buff_ptr = buffer;
		if(strncmp(buffer,"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;
		}
	}
	fclose(fp2);
	fclose(fp1);
	return 0;
}
 
Share this answer
 
Comments
Faez Shingeri 5-Mar-12 22:52pm    
Thanks for your help Chandrakantt.. :) The code works beautifully when I give READ as the first string of the line..., but when there is even a single space before READ it stops to work... and all the files which i gotta test are indented. Any idea why it doesn't work..?


Thanks in advance,
Faez
Chandrakantt 6-Mar-12 0:12am    
yes we are comparing the start of the line that is from 0th position. I have made some more updates in my code as per your need. you can check my code in another solution.
Faez Shingeri 8-Mar-12 1:44am    
Am having a tough time extracting the first string after "READ"... :(
Any suggestions..?
Eg:
buffer2 now contains
"READ Studentfile AT END MOVE HIGH-VALUES To StudentDetails."
I need the string "Studentfile" to be stored.

Thanks in advance,
Faez
Chandrakantt 9-Mar-12 0:20am    
So do you need Studentfile from the line "READ Studentfile AT END MOVE HIGH-VALUES To StudentDetails." or complete line "Studentfile AT END MOVE HIGH-VALUES To StudentDetails." after READ ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900