output file name

How do I create an output file name using some of the values I have in an array?

normally,

FILE *wt;
wt = fopen ("outputfile", "w");
fprintf ...
fclose(wt);

so what I want is, instead of me specifying a name such as, the "outputfile", I want the program to get the name combining some values I have in an array.

pls help me... and thanks in advance..

Comments

  • : How do I create an output file name using some of the values I have
    : in an array?
    :
    : normally,
    :
    : FILE *wt;
    : wt = fopen ("outputfile", "w");
    : fprintf ...
    : fclose(wt);
    :
    : so what I want is, instead of me specifying a name such as, the
    : "outputfile", I want the program to get the name combining some
    : values I have in an array.
    :
    : pls help me... and thanks in advance..
    :

    Derefrence the array through its index:

    [code]
    FILE *wt;
    [b]wt = fopen (names[0], "w");[/b]
    fprintf ...
    fclose(wt);
    [/code]
  • : : How do I create an output file name using some of the values I have
    : : in an array?
    : :
    : : normally,
    : :
    : : FILE *wt;
    : : wt = fopen ("outputfile", "w");
    : : fprintf ...
    : : fclose(wt);
    : :
    : : so what I want is, instead of me specifying a name such as, the
    : : "outputfile", I want the program to get the name combining some
    : : values I have in an array.
    : :
    : : pls help me... and thanks in advance..
    : :
    :
    : Derefrence the array through its index:
    :
    : [code]:
    : FILE *wt;
    : [b]wt = fopen (names[0], "w");[/b]
    : fprintf ...
    : fclose(wt);
    : [/code]:


    thanks for the help, but it doesn't seem to work fine. What I'm storing in my array is a bunch of numbers. so I want to combine those numbers to make a name for the output file.
    how do I do this?
    thanks alot!
  • From [[http://www.codepedia.com/1/CppIntToStr]]:

    #include
    #include

    //Converts an int to std::string
    std::string itoa(const int x)
    {
    std::ostringstream o;
    if (!(o << x)) return "ERROR";
    return o.str();
    }


    int main()
    {
    const int myIndex = /*something*/ ;
    const std::string fileName = "MyFile" + itoa(myIndex) + ".txt";
    FILE * f = fopen(fileName.c_str(), "w");
    //...
    }
    [/code]


    bilderbikkel
  • : From [[http://www.codepedia.com/1/CppIntToStr]]:
    :
    : #include
    : #include
    :
    : //Converts an int to std::string
    : std::string itoa(const int x)
    : {
    : std::ostringstream o;
    : if (!(o << x)) return "ERROR";
    : return o.str();
    : }
    :
    :
    : int main()
    : {
    : const int myIndex = /*something*/ ;
    : const std::string fileName = "MyFile" + itoa(myIndex) + ".txt";
    : FILE * f = fopen(fileName.c_str(), "w");
    : //...
    : }
    : [/code]:
    :
    :
    : bilderbikkel


    sorry, but I do not understand this code. I'm trying to write this in C. Also I noticed itoa is not a std function. so pls help me to write this step by step.
    I think, I first have to get these numbers and concatenate them to make it as single integer. then cast it into string.
    here is what I have.

    int filename= year[0]+year[1];

    sprintf(some2, %d, filename);

    wt = fopen(some2, "w");

    I'm getting the following error messages.

    error: syntax error before
  • : : From [[http://www.codepedia.com/1/CppIntToStr]]:
    : :
    : : #include
    : : #include
    : :
    : : //Converts an int to std::string
    : : std::string itoa(const int x)
    : : {
    : : std::ostringstream o;
    : : if (!(o << x)) return "ERROR";
    : : return o.str();
    : : }
    : :
    : :
    : : int main()
    : : {
    : : const int myIndex = /*something*/ ;
    : : const std::string fileName = "MyFile" + itoa(myIndex) + ".txt";
    : : FILE * f = fopen(fileName.c_str(), "w");
    : : //...
    : : }
    : : [/code]: :
    : :
    : :
    : : bilderbikkel
    :
    :
    : sorry, but I do not understand this code. I'm trying to write this
    : in C. Also I noticed itoa is not a std function.
    There is no standard 'convert int to char*' function. That's why I GIVE you one called 'itoa', because there IS a standard function called 'atoi'.

    I do not know the C equivalent of 'itoa', but I've seen it in a C FAQ (try Google).

    : so pls help me to
    : write this step by step.
    : I think, I first have to get these numbers and concatenate them to
    : make it as single integer. then cast it into string.
    : here is what I have.
    :
    : int filename= year[0]+year[1];
    :
    : sprintf(some2, %d, filename);
    : wt = fopen(some2, "w");
    :
    : I'm getting the following error messages.
    :
    : error: syntax error before

  • : I believe in C you must use the standard function strcat. Other
    : people on this Board can help you with that. Or Google.
    :

    And some quotation marks around the %d to indicate it is a string ("%d") might help too.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • :
    : : I believe in C you must use the standard function strcat. Other
    : : people on this Board can help you with that. Or Google.
    : :
    :
    : And some quotation marks around the %d to indicate it is a string
    : ("%d") might help too.
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    yes I fixed that but still its not giving out what I want.
    this is the array of int values I have...
    year[6]= 52
    year[7]=23

    so my output name should look like 5223.
    I thought sprintf would cast the int to string but it does not concatenate these values...
    any help <_>


  • : :
    : : : I believe in C you must use the standard function strcat. Other
    : : : people on this Board can help you with that. Or Google.
    : : :
    : :
    : : And some quotation marks around the %d to indicate it is a string
    : : ("%d") might help too.
    : :
    : : Best Regards,
    : : Richard
    : :
    : : The way I see it... Well, it's all pretty blurry
    :
    : yes I fixed that but still its not giving out what I want.
    : this is the array of int values I have...
    : year[6]= 52
    : year[7]=23
    :
    : so my output name should look like 5223.
    : I thought sprintf would cast the int to string but it does not
    : concatenate these values...
    : any help <_>
    :
    :
    :

    anyone?
  • : anyone?

    You can use a combination of strcat() and sprintf().

    For example:
    [code]
    int pNum[MAX_INT] = {
    23,
    45,
    67
    };

    char* str = (char*)malloc(sizeof(int)*MAX_INT +1);
    int i=0;

    // insure we end in null terminator
    memset (str,0,sizeof(sizeof(int)*MAX_INT +1));

    // tedius method--convert int to string then concatenates it
    for (i=0; i<MAX_INT; i++) {

    char tmp[256];
    memset (tmp,0,256);
    sprintf (tmp, "%i", pNum[i]);
    strcat (str, tmp);
    }

    // print the string
    printf ("%s",str);

    free (str);
    [/code]
    The above will convert the array of ints into a single string.

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.mt2002.sitesled.com]Website[rightbr][/link][/size]
  • Noo... very unefficient! And MAX_INT?? Correct me if I'm wrong, but I believe that in the best case, that's defined to 32767. There is no need to allocate 32kb (16-bit system) or 2.1Mb (32-bit system). Plus, MAX_INT isn't ANSI C, INT_MAX is.

    A more efficient solution:

    [code]#include
    #include
    #include

    #if INT_MAX == 32767

    #define INT_DIGITS 5

    #elif INT_MAX == 2147483647

    #define INT_DIGITS 10

    #endif



    int main()
    {
    int pNum[] =
    {
    1,
    23,
    45,
    67,
    890,
    };
    int pNum_n = sizeof(pNum) / sizeof(int);

    char* str = malloc(sizeof(char) * pNum_n * INT_DIGITS + 1);
    char* str_ptr = str;
    int i;


    for(i=0; i<pNum_n; i++)
    {
    sprintf(str_ptr, "%d", pNum[i]);
    str_ptr += strlen(str_ptr);
    }

    printf ("%s", str);
    getchar();

    free (str);
    return 0;
    }[/code]
  • Thank you very much for your helps!! It's working good now =)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories