-1

Edit: Deleted all but the main question.

My program here is supposed to create a file at a specified directory, and write specified text to it. A correct file's path and content should look something like this:

Path: D:\test.txt
Content: The printing succeeded.

For some reason, my code won't recognize the "path" variable. I don't know what I'm doing wrong here. The "text" variable works fine.

#include<stdio.h>
int main()
{

//Declaring variables
char path[999];
char text[999];
FILE *fp;

//prompting for path variable
printf("Specify a file path.\n");
fgets(path,999,stdin);
printf(path);

//prompting for the text variable.
printf("What do you want to write?");
fgets(text,999,stdin);
printf(text);

//opening and printing to file.
//fp = fopen("D:\\test.txt", "w");
fp = fopen(path, "w");
fprintf(fp, text);
fclose(fp);

//test print to see that the program completed correctly.
printf("\nThe printing has been done.");
return 0;
}

The thing I don't understand is that fp = fopen("D:\\test.txt", "w"); works, but fp = fopen(path, "w"); doesn't. I've tried putting in these different paths.:

D:\\test.txt
D:\test.txt
D\test.txt
D\\test.txt
Punit Vara
  • 3,744
  • 1
  • 16
  • 30
Emerald Spire
  • 131
  • 1
  • 1
  • 10
  • 3
    **Always** check the result of functions which might encounter an error. If `fopen` fails, you invoke _undefined behaviour_ (just search for this term). – too honest for this site Dec 14 '15 at 15:21
  • 1
    Wow that's a lot of questions. I know you've got a lot you want answers to, but asking so many at the same time makes giving a coherent answer really difficult! As a general rule, try to limit your questions to a single subject, rather than an entire topic like this one, which is basically "how does C work?" You may find that people will only answer the "main" question and ignore the rest, because they are all answers that can be found in most C tutorials. – Ieuan Stanley Dec 14 '15 at 15:27
  • "What are some of the most useful header files?" srsly? how about "those that you actually need"? – nonchip Dec 14 '15 at 15:28
  • also, files do not behave. and as @IStanley said: read some tutorials… – nonchip Dec 14 '15 at 15:29
  • @IStanley Okay, thanks. I'm new to Stackoverflow, so I didn't know about that. :P – Emerald Spire Dec 14 '15 at 15:53

1 Answers1

2

It doesn't open the file when you open the variable path because fgets() reads the newline and puts it at the end of the string (if there's enough space in the buffer). In order to make it work you have to manually remove the newline from the string.

Try this before opening the file.

  if(isspace(path[strlen(path)-1]))
             path[strlen(path)-1]='\0';

You might also need to include <ctype.h>

MattSt
  • 1,024
  • 2
  • 16
  • 35
  • You should test if `path` is not an empty string too. – wimh Dec 14 '15 at 15:24
  • 1
    See also [Do not assume that fgets() or fgetws() returns a nonempty string when successful](https://www.securecoding.cert.org/confluence/display/c/FIO37-C.+Do+not+assume+that+fgets()+or+fgetws()+returns+a+nonempty+string+when+successful) – wimh Dec 14 '15 at 15:32
  • 1
    In corner cases, `path[0]` is the null character and then `path[strlen(path)-1]` is undefined behavior. Robust solutions at http://stackoverflow.com/q/2693776/2410359 – chux - Reinstate Monica Dec 14 '15 at 15:40
  • Thanks @matts & @olaf! I didn't know that fgets read the newline. – Emerald Spire Dec 14 '15 at 15:56
  • @EmeraldSpire: There is enough information on that behavoir on stack overflow and elsewhere to be found. Also there is documentation for every standard libray function in the C [standard](http://port70.net/~nsz/c/c11/n1570.html). Furthermore, your IDE and/or OS should provide such (on Linux see the man-pages). I'd recommend to first do some research on your own first. – too honest for this site Dec 14 '15 at 16:20
  • @olaf my IDE doesn't have a C documentation (At least one that I'm aware of.) – Emerald Spire Dec 14 '15 at 16:33