BruceV 0 Posted April 28 Hello, I have recently installed and registered C++ builder CE, my first test program is not behaving correctly. It's a trivial C++ program, the file open fails. There is nothing that I can see that is incorrect, I've coded with files for decades. Any suggestions appreciated. #include <iostream> #include <fstream> #include <conio.h> #include <tchar.h> using namespace std ; ifstream ifs ; int _tmain(int argc, _TCHAR* argv[]) { ifs.open ("C:\\Junk\\testfile.txt") ; if (!ifs.good()) // ** Also tried if (!ifs) { cout << "Can't open input file.\n"; while (!kbhit()) ; return 1 ; } cout << "Opened OK\n" ; ifs.close() ; while (!kbhit()) ; return 0 ; } Share this post Link to post
bdw_nz20 11 Posted April 29 That code works for me fine on 12.1, I guess the basic question is do you have a file the that input stream can load. If it doesnt exist it will fail 1 Share this post Link to post
Remy Lebeau 1394 Posted April 29 (edited) The C++ standard file library really sucks what it comes to error reporting. I would suggest using TFileStream or even CreateFile() directly to get better details about what the actual failure is. On a side note: kbhit() is a really old-school Borland function, it is not standard in either C or C++. Consider using C++'s standard std::cin.get() method instead. Try this: #include <iostream> #include <memory> #include <System.SysUtils.hpp> #include <System.Classes.hpp> int _tmain(int argc, _TCHAR* argv[]) { try { auto ifs = std::make_unique<TFileStream>(_D("C:\\Junk\\testfile.txt"), fmOpenRead); std::cout << "Opened OK\n"; } catch (const Sysutils::Exception &e) { std::wcout << L"Can't open input file. Error: " << e.Message.c_str() << "\n"; std::cin.get(); return 1; } std::cin.get(); return 0; } Or: #include <iostream> #include <windows.h> int _tmain(int argc, _TCHAR* argv[]) { HANDLE hFile = CreateFileW(L"C:\\Junk\\testfile.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); if (hFile == INVALID_HANDLE_VALUE) { DWORD dwError = GetLastError(); cout << "Can't open input file. Error: " << dwError << "\n"; std::cin.get(); return 1; } std::cout << "Opened OK\n"; CloseHandle(hFile); std::cin.get(); return 0; } Edited April 29 by Remy Lebeau 1 Share this post Link to post
BruceV 0 Posted April 30 Thanks Remy for the extremely useful response. Your first suggestion threw up 40 (!) linker errors which are all way above my pay grade, and I wouldn't trouble you to go anywhere with them. The second one, the standard WIN32 approach, works fine and will do what I want. So thanks again! If I ever find out why my initial approach isn't working, I'll post the details. Share this post Link to post
BruceV 0 Posted April 30 This is a mea culpa. I tested the suggested WIN32 alternative in a different project, and referenced a different .txt file. that worked. That raised suspicions, so I carefully inspected the .txt files I was trying to open. Somehow, the one that didn't work had had a second .txt appended, ie. it was testfile.txt.txt. The way that Windows displays file, that was not obvious, it was only when I checked the situation in a command window that the situation became clear. So my original code did work, when pointed at the correct filename. In hindsight, both responses to my post were of assistance, in generating enough doubt for me to relook at things. I also thought it important to exonerate the platform. Apologies for posting before investigating thoroughly. Share this post Link to post
Remy Lebeau 1394 Posted April 30 7 hours ago, BruceV said: Your first suggestion threw up 40 (!) linker errors which are all way above my pay grade, and I wouldn't trouble you to go anywhere with them. You probably need to recreate the project and make sure the VCL library is enabled. 7 hours ago, BruceV said: The second one, the standard WIN32 approach, works fine and will do what I want. That wasn't the point of the exercise, though. It was to get more details about why the file is not opening correctly. But, if CreateFile() succeeds and std::ifstream fails, then there has to be another factor at play that is not being accounted for. Share this post Link to post