|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
empty file string manipulation
am doing some string manipulation on a file:
the file has all sorts of characters strings, including newline i.e content of the file contents - 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:0000003B or ("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 #include <stdio.h> #include <errno.h> /* for strerror() */ #include <string.h> #include <stdlib.h> int main(void) { FILE *fp, *tmp; if ((fp=fopen("C:\\video.reg","r"))==NULL){ /* error */ } if ((tmp=fopen("C:\\ob.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.. |
|
#2
|
||||
|
||||
|
How about something like this:
Code:
#include <stdio.h>
#include <errno.h> /* for strerror() */
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *fp, *tmp;
if ((fp=fopen("C:\\video.reg","r"))==NULL){
/* error */
}
if ((tmp=fopen("C:\\ob.reg","w+"))==NULL){
/* error */
printf("error videob");
} else{
char line[256];
while( ! feof(fp) ) {
fgets(line, sizeof(line), fp);
{
if (strstr (line,
"\"DefaultSettings.VRefresh\"=dword:0000004b") != NULL)
strcpy (line, "DefaultSettings.VRefresh\"=dword:0000004c");
}
fputs(line, tmp);
}
}
fclose(tmp);
fclose(fp);
return 0;
}
Your main error is where you used fputs (ptr, tmp); The problem is that if line does not contain "DefaultSettings.VRefresh...", then ptr will be NULL. If the line did contain "DefaultSettings.VRefresh", you were copying a new line over. However, since you had your strstr function in a while loop, so it would check the line again (which now contains the new data) and would set ptr to NULL again. That's why you ended up with an empty file. Also you had a return 0 before you closed your files, which I moved. I fixed the code above off the top of my head, so I won't guarantee that it'll compile, but I hope this helps! |
|
#3
|
|||
|
|||
|
lol u know too much scorpian
j/p btw how old are you? |
|
#4
|
||||
|
||||
|
Let's just say that my first programming experience was on a PDP-11 running an RT-11 OS. I was just a snotty little kid then, but I sorta got hooked right after I wrote my first program in BASIC. Before you think I'm some kinda old fart here, I'd assure you that I'm easily under 30
. |
|
#5
|
|||
|
|||
|
lol
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > empty file string manipulation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|