Im trying to make my program to either open or create a file if it does not exist, in "C://".

printf("Filename: ");
  FILE *fil;

This code opens or creates a file in the same folder as the program.

What i want is something like this:

printf("Filename: ");
  FILE *fil;

How do i write this ?

Recommended Answers

All 3 Replies

First, don't use scanf() because it won't take anything beyond the first space you type, consequently you can not enter a file name that includes spaces. You need to use fgets() for that. Next, you have to concantinate the two strings together.

char filename[260]; // max allows in MS-Windows path + filename
fgets(&filename[3], 257,stdin);
// now strip off the newline 
if( filename[strlen(filename)-1) == '\n')
    filename[strlen(filename)-1) = 0;

how to save a file in c ?

if you just want to use file name without space then use this

FILE *fp;
char filename[40];  // you can use max lenght 260


printf("\n Enter File Name:");


Be a part of the DaniWeb community

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