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
-
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
try to use CStdioFile::ReadString to read one line once. :)
-
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
-
try to use CStdioFile::ReadString to read one line once. :)
Need to code it in C :)
-
Need to code it in C :)
So what do you have so far? You'll find far more people willing to critique existing code than those willing to do your work outright for you.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
So what do you have so far? You'll find far more people willing to critique existing code than those willing to do your work outright for you.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
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
-
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 about something a tad smaller, like:
BOOL bPrefix = FALSE;
while(fgets(buffer, sizeof(buffer), fp1))
{
if (strncmp(buffer, "READ", 4) == 0)
bPrefix = TRUE;
else if (buffer[strlen(buffer) - 1] == '.')
bPrefix = FALSE;if (bPrefix) fputc('#', fp2); fputs(buffer, fp2);
}
I've not tested it, but hopefully you get the idea.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
How about something a tad smaller, like:
BOOL bPrefix = FALSE;
while(fgets(buffer, sizeof(buffer), fp1))
{
if (strncmp(buffer, "READ", 4) == 0)
bPrefix = TRUE;
else if (buffer[strlen(buffer) - 1] == '.')
bPrefix = FALSE;if (bPrefix) fputc('#', fp2); fputs(buffer, fp2);
}
I've not tested it, but hopefully you get the idea.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
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:
-
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:
Faez Shingeri wrote:
There are no changes in the new file at all :|
Use the debugger to step through the code line by line to see what is happening along the way.
Faez Shingeri wrote:
Also, there are multiple instance of READ such as END-READ etc whose line should not be precceded with #
The code snippet I provided only precedes those lines that start with READ.
Faez Shingeri wrote:
I aint understanding how the current file pointer can be incremented till a "\n" or "." :confused:
fgets()
internally increments the file pointer."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Faez Shingeri wrote:
There are no changes in the new file at all :|
Use the debugger to step through the code line by line to see what is happening along the way.
Faez Shingeri wrote:
Also, there are multiple instance of READ such as END-READ etc whose line should not be precceded with #
The code snippet I provided only precedes those lines that start with READ.
Faez Shingeri wrote:
I aint understanding how the current file pointer can be incremented till a "\n" or "." :confused:
fgets()
internally increments the file pointer."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
Nothing helped... Tried alot of trial and error but the o/p refuses to roll out as expected.. :^)
-
Faez Shingeri wrote:
There are no changes in the new file at all :|
Use the debugger to step through the code line by line to see what is happening along the way.
Faez Shingeri wrote:
Also, there are multiple instance of READ such as END-READ etc whose line should not be precceded with #
The code snippet I provided only precedes those lines that start with READ.
Faez Shingeri wrote:
I aint understanding how the current file pointer can be incremented till a "\n" or "." :confused:
fgets()
internally increments the file pointer."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
It's working great now .. :-D Thnx alot for help
-
It's working great now .. :-D Thnx alot for help
So what did you end up with (to get it all going)?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
So what did you end up with (to get it all going)?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
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..