Answer the question
In order to leave comments, you need to log in
In what directory should the file be stored for reading from the program?
Tell me in which directory to put the file for reading, regarding the project (VS) so that the file can be opened along the path "file_name.ext"?
I have already tried to put in all the folders created by the studio, but the file still does not open. fopen("file.txt", "r")
But if you specify an absolute path (for example, to drive C), then everything works.fopen("c:\\file.txt", "r")
Answer the question
In order to leave comments, you need to log in
С:\\Projects\\myProject\\Debug\\file.txt == ..\\debug\\file.txt
С:\\Projects\\myProject\\Release\\file.txt == ..\\release\\file.txt
С:\\Projects\\myProject\\Other\\file.txt == ..\\other\\file.txt
С:\\Projects\\myProject\\file.txt == ..\\file.txt
...
fopen("..\\file.txt", "r")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
char* get_file_path(char* dir, char* fname)
{
dir = _getcwd(dir, MAX_PATH);
strcpy(&dir[strlen(dir)], "\\");
strcpy(&dir[strlen(dir)], fname);
return dir;
}
int main(int argc, char* argv[])
{
/* uncomment
if(argc != 1)
{
printf("error: usage myapp.exe file.txt\n");
return -1;
}
uncomment */
char dir[MAX_PATH];
char* file = "file.txt";//test
char* full_file_name = get_file_path(dir, file);// get_file_path(dir, argv[0])
FILE* fp = fopen(full_file_name, "r");
if(fp)
{
printf("Open\n");
fclose(fp);
}
printf("%s", full_file_name);
free(full_file_name);
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question