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; Code: temp = malloc(strlen(modelname) + strlen(inc) + strlen("Lightcurves\") + 1); sprintf(temp, "%s%s%s%s", "Lightcurves\", modelname, inc, ".mag"); your allocating enough room ...

  1. #31
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    temp = malloc(strlen(modelname) + strlen(inc) + strlen("Lightcurves\\") + 1);
    sprintf(temp, "%s%s%s%s", "Lightcurves\\", modelname, inc, ".mag");
    your allocating enough room for modelname, inc, Lightcurves\\ and '/0'

    then putting Lightcurves\\, modelname, inc and ".mag" in it.

    there needs to be room for ".mag"......but really since it looks like the size of temp is the same each
    iteration of the loop, you should just declare it statically, instead of allocating and freeing each time each time.

    p.s. don't forget to close out somewhere

  2. #32
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    It is not enough
    "Lightcurves\\modelB.00.mag"
    takes more than 20 characters allocated in the modelfile array, so each strcpy(modelfile[i/5], temp); overwrites some additional bytes...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #33
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    #include <stdio.h>
    
    char modelname[] = "modelname.";
    
    void fitCurves()
    {
         int i;
         char inc[3];
         char modelfile[18][30];
    
    
         out = fopen("bestinc.txt", "a+"); //Opens file to write results to.
         fprintf(out, "i\tX^2\n"); //Prints header in periods.txt.
         fclose(out);
    
         for (i = 0; i < 18; i++) {
    
             sprintf(inc, "%02d", i*5);
             sprintf(modelfile[i], "Lightcurves\\%s%s.mag", modelname, inc);
    
             printf("%s\n", modelfile[i]);
    
             model = openFile(modelfile[i]);
             fclose(model);
    
         }
    }
    
    int main(void)
    {
            fitCurves();
            return 0;
    }
    I'm not sure if model is global or what. but this should fix some stuff

  4. #34
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Code:
             sprintf(inc, "%02d", i*5);
             sprintf(modelfile[i], "Lightcurves\\%s%s.mag", modelname, inc);
    is equivalent to
    Code:
             sprintf(modelfile[i], "Lightcurves\\%s%02d.mag", modelname, i*5);
    Code:
    char modelname[] = "modelname.";
    can be replaced by
    Code:
    const char *modelname = "modelname.";
    out should be checked before use

    Code:
    void fitCurves()
    better declare as
    Code:
    void fitCurves(void)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #35
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    and also using wierd numbers like char modelname[18][30] should be replaced with
    something like
    Code:
    #define NFILES 18         /*number of files of type modelname */
    #define NAMELEN 30   /* maximum length of the filename */
    ....
    ....
    in function
    ...
    char modelname[NFILES][NAMLEN]
    ...
    ...
    for (i= 0; i < NFILES; i++)
    makes it easier to see whats happening as well as easier to change it if need be.

    Code:
    sprintf(modelfile[i], "Lightcurves\\%s%s.mag", modelname, inc);
    may have to be
    Code:
    sprintf(modelfile[i], "Lightcurves\\\\%s%s.mag", modelname, inc);
    but really if your not using inc for anything else, you should just do what vart suggested
    and combine it to one sprintf, and get rid of inc all together
    Last edited by sl4nted; 11-14-2006 at 11:41 AM.

  6. #36
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Declare it without the asterisk as mentioned.
    Code:
    char *modelfile[19][20];
    ->
    Code:
    char modelfile[19][20];
    Then make sure openFile() takes a char* or char[] or char[20] as its argument.

    Also add 4 to the amount of memory that you allocate, for ".mag".

    [edit] Woah, way too slow . . . [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 3 of 3 FirstFirst 123
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