how to replace /e:1024 by /e:2048 ? 
Author Message
 how to replace /e:1024 by /e:2048 ?

Hello,

I'm trying to work out how to open a config.sys file and if I find

shell=c:\command.com /e:1024

change it to

shell=c:\command.com /e:2048

I am new to C - it seems difficult to do this compared to Perl - so
would appreciate some help here!

Cheers

Geoff



Fri, 02 Apr 2004 15:25:58 GMT  
 how to replace /e:1024 by /e:2048 ?

Quote:

> Hello,

> I'm trying to work out how to open a config.sys file and if I find

> shell=c:\command.com /e:1024

> change it to

> shell=c:\command.com /e:2048

> I am new to C - it seems difficult to do this compared to Perl - so
> would appreciate some help here!

I will assume that you have read a line of this file
into a character array called "line", as a null-terminated
character string.  I will also assume that you have the
following two lines somewhere at the top of your source code
file:

   #include <string.h>
   #include <stdlib.h>

Then the following should make the substitution(s) you want:

   {
      char * ptr;
      while ((ptr = strstr (line, "/e:1024")) != NULL) {
         strncpy (ptr, "/e:2048", 7);
      }
   }

Note, however, that I have not tested it, that it is
not very good from a maintenance viewpoint, and that
it only works because the two strings "/e:1024" and
"/e:2048" are the same length.  If you want to
understand how it works, read the documentation on
the "string" standard library functions.



Fri, 02 Apr 2004 16:19:09 GMT  
 how to replace /e:1024 by /e:2048 ?
On Mon, 15 Oct 2001 08:19:09 GMT, Kevin Miller

Kevin,

Quote:
>   #include <string.h>
>   #include <stdlib.h>

>Then the following should make the substitution(s) you want:

>   {
>      char * ptr;
>      while ((ptr = strstr (line, "/e:1024")) != NULL) {
>         strncpy (ptr, "/e:2048", 7);
>      }
>   }

I have tried following but there is an error to do with the fputs I
think?  Geoff

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *fp;
char filename[40] = "c:\\exe\\fred.txt";

int main()
{

if ((fp=fopen(filename, "w+")) == NULL)

{
fprintf(stderr, "Error opening file %s.", filename);
exit(1);

Quote:
} else {

char line[100];

while( ! feof(fp) ) {
fgets(line, sizeof(line), fp);
{
char * ptr;
while ((ptr = strstr (line, "/e:1024")) != NULL) {
strncpy (ptr, "/e:2048", 7);
printf("value = %s", ptr);
char fputs(ptr, fp);

Quote:
}
}
}

fclose(fp);
return 0;
Quote:
}
}



Fri, 02 Apr 2004 17:20:50 GMT  
 how to replace /e:1024 by /e:2048 ?

Quote:
> On Mon, 15 Oct 2001 08:19:09 GMT, Kevin Miller

> Kevin,
>>   #include <string.h>
>>   #include <stdlib.h>

>>Then the following should make the substitution(s) you want:

>>   {
>>      char * ptr;
>>      while ((ptr = strstr (line, "/e:1024")) != NULL) {
>>         strncpy (ptr, "/e:2048", 7);
>>      }
>>   }
> I have tried following but there is an error to do with the fputs I
> think?  Geoff
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> FILE *fp;
> char filename[40] = "c:\\exe\\fred.txt";

Why do you have these as global? They're only ever needed from within
one function.

Quote:
> int main()
> {
> if ((fp=fopen(filename, "w+")) == NULL)

Ugh. Please learn to indent.

Quote:
> {
> fprintf(stderr, "Error opening file %s.", filename);
> exit(1);
> } else {
> char line[100];
> while( ! feof(fp) ) {

This is the famous "Pascal disease". Search the C FAQ for better
techniques for detecting end-of-file.

Quote:
> fgets(line, sizeof(line), fp);
> {
> char * ptr;
> while ((ptr = strstr (line, "/e:1024")) != NULL) {
> strncpy (ptr, "/e:2048", 7);
> printf("value = %s", ptr);
> char fputs(ptr, fp);

Here's your problem. Function calls must not be preceded with type
names. You're apparently mixing up the concepts of function calls and
function definitions. Get rid of the "char" and you're in business.

Quote:
> }
> }
> }
> fclose(fp);
> return 0;
> }
> }

Do you see why it's good to indent? I don't know whether your braces
match up or not unless I count them by hand. If you indented your code
I would see at a glance whether they matched up or not.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Normal is what everyone else is, and you're not."
   - Dr. Tolian Soran



Fri, 02 Apr 2004 17:29:59 GMT  
 how to replace /e:1024 by /e:2048 ?
On Mon, 15 Oct 2001 09:20:50 GMT, Geoff Cox

Kevin,

got some { in wrong place - but still getting error messages when
compiling?

Geoff

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *fp;
char filename[40] = "c:\\exe\\fred.txt";

int main()
{

if ((fp=fopen(filename, "w+")) == NULL)

{
fprintf(stderr, "Error opening file %s.", filename);
exit(1);

Quote:
} else {

char line[100];

while( ! feof(fp) )
 {
 fgets(line, sizeof(line), fp);
 char *ptr;
   while ((ptr = strstr (line, "/e:1024")) != NULL)
   {
   strncpy (ptr, "/e:2048", 7);
   char fputs(ptr, *fp);
   }
 }
fclose(fp);
return 0;

Quote:
}
}



Fri, 02 Apr 2004 17:35:59 GMT  
 how to replace /e:1024 by /e:2048 ?

Quote:

> On Mon, 15 Oct 2001 08:19:09 GMT, Kevin Miller

> I have tried following but there is an error to do with the fputs I
> think?  Geoff

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>

> FILE *fp;
> char filename[40] = "c:\\exe\\fred.txt";

> int main()
> {

> if ((fp=fopen(filename, "w+")) == NULL)

w+ means open file for writing and effectively empty it which would mean
there is nothing to read, you probably meant to use r+ (open for reading
and writing). Personally I would open two files - the source for reading
and another for writing, so I could make sure everything was valid
before "corrupting" the original file.

Quote:
> {
> fprintf(stderr, "Error opening file %s.", filename);
> exit(1);

for portability, I would use
exit(EXIT_FAILURE);

Quote:

> } else {

> char line[100];

> while( ! feof(fp) ) {
> fgets(line, sizeof(line), fp);

This won't work quite how you expect it, feof(fp) will only return true
after an fgets call has reached the end of file. So with your loop
structure the last line is effectively processed twice (since at the end
of file fgets would return NULL and the buffer would retain its previous
contents).

Here, it would be sufficient to write
while (fgets(line, sizeof line, fp) != NULL) {

and if you're copying from one file to another you'll need to copy all
lines.

Quote:
> {
> char * ptr;
> while ((ptr = strstr (line, "/e:1024")) != NULL) {
> strncpy (ptr, "/e:2048", 7);
> printf("value = %s", ptr);
> char fputs(ptr, fp);

  ^^^^

This is spurious!

Quote:
> }
> }

> }
> fclose(fp);
> return 0;

> }
> }

        Regards
        Des Walker


Fri, 02 Apr 2004 17:58:41 GMT  
 how to replace /e:1024 by /e:2048 ?


Quote:

>> On Mon, 15 Oct 2001 08:19:09 GMT, Kevin Miller

>w+ means open file for writing and effectively empty it which would mean
>there is nothing to read, you probably meant to use r+ (open for reading
>and writing). Personally I would open two files - the source for reading
>and another for writing, so I could make sure everything was valid
>before "corrupting" the original file.

Des,

I have changed to r+ but now I get following in the file "fred";

/e:1024

/e:1024
/e

I am using the code below - must be wrong some where..

Cheers
Geoff

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *fp;
char filename[40] = "fred";

int main()
{

if ((fp=fopen(filename, "r+")) == NULL)

{
fprintf(stderr, "Error opening file %s.", filename);
exit(1);

Quote:
} else {

char line[100];
char *ptr="";
while( ! feof(fp) )
 {
 fgets(line, sizeof(line), fp);
/* char *ptr = "";*/
   while ((ptr = strstr (line, "/e:1024")) != NULL)
   {
   strncpy (ptr, "/e:2048", 7);
   fputs(ptr, fp);
   }
 }
fclose(fp);
return 0;
Quote:
}
}



Fri, 02 Apr 2004 18:13:13 GMT  
 how to replace /e:1024 by /e:2048 ?

Quote:




> >> On Mon, 15 Oct 2001 08:19:09 GMT, Kevin Miller

> >w+ means open file for writing and effectively empty it which would mean
> >there is nothing to read, you probably meant to use r+ (open for reading
> >and writing). Personally I would open two files - the source for reading
> >and another for writing, so I could make sure everything was valid
> >before "corrupting" the original file.

> Des,

> I have changed to r+ but now I get following in the file "fred";

> /e:1024

> /e:1024
> /e

> I am using the code below - must be wrong some where..

> Cheers
> Geoff

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>

> FILE *fp;
> char filename[40] = "fred";

> int main()
> {

> if ((fp=fopen(filename, "r+")) == NULL)

> {
> fprintf(stderr, "Error opening file %s.", filename);
> exit(1);

> } else {

> char line[100];
> char *ptr="";
> while( ! feof(fp) )
>  {
>  fgets(line, sizeof(line), fp);
> /* char *ptr = "";*/
>    while ((ptr = strstr (line, "/e:1024")) != NULL)
>    {
>    strncpy (ptr, "/e:2048", 7);
>    fputs(ptr, fp);
>    }
>  }
> fclose(fp);
> return 0;
> }

> }

Sorry Geoff,
my earlier comment was obviously misleading. I've never tried to modify
a file in situ like this - I would prefer to have two files (one to read
and one to write).

I would suspect to have any hope of making it work you would have to use
some combination of ftell and fseek to sort out the file position
indicators. I'm not definitely sure what locations the file position
indicators provide following read and write - another reason why I
prefer to use one file for each of reading and writing, because the
operations then do what I reasonably expect.

What is definitely wrong with your code is that it loops around doing
alternate read and write operations on the same stream. You are not
permitted to do this without an intervening positioning function call
(e.g. fseek). You could also follow the write operation by an fflush
function call, instead. I guess this is to allow the streams file
position indicator(s) to get synchronised otherwise any of your
implementations I/O optimisations might get in the way of what you are
trying to do

I would suggest first get your application to work with separate files
for reading and writing (The tmpfile and rename standard library
functions might be of use here). You can then try "improving" on it
later if you're not happy with it.

Don't forget that feof is unnecessary (and ineffective) in this case.

        Regards
        Des Walker



Fri, 02 Apr 2004 19:32:47 GMT  
 how to replace /e:1024 by /e:2048 ?


Quote:
>w+ means open file for writing and effectively empty it which would mean
>there is nothing to read, you probably meant to use r+ (open for reading
>and writing). Personally I would open two files - the source for reading
>and another for writing, so I could make sure everything was valid
>before "corrupting" the original file.

Des, re the 2 file approach - I have found following code which does
this except for some reason the temp.txt file is not being renamed and
deleted...can you see why?

Cheers Geoff

include <stdio.h>
#include <string.h>

#define MAX_FILE_LEN 156

int main(void) {
   int found;

   if((found = fsearch_replace("test.txt", "/e:1024", "/e:2048")) >
-1)
      printf("Found and replaced %d occurances of
%s\n",found,"/e:1024");
   else puts("Unable to search file");
   return 0;
   }

int fsearch_replace(const char *fname, const char *f, const char *r) {
   char *s, *s1 ,buf[MAX_FILE_LEN];
   int result = 0;
   FILE *fpi, *fpo;

   if((fpi = fopen(fname,"r")) == NULL) return -1;
   if((fpo = fopen("temp.txt", "w")) == NULL) {
      fclose(fpi);
      return -1;
      }
   while(1) {
      if(NULL == fgets(buf, sizeof buf, fpi)) break;
      if((s = strstr(buf,f)) == NULL) {
         fprintf(fpo,"%s",buf);
         fflush(fpo);
         }
      else {
         result++;
         for(s1 = buf; s1 != s; s1++) fputc(*s1,fpo);
         fprintf(fpo,"%s",r);
         s += strlen(f);
         fprintf(fpo,"%s",s);
         }
      }
   remove(fname);
   rename("temp.txt",fname);
   return result;
   }



Fri, 02 Apr 2004 19:49:17 GMT  
 how to replace /e:1024 by /e:2048 ?
On Mon, 15 Oct 2001 11:49:17 GMT, Geoff Cox

The code below works in that it
1. finds /e:1024 in test.txt
2. changes it to /e:2048 and write this to temp.txt
3. deletes test.txt and renames temp.txt to test.txt

Fine except I would like to copy all lines from test.txt into temp.txt
and back to test.txt so that nothing is lost...problem - I do not
fully understand the following part of the function;

   while(1) {
      if(NULL == fgets(buf, sizeof buf, fpi)) break;
      if((s = strstr(buf,f)) == NULL) {
         fprintf(fpo,"%s",buf);
         fflush(fpo);
         }
      else {
         result++;
         for(s1 = buf; s1 != s; s1++) fputc(*s1,fpo);
         fprintf(fpo,"%s",r);
         s += strlen(f);
         fprintf(fpo,"%s",s);
         }

Can anyone please help?!

Cheers

Geoff

--------------------------this works----------------------------------

#include <stdio.h>
#include <string.h>

#define MAX_FILE_LEN 156

int main(void) {
   int found;

   if((found = fsearch_replace("test.txt", "/e:1024", "/e:2048")) >
-1)
      printf("Found and replaced %d occurances of
%s\n",found,"/e:1024");
   else puts("Unable to search file");
   return 0;
   }

int fsearch_replace(const char *fname, const char *f, const char *r) {
   char *s, *s1 ,buf[MAX_FILE_LEN];
   int result = 0;
   FILE *fpi, *fpo;

   if((fpi = fopen(fname,"r")) == NULL) return -1;
   if((fpo = fopen("temp.txt", "w")) == NULL) {
      fclose(fpi);
      return -1;
      }
   while(1) {
      if(NULL == fgets(buf, sizeof buf, fpi)) break;
      if((s = strstr(buf,f)) == NULL) {
         fprintf(fpo,"%s",buf);
         fflush(fpo);
         }
      else {
         result++;
         for(s1 = buf; s1 != s; s1++) fputc(*s1,fpo);
         fprintf(fpo,"%s",r);
         s += strlen(f);
         fprintf(fpo,"%s",s);
         }
      }
      fclose(fpi);
      fclose(fpo);
      remove(fname);
      rename("temp.txt",fname);
      return result;
   }



Fri, 02 Apr 2004 20:59:31 GMT  
 how to replace /e:1024 by /e:2048 ?

Quote:



> >w+ means open file for writing and effectively empty it which would mean
> >there is nothing to read, you probably meant to use r+ (open for reading
> >and writing). Personally I would open two files - the source for reading
> >and another for writing, so I could make sure everything was valid
> >before "corrupting" the original file.

> Des, re the 2 file approach - I have found following code which does
> this except for some reason the temp.txt file is not being renamed and
> deleted...can you see why?

> Cheers Geoff

> include <stdio.h>
> #include <string.h>

> #define MAX_FILE_LEN 156

> int main(void) {
>    int found;

>    if((found = fsearch_replace("test.txt", "/e:1024", "/e:2048")) >
> -1)
>       printf("Found and replaced %d occurances of
> %s\n",found,"/e:1024");
>    else puts("Unable to search file");
>    return 0;
>    }

> int fsearch_replace(const char *fname, const char *f, const char *r) {
>    char *s, *s1 ,buf[MAX_FILE_LEN];
>    int result = 0;
>    FILE *fpi, *fpo;

>    if((fpi = fopen(fname,"r")) == NULL) return -1;
>    if((fpo = fopen("temp.txt", "w")) == NULL) {
>       fclose(fpi);
>       return -1;
>       }
>    while(1) {
>       if(NULL == fgets(buf, sizeof buf, fpi)) break;
>       if((s = strstr(buf,f)) == NULL) {
>          fprintf(fpo,"%s",buf);
>          fflush(fpo);
>          }
>       else {
>          result++;
>          for(s1 = buf; s1 != s; s1++) fputc(*s1,fpo);
>          fprintf(fpo,"%s",r);
>          s += strlen(f);
>          fprintf(fpo,"%s",s);
>          }
>       }
>    remove(fname);
>    rename("temp.txt",fname);
>    return result;
>    }

On possibility is that you might need to close the stream associated
with the temp.txt file before renaming it. And you definitely would need
to close the stream associated with the test.txt before removing it.

try adding

fclose(fpi);
fclose(fpo);

before the remove function call.

Note also this function only replaces the first occurrence of the sought
string in the line, and both sought and replacement strings must be the
same length.

        Regards
        Des Walker



Fri, 02 Apr 2004 21:14:40 GMT  
 how to replace /e:1024 by /e:2048 ?


Quote:
>try adding

>fclose(fpi);
>fclose(fpo);

Des,

that did the trick - also have realised that the code which I said
works does in fact copy all the lines...am starting to understand the
while {    }  part too!

Many thanks

Cheers

Geoff



Fri, 02 Apr 2004 22:07:35 GMT  
 how to replace /e:1024 by /e:2048 ?

Quote:

> I'm trying to work out how to open a config.sys file and if I find

> shell=c:\command.com /e:1024

> change it to

> shell=c:\command.com /e:2048

> I am new to C - it seems difficult to do this compared to Perl - so
> would appreciate some help here!

This has nothing to do with the C language, so you should be
asking elsewhere.

However, since what you are trying to do is dangerous on many
systems, I will point out that you would find no such statement on
my systems.  Instead, you would find:

  SHELL=C:\4DOS\602\4DOS.COM C:\4DOS\602\ /P

If you have insufficient space, emit a message and let the owner
take care of it.  You can even suggest how to do it.  Just don't
{*filter*}up his system.

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Sat, 03 Apr 2004 01:16:57 GMT  
 how to replace /e:1024 by /e:2048 ?

Quote:


> > I'm trying to work out how to open a config.sys file and if I find

> > shell=c:\command.com /e:1024

> > change it to

> > shell=c:\command.com /e:2048

> > I am new to C - it seems difficult to do this compared to Perl - so
> > would appreciate some help here!

> This has nothing to do with the C language, so you should be
> asking elsewhere.

    I believe that the question that the OP is asking is indeed
on-topic. He seems to want to know how to open a file, search it for the
text "shell=c:\command.com /e:1024", replace it with
"shell=c:\command.com /e:2048", and write it back out again.

Quote:

> However, since what you are trying to do is dangerous on many
> systems, I will point out that you would find no such statement on
> my systems.  Instead, you would find:

>   SHELL=C:\4DOS\602\4DOS.COM C:\4DOS\602\ /P

> If you have insufficient space, emit a message and let the owner
> take care of it.  You can even suggest how to do it.  Just don't
>{*filter*}up his system.

--
Clark S. Cox III

http://www.*-*-*.com/


Sat, 03 Apr 2004 01:51:08 GMT  
 how to replace /e:1024 by /e:2048 ?


Quote:
>However, since what you are trying to do is dangerous on many
>systems, I will point out that you would find no such statement on
>my systems.  Instead, you would find:

>  SHELL=C:\4DOS\602\4DOS.COM C:\4DOS\602\ /P

>If you have insufficient space, emit a message and let the owner
>take care of it.  You can even suggest how to do it.  Just don't
>screw up his system.

I take your point on this.

Cheers

Geoff



Sat, 03 Apr 2004 03:48:53 GMT  
 
 [ 21 post ]  Go to page: [1] [2]

 Relevant Pages 

1. CScrollView with range (-2048 to 2048) How?

2. please any one help me in dividing 1024 bit number with 1024 bit another number

3. Help, I'm exceeding 2048 char length

4. Another fail date for UNIX: 2048 (passwd aging)

5. FLEX definition string cannot be over than 2048-char wide

6. MSHierachical FlexGrid shows only 2048 Rows

7. BitBlt Slow when bitmap is greater then 2048 pixels in height

8. char (*array_p) [1024] question

9. converting const char* to unsigned char[1024]

10. How to listen to a port < 1024

11. 1024 letters in command line

12. ETDT_ENABLETAB background repeats at 1024 pixels - aggggh

 

 
Powered by phpBB® Forum Software