C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old September 25th, 2002, 02:51 PM
nur nur is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 0 nur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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..

Reply With Quote
  #2  
Old September 25th, 2002, 05:49 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 55 m 33 sec
Reputation Power: 797
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!

Reply With Quote
  #3  
Old September 26th, 2002, 07:55 PM
Optix Optix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 36 Optix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
lol u know too much scorpian

j/p

btw how old are you?

Reply With Quote
  #4  
Old September 27th, 2002, 03:11 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 55 m 33 sec
Reputation Power: 797
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 .

Reply With Quote
  #5  
Old September 28th, 2002, 06:16 PM
Optix Optix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 36 Optix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
lol

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > empty file string manipulation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway