hello i would like someone to help me to create a code in C for prompting a user to name a text file and then open it please help me ASAP thank you.

hello i would like someone to help me to create a code in C for prompting a user to name a text file

use something like: fscanf(stdin,"%s", &test); where test is an array of characters

and then open it

Look at fopen for opening files.
If you make a start with your program and post it here, you will get more help.

>use something like: fscanf(stdin,"%s", &test);
Please don't do that. scanf (or fscanf with stdin) is best avoided for strings unless you know what you're doing. fgets is easier to get right for most people. If you still want to use scanf, at the very least you should provide a maximum field width for the string that corresponds to the size of the array and check for success:

char test[20];

if ( scanf ( "%19s", test ) == 1 ) {
  /* All is well */
}

Also, you don't use the address-of operator for arrays. On top of being dangerous at runtime, your code exhibits undefined behavior because &test doesn't match the expected type for the %s conversion specifier.

fscanf() may not work for this purpose-- (1) it will allow you to enter more characters than the buffer can hold which will cause your program to crash and (2) it stops reading the keyboard at the first space so the name you enter can not contain spaces.

The solution to both those problems is to use fgets()

char filename[255];
fgets(filename, sizeof(filename), stdin);

Now the above has its problems too. fgets() will add the '\n' (Enter key) to the end of filename, so you have to strip it off before using it for anything.

scanf (or fscanf with stdin) is best avoided for strings unless you know what you're doing. fgets is easier to get right for most people.
[....] Also, you don't use the address-of operator for arrays. On top of being dangerous at runtime, your code exhibits undefined behavior because &test doesn't match the expected type for the %s conversion specifier.

You're right ofcourse... @OP: Please disregard my useless remarks about fscanf. I fully agree that fgets is the best solution.
I have no idea why this brainfart escaped this morning... (monday perhaps). Thanks for clearing this up for the OP, AD & Narue

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.