Jump to content
BruceV

File opening error C++ builder CE trivial program

Recommended Posts

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

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

  • Thanks 1

Share this post


Link to post
Posted (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 by Remy Lebeau
  • Thanks 1

Share this post


Link to post

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

 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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×