Hello,

I want to do classic "save as" function, how you know it from all programs, but I have a problem - when I put constant pathname in fopen function , like "file.txt" ,it works. But when I type a variable char[], it does nothing. Then I looked for function and find that pathname argument must be const char. I tried to make it another way - I was thinking about saving still as one name,for example "new.txt" and then rename it. But in function rename IS AGAIN THAT FUCKING CONST ! I tried many other ways,but nothing works - in all functions for working with files is CONST !

So, my question is, how it is done in all that programs? What is the correct way of conversion between const and variable? Is there some macro operator which will extract variable value I set as argument (something like ## but for values not names)? Or how they do that?

Thanks for every reply!

>>THAT ****ING CONST
Is there to assure you that the file name will not be changed on you while it's creating the file and is entirely appropriate. You should not try to circumvent it.

Please post the code of your "save as" function so we can see what you are doing. Then perhaps we can help you more.

OK, here is my save function:

<code>

void uloz2D(char nazevsouboru[])
{
char *NAZEVPR2 = nazevsouboru;
printf("%s\n",NAZEVPR2);
FILE *soubor2;
soubor2 = fopen(NAZEVPR2, "w");
...writing data...
fclose(soubor2);
}
</code>

Then, in main program I have global variable char otevrenysoubor[13] ,which indicates currently open file.

In WINAPI I have button "save as",which gives you access to textbox,where you must type new file name. Data from textbox are sent to char retezec[13].
When you enter new value:

<code>

strcpy(otevrenysoubor,retezec); uloz2D(otevrenysoubor);

</code>

Everything works OK,saving function is OK,rest of code too, only thing I want is to circumvent that const. If there was no such a limitation, it would be OK. I would not change path during saving or something, I just want to save to custom named file.

Thank you for trying to use [code] ...code tags... [/code]. The syntax is square brackets( '' ) though, not less-than and greater-than.

Do not circumvent the const, const is your friend.

Try changing your declaration of "NAZEVPR2" to a proper const declaration

const char *NAZEVPR2 = nazevsouboru;

This will declare NAZEVPR2 as a pointer to a const char.

If that doesn't work, try an explicit cast
soubor2 = fopen((const char *)NAZEVPR2, "w")

commented: Woking solution of problem & very fast reply! +1

This doesn't help. I just tried it and its the same - file is not saved.

Is there another way how to do it if not circumventing const? How would you do it?

I already told you how I would do it.

What do you mean by "file is not saved"? Do you mean that the file is created, but the data is incorrect, or the file flat out isn't created?

I mean that file is not created.

Sorry, it works! But only in testing program where is nothing just this. So I must find where is error in my program.

Thanks for help!

So this is my testing source,which works.

void uloz2D(char nazevsouboru[])
{
    const char *NAZEVPR2 = nazevsouboru;
    printf("%s\n",NAZEVPR2);
    FILE *soubor2;
    soubor2 = fopen((const char *)NAZEVPR2, "w");
    fprintf(soubor2,"A");
    fclose(soubor2);
}

    char otevrenysoubor[13];

int main()
{
    strcpy(otevrenysoubor,"soubor");
    char retezec[13];
    gets(retezec);
    fflush(stdout);
    strcpy(otevrenysoubor,retezec);
    uloz2D(otevrenysoubor);
    getchar();
    return 0;
}

When I try to get filename by other way than "gets",it again sucks like the last time - does everything with no error but doesn't create file.
For getting char[] from textbox, I use this function:

GetWindowText(GetDlgItem(hwnd, IDB_TEXTBOX1),retezec,32);
     strcpy(otevrenysoubor,retezec);
     uloz2D(otevrenysoubor);

I think that it is again problem with types,but I'm not sure - retezec is char[13],so it doesn't matter that in function description is some LPTSTR,text is copied well to "otevrenysoubor".
I tried some conversions but it's again the same.

Only case I get working save as function is to go into console and type name of file - but I don't want to go to console. Maybe some function which simulates string entry to console would help me.

perror() might come in handy, so perhaps try

void uloz2D(char nazevsouboru[])
{
    // You don't need the NAZEVPR2 variable here

    printf("%s\n", nazevsouboru);

    FILE *soubor2 = fopen(nazevsouboru, "w");
    if(soubor2)
    {
        fprintf(soubor2,"A");
        fclose(soubor2);
    }
    else
    {
        // Failed, what's the reason?
        perror(nazevsouboru);
    }
}

Are you sure about your last argument to GetWindowText() , that is the count of characters that fit in the input buffer. In other words,

char retezec[32] = "";
     GetWindowText(GetDlgItem(hwnd, IDB_TEXTBOX1), retezec, sizeof(retezec));

or maybe

TCHAR retezec[32] = _T("");
     GetWindowText(GetDlgItem(hwnd, IDB_TEXTBOX1), retezec, sizeof(retezec)/sizeof(*retezec));

I've been able to get fscanf() and scanf() to work as well, but they leave a dangling newline in the stream. This causes the getchar() near the end to read the newline and not pause the program. You could use fgets(), but that would require some additional string manipulation because it includes the newline in the string. If you try to use the string with the newline, fopen() doesn't like the newline at the end of the filename.

Perror give me ":Invalid argument". I had "\n" in string so I replaced it with "\0" and now it's ok. Thanks for kicking me to solution :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.