Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting quotation marks into a string 2

Status
Not open for further replies.

peterworm

Programmer
Aug 4, 2003
14
DE
I have to insert some quotation marks into a string. One “ after the sixth blank ond one “ befor the last
but one “ in the string (blank between “ and the last but one “). Mabe someone can help me, because
I’m not too familiar with C. I can use ANSI-C only.

The strings look like this:

217.3.189.72 - [27/Apr/2003:00:00:26 +0200] 200 277 POST /xmlcom HTTP/1.1 "Mozilla/4.0
(compatible; MSIE 5.0; Win32)"
217.3.189.72 - [27/Apr/2003:00:00:25 +0200] 200 277 POST /xmlcom HTTP/1.1 "Mozilla/4.0
(compatible; MSIE 5.0; Win32)"
217.83.13.3 - [27/Apr/2003:00:00:25 +0200] 200 902 POST /xmlcom HTTP/1.1 "Mozilla/4.0
(compatible; MSIE 6.0; Win32)"
217.83.13.3 - [27/Apr/2003:00:00:25 +0200] 200 197 GET /release_110d/config/messages_stlg.txt
HTTP/1.1 "Mozilla/4.0 (compatible; MSIE 6.0; Win32)"
217.83.13.3 - [27/Apr/2003:00:00:25 +0200] 200 2198 GET /release_110d/images/logo_applet_stlg.jar
HTTP/1.1 "Mozilla/4.0 (compatible; MSIE 6.0; Win32)"
217.83.13.3 - [27/Apr/2003:00:00:24 +0200] 200 197 GET /release_110d/config/messages_stlg.txt
HTTP/1.1 "Mozilla/4.0 (compatible; MSIE 6.0; Win32)"
217.3.189.72 - [27/Apr/2003:00:00:24 +0200] 200 7339 POST /xmlcom HTTP/1.1 "Mozilla/4.0
(compatible; MSIE 5.0; Win32)"

The code I use to read/write input/output is

int main (int argc, char *argv[])

{
char rfilename [ NAME_MAX + 1 ];
char wfilename [ NAME_MAX + 1 ];
FILE *rfp, *wfp;
int i;
char str[ S_LEN ];
char *str_addr;
char *add_str = "\x22";


if ( argc == 3 )

{

strncpy ( rfilename, argv [ 1 ], NAME_MAX );
rfilename [ NAME_MAX ] = '\0';

strncpy ( wfilename, argv [ 2 ], NAME_MAX );
wfilename [ NAME_MAX ] = '\0';
}
else
{

fprintf (stderr, "Aufruf: %s inputfile outputfile\n", argv[ 0 ] );
return 1;
}

rfp = fopen ( rfilename , "rb" );
if ( rfp == NULL )

{
fprintf (stderr, "%s: Error opening %s for reading! \n", argv[ 0 ],rfilename );
perror ( "Reason" );
return EXIT_OREAD;
}

wfp = fopen ( wfilename , "wb" );
if ( wfp == NULL )


{
fprintf (stderr, "%s: Error opening %s for writing! \n", argv[ 0 ],wfilename );
perror ( "Reason" );
return EXIT_OWRITE;
}

{
while ( ( str_addr = fgets ( str, S_LEN, rfp )) != NULL )

{

fprintf(wfp, " %s ",str_addr);



}

if ( ferror ( wfp ) )
{
fprintf ( stderr, "%s: Error writing to file %s ! \n" , argv[ 0 ], wfilename );
perror ( "Reason" );
fclose ( rfp );
fclose ( wfp );
return EXIT_EWRITE;

}
}
fclose ( rfp );
fclose ( wfp );
return EXIT_OKAY;
}
 
hi,
there are a lot of triks and rules to obtain this
depending os and compiler: see the best for you

1) use a \" printf(" hfdjhkfdj \"Mozilla ....\" " ) ;
2) a double "" printf(" hfdjhkfj ""Mozilla ....\" " ) ;
3) a %c for " printf( "%c%s%c\n", '"', "Mozzilla", '"' ) ;

note: in 3) before and after "Mozzilla" you have a (I put here extra spaces): ' " '


sure someone will know others .

bye
 
#1 above is ANSI C. It's also the clearest.

#2 above is not ANSI C.

#3 above is ANSI C.
 
Thanks a lot so far, but I need more help. Normaly I'm programming with Oracle9i.
1. How to locate the the sixth blank
2. How to find the “ befor the last but one “

Sorry for not describing this problem excact.
 
Hi Peter.

I have written some code which deals with your problem. Look at the code below (sorry if it isn't clear - the function that inserts quotes is called InsertQuotes):
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *InsertQuotes(char *);

int main (void)
{
  char *TestString = &quot;217.3.189.72 - [27/Apr/2003:00:00:26 +0200] 200 277 POST /xmlcom HTTP/1.1 \&quot;Mozilla/4.0 (compatible; MSIE 5.0; Win32)\&quot;&quot;;
  char RetString[200];

  printf(&quot;TestString:   %s\n&quot;, TestString);
  strcpy(RetString, InsertQuotes(TestString));
  printf(&quot;ReturnString: %s\n&quot;, RetString);

  return 0;
}

char *InsertQuotes(char *InputString)
{
  int InpChar = 0, RetChar = 0;
  int CharCount = 0;
  char ReturnString1[200];
  char ReturnString2[200];

  for (InpChar = 0; InpChar < strlen(InputString); InpChar++)
  {
    if (InputString[InpChar] == ' ')
      CharCount++;      // Count Spaces

    ReturnString1[RetChar] = InputString[InpChar];

    if (CharCount == 6) // If sixth space...
    {
      RetChar++;        // ...set next as quote...
      ReturnString1[RetChar] = '\&quot;';
      CharCount++;    // ...and ignore other spaces.
    }

    RetChar++;
  }
  ReturnString1[RetChar] = '\0'; // End of string with 1st &quot;.

  RetChar = strlen(ReturnString1) + 1;
  CharCount = 0;
  for (InpChar = strlen(ReturnString1); InpChar >= 0; InpChar--)
  {
    if (ReturnString1[InpChar] == '\&quot;')
      CharCount++;

    ReturnString2[RetChar] = ReturnString1[InpChar];

    if (CharCount == 2) // If second-last quote...
    {
      RetChar--;        // ...set previous as quote...
      ReturnString2[RetChar] = '\&quot;';
      CharCount++;    // ...and ignore other quotes.
    }

    RetChar--;
  }

  return (ReturnString2);
}
Hope that is useful... :)

Adonai
 
Hi Adonai,
thanks a lot. I think that this is a perfect solution.
But sorry, my Compiler has this output.

Wedit output window build: Tue Aug 05 17:55:05 2003
Warning &quot;c:\dokumente und einstellungen\administrator\desktop\add.c&quot;: 62 pointer to local 'ReturnString2' is an illegal return value
Compilation + link time:0.1 sec, Return code: 0

Maybe you know better what means illegal.

Peter
 
> char ReturnString2[200];
Local variables go out of scope when a function returns, which means any pointer to a local variable is also out of scope.

The quick fix is
Code:
static char ReturnString2[200];

However, the fixed-sized string creates its own set of problems (what if the result doesn't fit?)
Generally speaking, the caller should provide the space for the answer (say like strcat() does)
Eg
Code:
void InsertQuotes(char *InputString, char *OutputString);
 
hi adholioshake and salem,
thank you very much for your good support.
It works and I'm happy.

Peter
 
hi adholioshake and salem,
sorry, but I have a litte problem. I tried to load the outputstring into Oracle with the bulk loader.It's nessecary that the outputstring looks like
217.3.189.72 - [27/Apr/2003:00:00:26 +0200] 200 277 POST &quot;/xmlcom HTTP/1.1&quot; &quot;Mozilla/4.0
instead of
217.3.189.72 - [27/Apr/2003:00:00:26 +0200] 200 277 &quot;POST /xmlcom HTTP/1.1 &quot;&quot;Mozilla/4.0
If you could help me please.

Peter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top