#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    0
    Rep Power
    0

    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. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    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!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    36
    Rep Power
    13
    lol u know too much scorpian :p

    j/p

    btw how old are you?
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    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 :D.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    36
    Rep Power
    13
    lol ;)

IMN logo majestic logo threadwatch logo seochat tools logo