Click here to Skip to main content
16,015,504 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Correct me if im wrong. As per Structured Storage, Storage corresponds to Directory/folder and Stream corresponds to file.
Now Using Structured Storage concept, I have created a storage using StgCreateDocfile() with .stg extension. I have created a Stream inside it using IStream->CreateStream().
I want add my Hard disk file eg. Abstract.doc to this stream of created Storage(.stg). How can I do that??

I tried it using IStream::Write method
C++
HRESULT Write( 
  void const* pv, 
  ULONG cb,
  ULONG* pcbWritten 
);


can pointer pv be the direct pointer to my hard disk file eg. here abstract.doc instead of Buffer?

Thanking you in advance....
Posted
Updated 16-Mar-11 11:20am
v3

1 solution

Yes you are right. You can consider like that.
You need to read your file content into a buffer (partially or as a whole). Then you call IStream::Write().
#define BUFFERSIZE 4096  // (edit)

unsigned char * pBuffer = new unsigned char [BUFFERSIZE];

HANDLE hFile = CreateFile(pszFileName, ...);

// you can determine file size and set initially
// pStream->SetSize(...)

DWORD dwRead = 0;
ULONG ulWritten = 0;
do
{
    if(!ReadFile(hFile, pBuffer, BUFFERSIZE, &dwRead, NULL))
        break;

    if(0 == dwRead) // means EOF
        break;

    pStream->Write(pBuffer, dwRead, &ulWritten);

    // you can also check (ulWritten == dwRead) just to make sure

} while(BUFFERSIZE == dwRead);    // be careful (edit)

CloseFile(hFile);

delete [] pBuffer;
 
Share this answer
 
v4
Comments
Ozer Karaagac 16-Mar-11 17:40pm    
BTW, You can also use CFile or CStdioFile MFC classes for file operations if you are using MFC.
MartinTaylor 16-Mar-11 18:01pm    
Thank u , Sir
Ozer Karaagac 16-Mar-11 18:16pm    
Not at all!
I had written pStorage instead of pStream mistakenly. I've fixed it though IStorage doesn't have a Write method.
MartinTaylor 22-Mar-11 11:54am    
Now i have tried the reverse procedure..to restore file(stream) from storage i am getting first 1024 bytes as junk chars like "ÍÍÍÍÍÍÍÍÍÍÍÍ...." and bcoz of my mistake in 'do..while()' condn of data writting, the restored file size is going in MB and increasing rapidly for following code..
#include <stdio.h>
#include<conio.h>
#include <windows.h>
#include<shobjidl.h>
#include <ole2.h>
#include<objidl.h>
#define BUFFERSIZE 1024

EXTERN_C void wmain()
{
IStorage* pStorage;
IStream* pStream;
HRESULT hr,hr1,hr3;
hr = StgOpenStorageEx(L"E:\\Root2.stg",STGM_READ|STGM_SHARE_DENY_WRITE,STGFMT_DOCFILE,0, NULL, 0,IID_IStorage,(void**)&pStorage);
if(S_OK==hr)
{
printf("Storage opened........\n");
hr1=pStorage->OpenStream(L"file.txt",NULL, STGM_READ | STGM_SHARE_EXCLUSIVE,0,&pStream);
if(hr1==S_OK)
{
unsigned char * pBuffer = new unsigned char [BUFFERSIZE];
HANDLE hFile = CreateFile(L"E:\\sumit2.txt",GENERIC_WRITE,FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile != INVALID_HANDLE_VALUE)
printf("\n\n\n File created");
DWORD dwWrite=0 ;
ULONG ulRead=0 ;
do
{
hr3=pStream->Read(pBuffer,dwWrite, &ulRead);
if(hr3==S_OK)
printf("\t Read Successful");
WriteFile(hFile, pBuffer, BUFFERSIZE, &dwWrite, NULL);
if(0 == dwWrite) // for checking EOF
break;
} while(ulRead = dwWrite); // also tried with dw
printf("\n\n DONE");
CloseHandle(hFile);
delete [] pBuffer;
//------------------------------------------
pStream->Release();
}
pStorage->Release();
}
_getch();
}
Sir plz help me out of this...
Ozer Karaagac 22-Mar-11 12:40pm    
I posted a reply for your new Q.
http://www.codeproject.com/Questions/171679/Problem-in-Reading-Data-from-stream-of-Structured-.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900