0

QUESTION

Open a file with keyboard input Write a C program that asks to the user a name of a file (the name might contain the directory path as well) and tries to open the file. The program displays a message “File open !” or “Error opening the file” (the program does not read the file, just tries to open it). Use gets() to get user’s input

So using the code below I tried to read in a file directory path and then copy this file directory path into fopen() then confirming whether or not the file had been opened or not. The file directory path is entered in by the user using gets().

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp;
    char directory[100];
    printf("Please enter the directory path of the file you wish to open.\n");
    gets(directory);
    fp = fopen("%s,directory","r");
    if(fp==NULL)
    {
        printf("\nCannot open file.");
    }
    else
    {
        printf("\nFile open!");
    }
    return 0;
}   

MY PROBLEM

No matter what I put in it doesn't work and I've copied file directory paths directly from other programs that I've created and I know work. Any ideas?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Stephen D
  • 19
  • 1
  • 5
  • 3
    See [Why is `gets()` to dangerous to use?](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). And tell your teacher off for writing an assignment that uses `gets()` — it should never be used. – Jonathan Leffler Mar 31 '16 at 21:14
  • 1
    You need to pass the directory name entered by the user to `fopen()` — which is quite separate from `printf()`, though it looks a bit as if you're trying to mix the notations. Use: `fp = fopen(directory, "r"):` which will probably still fail if the given name is a directory, but will work if it is a file. Also, it is a good idea to end messages with a newline, rather than (or sometimes as well as) starting the message with a newline. – Jonathan Leffler Mar 31 '16 at 21:19
  • 2
    Second what Jonathan Leffler said. Never use `gets`, under any circumstances. (Sorry to sound shrill, but this is important.) – Steve Summit Mar 31 '16 at 21:38

1 Answers1

1

fp = fopen("%s,directory","r");

This is really confusing. Why are you using %s and ""? Just use directory as parameter directly. e.g.

fopen(directory, "r"); // make sure _directory_ refers to file for this function to succeed

gets is also considered dangerous.

Community
  • 1
  • 1
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • Yeah I am aware of the danger that comes with the use gets it was just specified that in the program we must use gets to read in the users input (I don't know why). I tried using just directory as the parameter and it still won't work. – Stephen D Mar 31 '16 at 21:23
  • @StephenD Well like I said using just directory won't work, fopen needs file. You need to concatenate the directory with file – Giorgi Moniava Mar 31 '16 at 21:24
  • Apologies, I kept in the " and that's why it wasn't working, thanks a million. – Stephen D Mar 31 '16 at 21:24
  • 3
    @Stephen D *"I don't know why"*... because your course teacher has been delivering the same lecture for 20 years. – Weather Vane Mar 31 '16 at 21:38
  • 2
    @StephenD Please give us your teacher's email address so we can educate him on this. There's undefined behavior that it might be okay to depend on (and there are those who will disagree with me even on that), but `gets` is just in a whole nother league, and there's no excuse for ever using it in a C program, and even less excuse for teaching it. – Steve Summit Mar 31 '16 at 21:41
  • 1
    The primary 'advantage' of using `gets()` over `fgets()` is that it deletes the newline, which makes it simpler to use for scenarios like this. However, `gets()` was removed from the C standard in C11 because it is so dangerous. It was one of the ways the first Internet worm (Google search 'morris internet worm') used to get around way back in 1988. Using `gets_s()` if it is available (Microsoft) is a good replacement. – Jonathan Leffler Mar 31 '16 at 21:56
  • Failing that, wrap `fgets()` and have the wrapping code remove the newline. `char *fgets_wrapper(char *buffer, int bufsiz, FILE *fp) { char *rp = fgets(buffer, bufsiz, fp); if (rp) buffer[strcspn(buffer, "\n")] = '\0'; return rp; }`. Test program: `int main(void) { char str[32]; while (fgets_wrapper(str, sizeof(str), stdin) != 0) printf("%2zu: [%s]\n", strlen(str), str); return 0; }` — headers `` and ``. – Jonathan Leffler Mar 31 '16 at 22:00