% stdout?

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

% stdout?

Cassio B. Caporal
        Hey,

        I have problems to print '%' in stdout... Suppose code below:

                #include <stdio.h>

                main() {
                         char foo[] = "bar=30%\n";
                         fprintf(stdout, bar);
                }

        OpenBSD returns : bar=30
        Linux returns   : bar=30%

        How can I solve this? Thanks,

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

Andreas Kahari
Have a look in your C code book.  The you will need to printf "%%" to get a '%'.

Andreas


On 09/11/06, Cassio B. Caporal <[hidden email]> wrote:

>         Hey,
>
>         I have problems to print '%' in stdout... Suppose code below:
>
>                 #include <stdio.h>
>
>                 main() {
>                          char foo[] = "bar=30%\n";
>                          fprintf(stdout, bar);
>                 }
>
>         OpenBSD returns : bar=30
>         Linux returns   : bar=30%
>
>         How can I solve this? Thanks,
>
>


--
Andreas Kahari
Somewhere in the general Cambridge area, UK

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

fuzzyping
In reply to this post by Cassio B. Caporal
On Nov 9, 2006, at 11:37 AM, Cassio B. Caporal wrote:

> Hey,
>
> I have problems to print '%' in stdout... Suppose code below:
>
> #include <stdio.h>
>
> main() {
> char foo[] = "bar=30%\n";
> fprintf(stdout, bar);
> }
>
> OpenBSD returns : bar=30
> Linux returns   : bar=30%
>
> How can I solve this? Thanks,

$ cat foo.c
#include <stdio.h>

main() {
         char foo[] = "bar=30%%\n";
         fprintf(stdout, foo);
}
$ gcc foo.c -o foo
$ ./foo
bar=30%


--
Jason Dixon
DixonGroup Consulting
http://www.dixongroup.net

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

Cassio B. Caporal
        Yes, but I read lines from a file.. I wrote a function that add one
more '%' and works fine. Thanks!

Jason Dixon wrote:

> On Nov 9, 2006, at 11:37 AM, Cassio B. Caporal wrote:
>
>>     Hey,
>>
>>     I have problems to print '%' in stdout... Suppose code below:
>>
>>         #include <stdio.h>
>>
>>         main() {
>>              char foo[] = "bar=30%\n";
>>              fprintf(stdout, bar);
>>         }
>>
>>     OpenBSD returns : bar=30
>>     Linux returns   : bar=30%
>>
>>     How can I solve this? Thanks,
>
> $ cat foo.c
> #include <stdio.h>
>
> main() {
>         char foo[] = "bar=30%%\n";
>         fprintf(stdout, foo);
> }
> $ gcc foo.c -o foo
> $ ./foo
> bar=30%
>
>
> --
> Jason Dixon
> DixonGroup Consulting
> http://www.dixongroup.net

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

terry tyson
In reply to this post by Andreas Kahari
Also, I think you mean:

    fprintf(stdout, foo);

not

    fprintf(stdout, bar);

right?

Terry

On Thu, Nov 09, 2006 at 04:49:20PM +0000, Andreas Kahari wrote:

> Have a look in your C code book.  The you will need to printf "%%" to get a
> '%'.
>
> Andreas
>
>
> On 09/11/06, Cassio B. Caporal <[hidden email]> wrote:
> >        Hey,
> >
> >        I have problems to print '%' in stdout... Suppose code below:
> >
> >                #include <stdio.h>
> >
> >                main() {
> >                         char foo[] = "bar=30%\n";
> >                         fprintf(stdout, bar);
> >                }
> >
> >        OpenBSD returns : bar=30
> >        Linux returns   : bar=30%
> >
> >        How can I solve this? Thanks,
> >
> >
>
>
> --
> Andreas Kahari
> Somewhere in the general Cambridge area, UK

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

Tom Cosgrove-2
In reply to this post by fuzzyping
Seriously guys.  NOOOOOOO!!!!!!!

To print an arbitrary string use fprintf(stdout, "%s", foo);

Come on.

Tom

>>> Jason Dixon 9-Nov-06 16:59 >>>
>
> On Nov 9, 2006, at 11:37 AM, Cassio B. Caporal wrote:
>
> > Hey,
> >
> > I have problems to print '%' in stdout... Suppose code below:
> >
> > #include <stdio.h>
> >
> > main() {
> > char foo[] = "bar=30%\n";
> > fprintf(stdout, bar);
> > }
> >
> > OpenBSD returns : bar=30
> > Linux returns   : bar=30%
> >
> > How can I solve this? Thanks,
>
> $ cat foo.c
> #include <stdio.h>
>
> main() {
>          char foo[] = "bar=30%%\n";
>          fprintf(stdout, foo);
> }
> $ gcc foo.c -o foo
> $ ./foo
> bar=30%

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

John Wright-6
In reply to this post by fuzzyping
On Nov 9, 2006, at 11:37 AM, Cassio B. Caporal wrote:

> Hey,
>
> I have problems to print '%' in stdout... Suppose code below:
>
> #include <stdio.h>
>
> main() {
> char foo[] = "bar=30%\n";
> fprintf(stdout, bar);
> }
>
> OpenBSD returns : bar=30
> Linux returns   : bar=30%
>
> How can I solve this? Thanks,

fprintf(stdout, "%s", foo);

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

Andy Hayward
In reply to this post by Cassio B. Caporal
On 11/9/06, Cassio B. Caporal <[hidden email]> wrote:
> I have problems to print '%' in stdout... Suppose code below:

Use:

        fprintf(stdout, "%s", foo);

This is mentioned in the man page for fprintf.

-- ach

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

Andreas Kahari
In reply to this post by fuzzyping
Suppose the data in 'foo' comes from user input:

#include <stdio.h>

main()
{
    char            foo[] = "bar=30%\n";
    fprintf(stdout, "%s", foo);
}



Andreas


On 09/11/06, Jason Dixon <[hidden email]> wrote:

> On Nov 9, 2006, at 11:37 AM, Cassio B. Caporal wrote:
>
> >       Hey,
> >
> >       I have problems to print '%' in stdout... Suppose code below:
> >
> >               #include <stdio.h>
> >
> >               main() {
> >                        char foo[] = "bar=30%\n";
> >                        fprintf(stdout, bar);
> >               }
> >
> >       OpenBSD returns : bar=30
> >       Linux returns   : bar=30%
> >
> >       How can I solve this? Thanks,
>
> $ cat foo.c
> #include <stdio.h>
>
> main() {
>          char foo[] = "bar=30%%\n";
>          fprintf(stdout, foo);
> }
> $ gcc foo.c -o foo
> $ ./foo
> bar=30%
>
>
> --
> Jason Dixon
> DixonGroup Consulting
> http://www.dixongroup.net
>
>


--
Andreas Kahari
Somewhere in the general Cambridge area, UK

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

Reyk Floeter-2
In reply to this post by fuzzyping
On Thu, Nov 09, 2006 at 11:59:12AM -0500, Jason Dixon wrote:

> > I have problems to print '%' in stdout... Suppose code below:
> >
> > #include <stdio.h>
> >
> > main() {
> > char foo[] = "bar=30%\n";
> > fprintf(stdout, bar);
> > }
> >
> > OpenBSD returns : bar=30
> > Linux returns   : bar=30%
> >
> > How can I solve this? Thanks,
>
> $ cat foo.c
> #include <stdio.h>
>
> main() {
>         char foo[] = "bar=30%%\n";
>         fprintf(stdout, foo);

heh, you found the bug. i just wanted to bet that the code would not
run under linux...

> }
> $ gcc foo.c -o foo
> $ ./foo
> bar=30%
>

you should also completely avoid the format string in this case.

printf("%s", foo);

reyk

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

Steffen Wendzel
In reply to this post by Cassio B. Caporal
On Thu, 09 Nov 2006 14:37:33 -0200 "Cassio B. Caporal" <[hidden email]> wrote:

: Hey,

Hi,

:
: I have problems to print '%' in stdout... Suppose code below:
:
: #include <stdio.h>
:
: main() {

your main should be of type 'int'.

: char foo[] = "bar=30%\n";
: fprintf(stdout, bar);

You mean "fprintf(stdout, foo);" ?

: }
:
: How can I solve this? Thanks,

If you want to print a '%', you need to use '%%' in your array.

--steffen

--
website: http://cdp.doomed-reality.org
hardened linux: http://drlinux.doomed-reality.org

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

Matthew Closson
In reply to this post by Cassio B. Caporal
On Thu, 9 Nov 2006, Cassio B. Caporal wrote:

> Hey,
>
> I have problems to print '%' in stdout... Suppose code below:
>
> #include <stdio.h>
>
> main() {
> char foo[] = "bar=30%\n";
> fprintf(stdout, bar);
> }
>
> OpenBSD returns : bar=30
> Linux returns   : bar=30%
>
> How can I solve this? Thanks,

Use the format specifier with fprintf:

#include <stdio.h>

int main()
{
   char foo[] = "bar=30%\n";
   fprintf(stdout, "%s", foo);
}

cc test.c
./a.out
bar=30%


  -Matt-

Reply | Threaded
Open this post in threaded view
|

Re: % stdout?

Philip Guenther-2
In reply to this post by Cassio B. Caporal
On 11/9/06, Cassio B. Caporal <[hidden email]> wrote:
>         I have problems to print '%' in stdout... Suppose code below:
>
>                 #include <stdio.h>
>
>                 main() {
>                          char foo[] = "bar=30%\n";
>                          fprintf(stdout, bar);

When posting code, please cut-and-paste it into your message, as the
above code won't compile.  I presume you meant to write:
                             fprintf(stdout, foo);

That passes 'foo' as the format argument to fprintf().  The format
argument is a compact description of what should be output and *NOT*
simply a string to be output.  If you want to simply output a literal
string you should *not* pass that string as the format to fprintf, but
rather pass a format saying "just output the next argument as a
string" and pass the string as the next argument, ala:
                              fprintf(stdout, "%s", foo);

If the string being printed is under the control of an outside party,
then it is *critical* that you do something like the above to avoid
security holes.

IMHO, you should never invoke fprintf() with exactly two arguments,
nor printf() with exactly one argument.  Either use a format of "%s"
or switch to fputs()/puts().

(...though you have to reverse the order of the arguments when going
from fprintf() to fputs()...)


Philip Guenther