I am trying to read from a text file using ifstream, but for some reason when I use g++ to compile, the eof flag is set before anything is even read from the file. Testing if the file is in fact open does return true. When using visual studio to compile it works correctly. Even when the file "file.txt" exists, the program below prints 1 when compiled with g++.
#include <iostream>
#include <fstream>
int main() {
std::ifstream file("file.txt");
if (file)
std::cout << file.eof();
}
The following is from powershell
PS C:\Users\baat2\prosjekter\CPP\testdir> dir
Directory: C:\Users\baat2\prosjekter\CPP\testdir
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 24.01.2022 12:39 33 file.txt
-a---- 24.01.2022 12:39 138 testfile.cpp
-a---- 24.01.2022 12:47 130594 testfile.exe
PS C:\Users\baat2\prosjekter\CPP\testdir> g++ -o testfile.exe testfile.cpp
PS C:\Users\baat2\prosjekter\CPP\testdir> .\testfile
1
PS C:\Users\baat2\prosjekter\CPP\testdir> type testfile.cpp
#include <iostream>
#include <fstream>
int main() {
std::ifstream file("file.txt");
if (file)
std::cout << file.eof();
}
PS C:\Users\baat2\prosjekter\CPP\testdir> type file.txt
a file with
some text
to test
OS is Windows 10. Version info for g++ is given below
PS C:\Users\baat2\prosjekter\CPP\testdir> g++ -v
Using built-in specs.
COLLECT_GCC=C:\msys64\mingw64\bin\g++.exe
COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-11.2.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --enable-checking=release --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++,jit --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts --enable-libstdcxx-time --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-lto --enable-libgomp --disable-multilib --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev2, Built by MSYS2 project' --with-bugurl=https://github.com/msys2/MINGW-packages/issues --with-gnu-as --with-gnu-ld --with-boot-ldflags='-pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high -Wl,--disable-dynamicbase -static-libstdc++ -static-libgcc' 'LDFLAGS_FOR_TARGET=-pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high' --enable-linker-plugin-flags='LDFLAGS=-static-libstdc++\ -static-libgcc\ -pipe\ -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high\ -Wl,--stack,12582912'
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (Rev2, Built by MSYS2 project)