please help newbie string replacement in big file
Author |
Message |
nur e #1 / 17
|
 please help newbie string replacement in big file
I am doing some string manipulation on a file: the file has all sorts of characters strings, including whiteline i.e content, part of the file - video.reg is as follows: [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardware Profiles\Current\System\CurrentControlSet\SERVICES\ATIRAGE3] [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardware Profiles\Current\System\CurrentControlSet\SERVICES\ATIRAGE3\DEVICE0] "DefaultSettings.YResolution"=dword:00000258 "DefaultSettings.VRefresh"=dword:0000004b "DefaultSettings.Flags"=dword:00000000 "DefaultSettings.XPanning"=dword:00000000 "DefaultSettings.YPanning"=dword:00000000 The problem is i want to replace the occurance of "DefaultSettings.VRefresh"=dword:0000004b with "DefaultSettings.VRefresh"=dword:0000004C and save the changes to the same file or different file This is the code i have written so far but doesnt work It produces an empty file #include <stdio.h> #include <string.h> #include <stdlib.h> static occur = 0; int main(void) { FILE *fp, *tmp; if ((fp=fopen("C:\\video.reg","r"))==NULL){ /* error */ } if ((tmp=fopen("C:\\videob.reg","w+"))==NULL){ /* error */ printf("error videob"); } else{ char line[256]; while( ! feof(fp) ) { fgets(line, sizeof(line), fp); { char *ptr; while ((ptr = strstr (line, "\"DefaultSettings.VRefresh\"=dword:0000004b")) != NULL) { strcpy (ptr, "DefaultSettings.VRefresh\"=dword:0000004c"); } } fputs(ptr, tmp); } } return 0; fclose(tmp); fclose(fp); This doesnt work, it produces an empty file!! Thank you all in advance.. nur
|
Sun, 13 Mar 2005 23:33:51 GMT |
|
 |
Emmanuel Delahay #2 / 17
|
 please help newbie string replacement in big file
Quote: > I am doing some string manipulation on a file: > the file has all sorts of characters strings, including whiteline i.e > content, part of the file - video.reg is as follows: > [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardware > Profiles\Current\System\CurrentControlSet\SERVICES\ATIRAGE3] > [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardware > Profiles\Current\System\CurrentControlSet\SERVICES\ATIRAGE3\DEVICE0] > "DefaultSettings.YResolution"=dword:00000258 > "DefaultSettings.VRefresh"=dword:0000004b > "DefaultSettings.Flags"=dword:00000000 > "DefaultSettings.XPanning"=dword:00000000 > "DefaultSettings.YPanning"=dword:00000000 > The problem is i want to replace the occurance of > "DefaultSettings.VRefresh"=dword:0000004b with > "DefaultSettings.VRefresh"=dword:0000004C and save the changes to the > same file or different file
This is a job for any decent text editor. Try UltraEdit. It's a must! http://www.ultraedit.com/ Quote: > This is the code i have written so far but doesnt work > It produces an empty file > #include <stdio.h> > #include <string.h> > #include <stdlib.h> > static occur = 0; > int main(void) > { > FILE *fp, *tmp; > if ((fp=fopen("C:\\video.reg","r"))==NULL){ > /* error */ > } > if ((tmp=fopen("C:\\videob.reg","w+"))==NULL){ > /* error */ > printf("error videob"); > }
You open the same file twice? This is not portable and dangerous. The good way is to use 2 different files. I hope your error handling stops the execution... Quote: > else{ > char line[256]; > while( ! feof(fp) ) {
(FAQ) C is not Pascal. This is not the good way to find the end of a file. You must read your manual and test the value returned by the reading function (here fgets()). Quote: > fgets(line, sizeof(line), fp); > { > char *ptr; > while ((ptr = strstr (line, > "\"DefaultSettings.VRefresh\"=dword:0000004b")) != NULL) {
Doesn't work because fgets() gets the '\n' too. Quote: > strcpy (ptr, > "DefaultSettings.VRefresh\"=dword:0000004c"); > } > } > fputs(ptr, tmp);
You forgot the final '\n' Quote: > } > } > return 0; > fclose(tmp); > fclose(fp);
-- -ed- emdel at noos.fr ~]=[o FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ C-library: http://www.dinkumware.com/htm_cl/index.html "Mal nommer les choses c'est ajouter du malheur au monde." -- Albert Camus.
|
Mon, 14 Mar 2005 01:36:12 GMT |
|
 |
Ryan Henness #3 / 17
|
 please help newbie string replacement in big file
<snip> Quote: > > FILE *fp, *tmp; > > if ((fp=fopen("C:\\video.reg","r"))==NULL){ > > /* error */ > > } > > if ((tmp=fopen("C:\\videob.reg","w+"))==NULL){ > > /* error */ > > printf("error videob"); > > } > You open the same file twice? This is not portable and dangerous. The good > way is to use 2 different files. I hope your error handling stops the > execution...
Uh, look again. Carefully. Ryan.
|
Mon, 14 Mar 2005 01:33:13 GMT |
|
 |
Eric Sosma #4 / 17
|
 please help newbie string replacement in big file
Quote:
> I am doing some string manipulation on a file > [that resembles a text version of a Windows registry] > This is the code i have written so far but doesnt work > It produces an empty file [...]
I'm not entirely sure why you wind up with an empty file, but one possible explanation is that the input "c:\\video.reg" file might not have the format you expect. <off-topic> The Windows registry files themselves use a proprietary binary format which needs to be translated to (and from) the textual representation you've shown. If you're trying to read an untranslated binary registry file and treat it as text, all manner of odd things might happen. </off-topic> In case that's not the problem, let me point out a few miscellaneous errors and oddities in your program. I don't see how any of them would explain the empty output file, but cleaning away extraneous bugs may help you locate the Big One: Quote: > #include <stdio.h> > #include <string.h> > #include <stdlib.h>
You don't use any of the facilities in <stdlib.h>, so you needn't #include it. The inclusion is harmless, though. Quote: > static occur = 0;
You don't use this variable anywhere, so there's no point in defining it. The definition is harmless, though. Quote: > int main(void) > { > FILE *fp, *tmp; > if ((fp=fopen("C:\\video.reg","r"))==NULL){ > /* error */ > }
If this fopen() fails you'll be in big trouble, because you just plow straight ahead as if all had gone well. Later when you try to use the NULL-valued `fp', bad things will almost certainly happen. Quote: > if ((tmp=fopen("C:\\videob.reg","w+"))==NULL){ > /* error */ > printf("error videob"); > } > else{ > char line[256]; > while( ! feof(fp) ) {
This is the wrong way to use feof(), and will give you erroneous results. See Question 12.2 in the comp.lang.c Frequently Asked Questions (FAQ) list http://www.*-*-*.com/ ~scs/C-faq/top.html Quote: > fgets(line, sizeof(line), fp); > { > char *ptr; > while ((ptr = strstr (line, > "\"DefaultSettings.VRefresh\"=dword:0000004b")) != NULL) { > strcpy (ptr, > "DefaultSettings.VRefresh\"=dword:0000004c"); > }
There are at least four weird things here. First, the use of `while' is distinctly peculiar: a simple `if' would seem more appropriate. Second, using strstr() instead of strcmp() might be defensible on the grounds that it could locate the target string in the middle of other stuff, but since the strcpy() will obliterate any trailing characters it doesn't seem you've really thought this through. Third, one of the obliterated characters will be the '\n' that marks the end of the line; the result will be that the "...004c" will {*filter*}right up against the characters from the next line rather than appearing at the end of their own line. And finally, the replacement string lacks an initial double-quote. Quote: > } > fputs(ptr, tmp); > } > } > return 0;
By returning zero unconditionally, the program always indicates "success" even when it's detected and reported a failure to open "c:\\videob.reg". That's not fatal -- you're at liberty to write a program that always "succeeds" no matter how much trouble it gets into -- but it's unusual. Quote: > fclose(tmp); > fclose(fp);
Because of the `return' statement above, these two fclose() calls will never be executed. Again, that's not necessarily fatal: the ordinary actions of program termination will, among other things, close all still-open streams. Still, the presence of these unreachable calls suggests you've overlooked something. --
|
Mon, 14 Mar 2005 01:49:42 GMT |
|
 |
ak #5 / 17
|
 please help newbie string replacement in big file
|I am doing some string manipulation on a file: | |the file has all sorts of characters strings, including whiteline i.e |content, part of the file - video.reg is as follows: | |[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardware |Profiles\Current\System\CurrentControlSet\SERVICES\ATIRAGE3] | |[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardware |Profiles\Current\System\CurrentControlSet\SERVICES\ATIRAGE3\DEVICE0] | |"DefaultSettings.YResolution"=dword:00000258 |"DefaultSettings.VRefresh"=dword:0000004b |"DefaultSettings.Flags"=dword:00000000 |"DefaultSettings.XPanning"=dword:00000000 |"DefaultSettings.YPanning"=dword:00000000 | |The problem is i want to replace the occurance of |"DefaultSettings.VRefresh"=dword:0000004b with |"DefaultSettings.VRefresh"=dword:0000004C and save the changes to the |same file or different file | |This is the code i have written so far but doesnt work |It produces an empty file | |#include <stdio.h> |#include <string.h> |#include <stdlib.h> | |static occur = 0; |int main(void) |{ | | | | |FILE *fp, *tmp; | |if ((fp=fopen("C:\\video.reg","r"))==NULL){ | /* error */ | } | | |if ((tmp=fopen("C:\\videob.reg","w+"))==NULL){ | /* error */ | printf("error videob"); | | } | |else{ | | | char line[256]; | | while( ! feof(fp) ) { | fgets(line, sizeof(line), fp); | | { | char *ptr; | while ((ptr = strstr (line, |"\"DefaultSettings.VRefresh\"=dword:0000004b")) != NULL) { | strcpy (ptr, |"DefaultSettings.VRefresh\"=dword:0000004c"); | } | } | | fputs(ptr, tmp); | } | | | | | return 0; | | |fclose(tmp); | |fclose(fp); | | |This doesnt work, it produces an empty file!! | |Thank you all in advance.. | |nur try for instance: static char keyword[] = "\"DefaultSettings.VRefresh\"=dword:0000004"; static char replace[] = "\"DefaultSettings.VRefresh\"=dword:0000004c"; while (fgets( line, sizeof(line), fp)!=NULL) { if (!strnicmp( line,keyword, strlen( keyword) ) { strcpy(line,replace); } fputs(line,tmp); Quote: }
not checked for syntax. hth/ak
|
Mon, 14 Mar 2005 02:22:53 GMT |
|
 |
Emmanuel Delahay #6 / 17
|
 please help newbie string replacement in big file
Quote: >> > FILE *fp, *tmp; >> > if ((fp=fopen("C:\\video.reg","r"))==NULL){ >> > /* error */ >> > } >> > if ((tmp=fopen("C:\\videob.reg","w+"))==NULL){ >> > /* error */ >> > printf("error videob"); >> > } >> You open the same file twice? This is not portable and dangerous. The >> good way is to use 2 different files. I hope your error handling >> stops the execution... > Uh, look again. Carefully.
Oops. T'was so a small difference! My apologies to the OP. -- -ed- emdel at noos.fr ~]=[o FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ C-library: http://www.dinkumware.com/htm_cl/index.html "Mal nommer les choses c'est ajouter du malheur au monde." -- Albert Camus.
|
Mon, 14 Mar 2005 02:48:49 GMT |
|
 |
Emmanuel Delahay #7 / 17
|
 please help newbie string replacement in big file
Quote: > static char keyword[] = "\"DefaultSettings.VRefresh\"=dword:0000004"; > static char replace[] = "\"DefaultSettings.VRefresh\"=dword:0000004c"; > while (fgets( line, sizeof(line), fp)!=NULL) { > if (!strnicmp( line,keyword, strlen( keyword) ) {
strnicmp() is not a standard C function. strncmp() should work here. -- -ed- emdel at noos.fr ~]=[o FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ C-library: http://www.dinkumware.com/htm_cl/index.html "Mal nommer les choses c'est ajouter du malheur au monde." -- Albert Camus.
|
Mon, 14 Mar 2005 02:52:43 GMT |
|
 |
Eric Sosma #8 / 17
|
 please help newbie string replacement in big file
Quote:
> > I am doing some string manipulation on a file > > [that resembles a text version of a Windows registry] > > This is the code i have written so far but doesnt work > > It produces an empty file [...]
Something I seem to have missed on my first look at your code is that -- well it ISN'T the code! The stuff you posted won't compile, much less "produce an empty file." Specifically, the identifier `ptr' is undefined when it appears in the `fputs(ptr, tmp);' line: Quote: > { > char *ptr; > while ((ptr = strstr (line, > "\"DefaultSettings.VRefresh\"=dword:0000004b")) != NULL) { > strcpy (ptr, > "DefaultSettings.VRefresh\"=dword:0000004c"); > } > } > fputs(ptr, tmp);
... and there's no other variable named `ptr' lying around anywhere. Please, please, please, Only One (and anyone else): if you want help debugging some code, exhibit THE ACTUAL CODE and not some kind of paraphrase. If your car is giving you trouble, do you ask the mechanic to look at your bicycle? --
|
Mon, 14 Mar 2005 02:31:47 GMT |
|
 |
nrk #9 / 17
|
 please help newbie string replacement in big file
Quote:
> I am doing some string manipulation on a file: > the file has all sorts of characters strings, including whiteline i.e > content, part of the file - video.reg is as follows: > [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardware > Profiles\Current\System\CurrentControlSet\SERVICES\ATIRAGE3] > [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardware > Profiles\Current\System\CurrentControlSet\SERVICES\ATIRAGE3\DEVICE0] > "DefaultSettings.YResolution"=dword:00000258 > "DefaultSettings.VRefresh"=dword:0000004b > "DefaultSettings.Flags"=dword:00000000 > "DefaultSettings.XPanning"=dword:00000000 > "DefaultSettings.YPanning"=dword:00000000 > The problem is i want to replace the occurance of > "DefaultSettings.VRefresh"=dword:0000004b with > "DefaultSettings.VRefresh"=dword:0000004C and save the changes to the > same file or different file
The first question is: Why not use a decent text editor to do the job (as your file seems to be text)? Quote: > This is the code i have written so far but doesnt work > It produces an empty file
As Eric mentions, the code won't even compile. Nevertheless, even making assumptions for it to compile, it is more than likely that you will end up invoking undefined behavior. Read on. Quote: > #include <stdio.h> > #include <string.h> > #include <stdlib.h> > static occur = 0; > int main(void) > {
Do you like empty lines? In code, it usually makes your task of finding what you want more difficult. Quote: > FILE *fp, *tmp; > if ((fp=fopen("C:\\video.reg","r"))==NULL){ > /* error */ > }
Using consistent and readable indentation standards is the first step towards writing maintainable, better programs. Quote: > if ((tmp=fopen("C:\\videob.reg","w+"))==NULL){ > /* error */ > printf("error videob"); > }
The above should have been an else if. Otherwise, even when the first fopen fails, you could still enter the else part below with a NULL file pointer to read from and that would invoke undefined behavior. So, something along the lines of: if ( (fp = fopen(source_name)) == NULL ) { /* error */ } else { if ( (tmp = fopen(dest_name)) == NULL ) { /* error */ } else { /* do our processing */ } } Quote: > else{ > char line[256]; > while( ! feof(fp) ) { > fgets(line, sizeof(line), fp);
Bad idea. When using fgets, you should always check its return to see if EOF or an I/O error has occurred. So, what you want is: while ( fgets(line, sizeof(line), fp) != NULL ) { Quote: > {
Why start a new block here? It doesn't seem to serve any useful purpose. Quote: > char *ptr;
I will assume that the declaration of ptr is made outside this block. Quote: > while ((ptr = strstr (line, > "\"DefaultSettings.VRefresh\"=dword:0000004b")) != NULL) {
Read the documentation of strstr. It returns NULL if there was no match for the needle in the haystack. In which case, what happens to the fputs that uses ptr down below? Undefined behavior. Quote: > strcpy (ptr, > "DefaultSettings.VRefresh\"=dword:0000004c");
If your intent was to replace the occurrences of the original string with the target string, this is not the way to do it. What strcpy will do is that it will truncate your input line at the end of the first occurrence of the search string. For e.g. If input line was: "I am a big \"DefaultSettings.VRefresh\"=dword:0000004b line\n" Your strcpy will convert it to: "I am a big \"DefaultSettings.VRefresh\"=dword:0000004c" Since your search and replacement strings are the same size (assuming you missed out a \" in the replacement string), you could take the following approach: const char replstr[] = "\"DefaultSettings.VRefresh\"=dword:0000004c"; and inside the while loop: int replindex; for ( replindex = 0; replindex < sizeof(replstr)-1; ++replindex ) *ptr++ = replstr[replindex]; Or in your case where only the last character is to be changed: ptr[sizeof(replstr)-1] = replstr[sizeof(replstr)-1]; Quote: > } > } > fputs(ptr, tmp);
You want this to be: fputs(line, tmp); and it should be outside the while( (ptr = strstr...) ) loop if you want all the lines of the input file to be present in your output file as well. In your strstr loop, ptr would point to some memory inside the array line. So any changes you make to the contents of ptr in that loop actually changes the contents of line. So using line in the fputs would give you the new changed line in the output file. Quote: > } > } > return 0; > fclose(tmp); > fclose(fp);
Huh?? Do you expect these two fclose statements to ever get executed? HTH, nrk.
|
Mon, 14 Mar 2005 08:19:21 GMT |
|
 |
m #10 / 17
|
 please help newbie string replacement in big file
Quote:
> > This is the code i have written so far but doesnt work > > It produces an empty file > > fgets(line, sizeof(line), fp); > > { > > char *ptr; > > while ((ptr = strstr (line, > > "\"DefaultSettings.VRefresh\"=dword:0000004b")) != NULL) { > Doesn't work because fgets() gets the '\n' too.
AFAICS, the '\n' read by fgets() into line is irrelevant here... He's using strstr(), not strcmp(), and so the function will succeed if the string "\"DefaultSettings.VRefresh\"=dword:0000004b" is in the line array. Quote: > > strcpy (ptr, > > "DefaultSettings.VRefresh\"=dword:0000004c"); > > } > > } > > fputs(ptr, tmp);
This line is his most blatant problem. The code above is mangled, but as far as I can see, the strcpy() is ONLY called if the previous strstr() function succeeds. Moreover, ptr is declared in a block which ends just before fputs, making ptr local to that block and thus meaning that ptr's storage should be destroyed at the end of the block, before the call to fputs. Finally, if the strstr() fails, ptr will be NULL and the call to fputs will invoke undefined behavior. Matt
|
Mon, 14 Mar 2005 09:39:53 GMT |
|
 |
nur e #11 / 17
|
 please help newbie string replacement in big file
Thank you all, Eric you right about .reg format i had to pipe the resulting .reg to txt i.e type video.reg > vga.txt This allowed me to do string manipulation. Thanks to Rex of team OR, this is the modified and working code..... #include <stdio.h> #include <errno.h> /* for strerror() */ #include <string.h> #include <stdlib.h> static occur = 0; int main(void) { FILE *fp, *tmp; if ((fp=fopen("C:\\video.reg","r"))==NULL){ /* error */ Quote: }
if ((tmp=fopen("C:\\videob.reg","w+"))==NULL){ /* error */ printf("error videob"); Quote: }
else{ char line[256]; while( ! feof(fp) ) { fgets(line, sizeof(line), fp); { char *ptr; while ((ptr = strstr (line, "\"DefaultSettings.VRefresh\"=dword:0000004b")) != NULL) { strcpy (ptr, "DefaultSettings.VRefresh\"=dword:0000004c"); Quote: } }
fputs(ptr, tmp); Quote: } }
return 0; fclose(tmp); fclose(fp);
|
Mon, 14 Mar 2005 22:38:56 GMT |
|
 |
Eric Sosma #12 / 17
|
 please help newbie string replacement in big file
Quote:
> Thank you all, > Eric you right about .reg format i had to pipe the resulting .reg to > txt i.e type video.reg > vga.txt > This allowed me to do string manipulation. > Thanks to Rex of team OR, > this is the modified and working code.....
No, it isn't. You're lying, and wasting everyone's time. --
|
Tue, 15 Mar 2005 00:30:01 GMT |
|
 |
nur e #13 / 17
|
 please help newbie string replacement in big file
Quote: > No, it isn't. You're lying, and wasting everyone's time.
Have you tried compiling using DJGPP and on windows 2000 professional??? Guess not? I have replaced the string "\Def....000.." with "0000004b". I have compiled this and it did work!! Honest.. Eric how else do you want me to prove it?????? I am ever thankful... Again i want to thank you for your contribution.. Thank you all
|
Tue, 15 Mar 2005 22:55:32 GMT |
|
 |
ak #14 / 17
|
 please help newbie string replacement in big file
|> static char keyword[] = "\"DefaultSettings.VRefresh\"=dword:0000004"; |> static char replace[] = "\"DefaultSettings.VRefresh\"=dword:0000004c"; |> |> while (fgets( line, sizeof(line), fp)!=NULL) { |> if (!strnicmp( line,keyword, strlen( keyword) ) { | |strnicmp() is not a standard C function. strncmp() should work here. yes, me bad :)
|
Tue, 15 Mar 2005 23:43:21 GMT |
|
 |
Eric Sosma #15 / 17
|
 please help newbie string replacement in big file
Quote:
> > No, it isn't. You're lying, and wasting everyone's time. > Have you tried compiling using DJGPP and on windows 2000 professional??? > Guess not?
I don't have Windows 2000, but I do have DJGPP. When I run your code through it (default options), the compiler complains nurein.c: In function `main': nurein.c:42: `ptr' undeclared (first use in this function) nurein.c:42: (Each undeclared identifier is reported only once nurein.c:42: for each function it appears in.) nurein.c:54: parse error at end of input Conclusion: You may indeed have gotten the program to work, but the program wasn't generated from the code you posted. --
|
Wed, 16 Mar 2005 00:00:01 GMT |
|
|
Page 1 of 2
|
[ 17 post ] |
|
Go to page:
[1]
[2] |
|