0

I'm trying to read a file from a *.zip to TMemoryStream:

String fName = "myInfo.txt";
std::auto_ptr<TMemoryStream> input(new TMemoryStream);
std::auto_ptr<TStringList> sFile(new TStringList);
std::auto_ptr<TZipFile> zip(new TZipFile());
TZipHeader fHeader;
zip->Open(file, zmRead);
zip->Read(fName, input.get(), fHeader);
...

but I get an error:

E2285 Could not find a match for 'TZipFile::Read(UnicodeString,TMemoryStream *,TZipHeader)'

I am an amateur programmer, and I would really appreciate your help. Unfortunately, Google does not find an answer in this topic.

What am I doing wrong?

I was looking for examples of using TZipFile.Read for C++Builder.

New contributor
André is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1

2 Answers 2

0

Per the documentation, the Stream parameter is an output parameter, not an input parameter. Read() will create its own stream object and return back a pointer to it.

The parameter is a non-const reference to a TStream* variable, and a non-const reference can't bind to a temporary, so you can't pass in the temporary returned by auto_ptr::get(). You will have to pass in an actual non-auto raw TStream* variable, and then construct the auto_ptr from that raw pointer, eg:

String fName = "myInfo.txt";

std::auto_ptr<TZipFile> zip(new TZipFile);
zip->Open(file, zmRead);

TStream* pStream = NULL;
TZipHeader fHeader;
zip->Read(fName, pStream, fHeader);
std::auto_ptr<TStream> input(pStream);

std::auto_ptr<TStringList> sFile(new TStringList);
sFile->LoadFromStream(input.get());
...
4
  • I tryed this code but "input" is not recive any data. It's empty.
    – André
    yesterday
  • My bad, I put the wrong filename in the Open() call. I have fixed that. yesterday
  • It is not a problem with file name.
    – André
    yesterday
  • @André there is no functional difference between the code I showed in my answer vs the "working" code you have now posted in your own answer. Except that I'm still using auto_ptr to free the stream, and you have abandoned that. But as far as the code calling Read() is concerned, the two codes are functionally identical. yesterday
0

Finally it worked. The error was in the TStream declaration. Thank you very much for help. And here is the code that works.

String file = "myZipFile.zip";
String fName = "myFile.txt";
std::auto_ptr<TStringList> sFile(new TStringList);
std::auto_ptr<TZipFile> zip(new TZipFile());
zip->Open(file, zmRead);

TStream *fStream = NULL;
TZipHeader fHeader;

zip->Read(fName, fStream, fHeader);

sFile->LoadFromStream(fStream, TEncoding::GetEncoding(65001));
zip->Close();
...

I am extremely grateful for your help

New contributor
André is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • TEncoding::GetEncoding(65001) should be TEncoding::UTF8 instead. And you need to delete the TStream object when you are done using it (which auto_ptr can do for you). yesterday
  • These codes are not identical. With auto_ptr you create an 'input; stream, which I don't use. In that case, I don't have to delete it. UTF8 has a code page 65001 so the effect is the same.
    – André
    yesterday
  • I said they were functionally identical. In my example, the pStream pointer and the input auto_ptr are pointing at the same TStream object. In your code, you originally used an auto_ptr but you weren't using it correctly,and now you've abandoned it, creating a memory leak if you don't delete the TStream yourself. yesterday
  • Also, TEncoding::GetEncoding() returns a poo get to a new TEncoding object that you must delete when you are done using it, otherwise it is leaked. Whereas TEncoding::UTF8 returns a pointer to a singleton TEncoding object that the RTL owns and will free for you during program exit. yesterday

Your Answer

André is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.