I am writing a program in C not C++ how do I allow the user to input a name for the file, but not the extension. I will use the extension .csv always.

For simplicity sake let's use the perfectly unsafe gets() function.

...
char filename[80];
char extension[] = ".csv";

puts("Enter filename (no extension):")
gets(filename);
strcat(filename, extension);
...

This assumes that the user has some kind of brain. If the user is brainless you have to detect the period character in the entry string with strchr() and shoot back ...

>This assumes that the user has some kind of brain.
You mean the hypothetical imaginary user that university professors use to avoid teaching students the reality of error checking? I've never met such a user in real life.

>you have to detect the period character in the entry string with strchr()
Filenames with multiple periods are common. A perfectly valid filename would be destroyed by this method, resulting in an obscure error.

Hello,

I have seen a lot of error-checking mistakes in the computer labs. And also agree that multiple extensions .here.there are possible too. Also might want to make sure that you are not overwriting a legitimate file that may already exist.

Christian

Filenames with multiple periods are common. A perfectly valid filename would be destroyed by this method, resulting in an obscure error.

Don't we all know that one! I didn't advice to extirpate the filename, I said "shoot back", let the Dummkopf user know ...

I am eagerly waiting for Narue's and Christian's solution of Alfarata's problem!

>I am eagerly waiting for Narue's and Christian's solution of Alfarata's problem!
The design is flawed to begin with. There's no good solution. If you ask the user for a filename, it's only reasonable to require the full path. If you require the full path then what possible use could you have for discarding the extension and tacking on .csv? What if it's not a CSV formatted file?

This question has already been answered. Start a new discussion instead.