Thread: Output filename / Char* question.

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Output filename / Char* question.

    Hello... I may have a fairly simple question, and if so I apologize.
    However, I haven't been able to discern or find a solution, so here goes:

    I have a program which needs to create a set of files: "values.1","values.2","values.3"
    However, then number of such files cannot be hardcoded (as it is defined at runtime as the number of processors I chose to run on), and so the names of these files cannot be hardcoded into the source either.

    As such, I want to do the following (where the line I cannot do is sketched (but not correct)). The wrapping lines are included in case the context matters.


    Code:
    FILE *fout; /* Yes, I do usually do C++, but can't in this instance */
    int Np = 4;    /* this is the value that cannot be hardcoded */
    int i;
    char path[32] = "./values.";
    char filename[32];
    
      /* The FOR loop is not actually part of the code, but that's MPI specific,
          and if I can make it work in this case, I'm (1-eps) sure
          that I can make it work in the real code */
    
    for (i=0;i<Np;i++) {
       strcpy(filename,path);
       strcat(   filename,   (char *) i    );   /* THIS LINE SEEMS TO BE WRONG ... */
       fout = fopen(filename,"w");        /* THIS MAY WELL BE WRONG TOO... */
       fprintf(fout,"somestuff",somevalues);
       fclose(fout);
    
    }  /* endfor */

    When I try to run this, it compiles, but I do get a warning:
    PGC-W-0155-Pointer value created from a nonlong integral type (test.c: 112)

    And when I try to run it, I get the error:
    p0_20484: p4_error: interrupt SIGSEGV: 11



    Any help would be much appreciated on this problem, thank you in advance.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The simpler snprintf() can replace both strcpy() and strcat(), as in
    Code:
    snprintf(filename, sizeof filename, "%s%d", path, i);

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Thanks... I hadn't known that.
    However, that is not the primary issue with the code...

    I need to know how to structure those couple lines so that I can open and write to the numbered files I want.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Nevermind, question answered....
    I missed something stupid... basically the difference between char a[] and char *a...

    thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  3. Generic Resource_Manager WIP with lots TODO
    By Shamino in forum C++ Programming
    Replies: 19
    Last Post: 02-01-2006, 01:55 AM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Simple question on quoting output
    By LouB in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2002, 02:57 PM