sprintf giving me issues

This is a discussion on sprintf giving me issues within the C Programming forums, part of the General Programming Boards category; This is probably very basic but I'm not sure where I'm going wrong here...I want to have a loop going ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    25

    sprintf giving me issues

    This is probably very basic but I'm not sure where I'm going wrong here...I want to have a loop going through numbers from 0 through 90 in increments of 5, and making them into strings ("00", "05", "10", etc.), so I have this:

    Code:
    int i;
    char inc[10];
         for (i = 10; i <= 90; i += 5) {
             sprintf(inc, "%d", i);
             printf("%c\n", *inc); //Tests
             //Does stuff with the string inc
         }
    But when I print the value of inc to test that this is working, it seems to be only taking in the first digit of each number (so I get 0,5,1,1,2,2,etc.)...what am I doing wrong?

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    you need to print the string with %s
    your using %c, so it will only print the first character

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If you want to print a string, print a string.
    Code:
    printf("%s\n", inc);
    Or even
    Code:
    puts(inc);
    Last edited by Dave_Sinkula; 11-13-2006 at 06:50 PM. Reason: D'oh! Pokey again. :(
    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.*

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Thanks! This works but my program now crashes when I try I mean, assuming my code makes the string properly, this isn't a big deal if all I can't do is print it, but I'd still like to fix it. Windows gives an error with msvcrt.dll at offset 00032a16...

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    And where is the new code?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Here's new code. I removed the print but I'm getting a similar error when I use the string inc to make a new string. Here:

    Code:
    int i;
         char *modelfile;
         char inc[10];
         for (i = 0; i <= 90; i += 5) {
             sprintf(inc, "%d", i);
             modelfile = strcat(modelname, inc); //ERROR HERE
             //model = fopen(modelfile, mode);
             //fclose(model);
         }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by ramparts
    Here's new code. I removed the print but I'm getting a similar error when I use the string inc to make a new string. Here:

    Code:
    int i;
         char *modelfile;
         char inc[10];
         for (i = 0; i <= 90; i += 5) {
             sprintf(inc, "%d", i);
             modelfile = strcat(modelname, inc); //ERROR HERE
             //model = fopen(modelfile, mode);
             //fclose(model);
         }
    Only try to write to memory you own. Dangling pointers don't count as memory you own.
    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.*

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    I'm really sorry, I don't quite know enough about pointers to pick that up right away, I'm learning as I go along...how do I fix this?

    Note: The reason I'm using modelfile as a pointer is because I need to do that to pass it to fopen...that said, I'm not quite sure how to fix this.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Ooh, sorry, forgot to mention, modelname is defined globally as:

    char *modelname = "modelB.";

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    allocate some memory to it using malloc

    somthing like modelfile = malloc(strlen(modelname) + strlen(inc) + 1);
    then sprintf(modelfile, "%s%s", modelname, inc);

    or you need to increase the size of modelname to allow for inc to be appended to it. (which would be for using strcat)

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    It crashes at the sprintf.

    All I want to do is make a string saying "modelB.00", "modelB.05", etc., for i=0, 5, etc., this really shouldn't be this difficult--is there not another way to do this?
    Last edited by ramparts; 11-14-2006 at 12:23 AM.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Code:
    char *modelfile;
    points nowhere.
    strcat takes its first argument, looks to the place where it points (in your case no one knows where), searches where for the ‘/0’ character and starts writing from this point the contents of the second argument.

    So you should point your modelfile to some buffer you allocate statically
    Code:
    char modelfile[SOME_BIG_NUMER];
    Or dynamically…

    Make sure it contains the zero character
    Code:
    char modelfile[SOME_BIG_NUMER] = “”;
    And only then use it as an input to strcat.
    And also – better to use some other function, that can check that there is no buffer overrun (strcat does not check if there is enough place in your buffer for the string you’re trying to add, it will continue to write the second string till its end)


    PS.
    Code:
    char *modelname = "modelB.";
    This pointer points to some read-only memory, you cannot write there…
    Code:
    char modelfile[SOME_BIG_NUMER] = "modelB.";
    So you allocate SOME_BIG_NUMER of bytes statically for your string and copy there the string "modelB." At the beginning (including the ‘/0’ character)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (void) {
            char *modelfile;
            char modelname[] = "modelname.";
            char inc[] = "00";
    
            modelfile = malloc(strlen(modelname) + strlen(inc) + 1);
            sprintf(modelfile, "%s%s", modelname, inc);
    
            printf("%s\n", modelfile);
    
            return 0;
    }
    Output:
    modelname.00

    I'm sure you can tweak this for what you need.
    Last edited by sl4nted; 11-14-2006 at 12:28 AM.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Thanks so much! I have it working pretty much perfectly. The one thing is that, when I add it into the loop (so instead of making inc[]="00", I make it equal that when i=0 in the for loop), I get single digits for the single digit characters when I want trailing zeroes (so I get outputs like modelname.0 when I want modelname.00). This:
    Code:
    if (i < 10) {
                   sprintf(inc, "%d%s", 0, inc);
             }
    Gives me modelname.00 for i=0 (which it should) and i=5 (which it shouldn't). My code for making the int into a string is:

    Code:
    sprintf(inc, "%d", i);
    While making the %d to a %2d gives me trailing spaces, and not trailing zeroes. Why am I getting these weird outputs (the trailing spaces I understand but not getting 00 for i=5).

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    you don't really need the %d
    you can just put a 0

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (void) {
            int i;
            char *modelfile;
            char modelname[] = "modelname.";
            char inc[3];
    
            modelfile = malloc(strlen(modelname) + 4);
    
            for (i = 0; i < 99; i += 5)
            {
                    if (i < 10)
                            sprintf(inc, "0%d", i);
                    else
                            sprintf(inc, "%d", i);
    
            sprintf(modelfile, "%s%s", modelname, inc);
            printf("%s\n", modelfile);
    
            }
            free(modelfile);
            return 0;
    }
    output:
    modelname.00
    modelname.05
    modelname.10
    modelname.15
    modelname.20
    modelname.25
    modelname.30
    modelname.35
    modelname.40
    modelname.45
    modelname.50
    modelname.55
    modelname.60
    modelname.65
    modelname.70
    modelname.75
    modelname.80
    modelname.85
    modelname.90
    modelname.95
    Last edited by sl4nted; 11-14-2006 at 12:49 AM.

Page 1 of 3 123 LastLast
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 of float giving "1.#QO"
    By Mafepero in forum C++ Programming
    Replies: 2
    Last Post: 03-03-2008, 03:52 AM
  3. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 09:21 AM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 11:26 AM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 12:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21