Thread: sprintf?

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    sprintf?

    does anyone know why this doesnt work?


    char BaseFileName[] = "soemthing%s";
    char FileNameA[255];
    sprintf(FileNameA,BaseFileName,"more");

    sorry to do this, but i have to make this work by the end of the day... and i cant get it

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I'd go for:
    Code:
    char BaseFileName[] = "something";
    char FileNameA[255] = {0};
    sprintf (FileNameA, "%s%s", BaseFileName, "more");
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Define "doesn't work". Are you sure it's not working? Did you print the string?

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    does anyone know why this doesnt work?
    It doesn't? What doesn't work about it...?
    It's a fairly simple line of code, and I'm not seeing anything wrong with it.
    It compiles and seems to work correctly...

    (You are aware you're using sprintf, and that sprintf() doesn't output anything to stdout?)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You must be doing something else wrong, because there is nothing obviously wrong with those 3 lines.

    Try them in a 3 line program, and confirm it for yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    sorry.. by doesnt work i meant doesnt compile
    Sorry i posted a silly quesiton like that on the board, but here is a good question

    i found the problem .. but i dont understand why its happening.
    I have noticed this many times before too.

    If i put a command in the middle of my initialization of variables my program wont compile... why is that?

    ie.
    char face9[255];
    face9="w00t";
    int i;

    //now my program would be broken

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    When you take the name of an array by itself, it's taken as a pointer to the first element, however it's considered const in the sense that you can't make it point to somewhere else. It can't be an lvalue in an expression (ie. nothing can be assigned to it).

    Arrays can only be initialized during declaration.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    ok.. well that was just me making a bad example.. sorry

    lets use an easy one

    int i,k;
    i=9;
    k=i*9;

    short P;

    //now my program doesnt work because i did operations before i finished all of my declarations.

  10. #10
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    oh.. ok problem solved.. thanks Cat guy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 10:21 AM
  3. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 04:14 AM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM