Can't Open Txt Files in C++ Program with Visual Studio 2019

Cant open a file in Visual Studio 2019

By default, Visual Studio C++ projects execute with the directory containing the .vcxproj file as the working directory (where all file operations are relative to).

You can see this if you right click your project in the "Solution Explorer" -> "Properties" menu item. The on the left of the new window select "Debugging". On the right the "Working Directory" item is most likely set to "$(ProjectDir)".

project folder but no success.

So assuming you didn't change that setting, this should definitely work. Make sure you did place the file there, and that it is properly named (if using Explorer, be sure to have "File name extensions" enabled under "View", so you don't end up making like a myfile.txt.txt by mistake).

It is also possible opening the file fails for some other reason (unfortunately C++ error reporting on this is extremely limited). For example if the file permissions do not allow your program to read it.

If still no luck, you could maybe try writing a file, and see where it puts it.

ofstream file_writer("lostfile.txt");

Why can't I read a text file in C using Visual Studio?

The problem you'll have even if the file is found is that you lose the first char of your input.

Correct way of doing it:

errno_t err;

if ((err=fopen_s(&pInputFile, "Text.txt", "r"))==0)
{
// file exists: don't read a char before the loop or
// it will be lost

while ((chr = getc(pInputFile)) != EOF)
{
printf("%c", chr);
}

fclose(pInputFile);
}
else
{
fprintf(stderr,"Cannot open file, error %d\n",err);
// handle the error further if needed
}

unable to open file in visual studio

you need to keep the file in the directory where the executable is generated by default when using Visual Studio. this is typically located in your solution directory under a folder called Debug/Release depending on your configuration. check the project settings to see where the executable will be generated and copy the file there.

C++ fstream won't open a text file in windows

You can set the working directory for the Visual Studio in the project properties, Debugging tab.

The default value is $(ProjectDir).

It's a very dangerous practice to assume that the files you need are in the current working directory - it can change depending on how you start your program. You should either use a "well-known" path (like APPDATA on windows) or a path relative to your executable (then you can build that path at run time).

visual studio c++ how to properly use fopen() to open txt file

Note escape sequences of string literals, so your path:

static const char* full_name = "C:\Users\Lukas\Desktop\Programming\file_system_test\file_system_test\items.txt";    

contains \f escape sequence which is interpreted as form feed - new page byte 0x0c in ASCII encoding. This character can't be part of of a path so Invalid argument error is reported.

Also compilers complain that other escape sequences are unknown.

There are three ways to fix it.

  1. As Luka Rahne suggested by using back slash escape sequence \\
  2. Or by using forward slashes (since C suppose to be portable, standard library is able to convert Unix path separator to platform specific path separators).
static const char* full_name = "C:/Users/Lukas/Desktop/Programming/file_system_test/file_system_test/items.txt";

  1. If you are using C++11 or newer (your code is C not C++, but tag says C++), you can leverage raw string literal:
static const char* full_name = R"(C:\Users\Lukas\Desktop\Programming\file_system_test\file_system_test\items.txt)";

Here I did some live testing with msvc (file named: open.c):

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

int main(int argc, const char argv[])
{
#if VERSION == 0
// here '\f' is used to reproduce error "invalid argument":
static const char name[] = "C:\fUsers\\User\\Downloads\\open.c";
#elif VERSION == 1
static const char name[] = "C:\\Users\\User\\Downloads\\open.c";
#elif VERSION == 2
static const char name[] = "C:/Users/User/Downloads/open.c";
#elif VERSION == 3
static const char name[] = R"(C:\Users\User\Downloads\open.c)";
#endif
FILE* f = fopen(name, "r");

if (!f) {
perror("fopen");
return 1;
}

char buf[256] = "";

fgets(buf, sizeof(buf), f);
printf("%s\n", buf);

fclose(f);

return 0;
}

Here is result of compiling and running from cmd.exe:

C:\Users\User\Downloads>cl open.c /D VERSION=0 && open.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.

open.c
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.

/out:open.exe
open.obj
fopen: Invalid argument

C:\Users\User\Downloads>cl open.c /D VERSION=1 && open.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.

open.c
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.

/out:open.exe
open.obj
#include <stdlib.h>

C:\Users\User\Downloads>cl open.c /D VERSION=2 && open.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.

open.c
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.

/out:open.exe
open.obj
#include <stdlib.h>

C:\Users\User\Downloads>cl open.c /D VERSION=3 && open.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.

open.c
open.c(11): warning C4129: 'm': unrecognized character escape sequence
open.c(11): warning C4129: 'D': unrecognized character escape sequence
open.c(11): warning C4129: 'o': unrecognized character escape sequence
open.c(11): error C2065: 'R': undeclared identifier
open.c(11): error C2143: syntax error: missing ';' before 'string'
open.c(11): error C2099: initializer is not a constant

So everything works as I described and last version 3 fails since I compile code as C.



Related Topics



Leave a reply



Submit