Click to See Complete Forum and Search --> : GetVolumeInformation writing to file


eustace
November 22nd, 2007, 08:23 AM
Hello,

I'm making a call to GetVolumeInformation and trying to write the results to a file, however the output I get is incomprehensible in one case and null in the other. I'm very new to c++ so was wondering if anyone can spot what is going wrong. Here's what I've got so far:


#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>

#include "windows.h"

namespace std {}
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
printf("getting volume info");

CHAR szVolumeNameBuffer[12]; //this will have the name of your drive
DWORD dwVolumeSerialNumber; //this will have the serial number of your drive
DWORD dwMaximumComponentLength; //this is the max length in between each \ in a path
DWORD dwFileSystemFlags; //this will return flags about the drive's file system
CHAR szFileSystemNameBuffer[10]; //this will contain the type of file system (ex: NTFS)

GetVolumeInformation((LPCWSTR)"C:\\", (LPTSTR)szVolumeNameBuffer, 12, &dwVolumeSerialNumber, &dwMaximumComponentLength, &dwFileSystemFlags, (LPWSTR)szFileSystemNameBuffer, 10);

string s = szVolumeNameBuffer;
string d = szFileSystemNameBuffer;

string string1;
string1.assign( szVolumeNameBuffer, szVolumeNameBuffer+25);

ofstream myfile;
myfile.open ("example.txt");
myfile << string1;
myfile.close();

FILE * pFile;
int n;
char name [100];

pFile = fopen ("myfile.txt","w");
fprintf (pFile, "Filesystem %d [%-10.10s]\n",1,d);
fclose (pFile);

return 0;
}


example.txt output:

ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌñà¨ÿ ‘

myfile.txt output:

Filesystem 1 [(null)

I'm using VS5 on winxp if it helps.
Thanks
E

eustace
November 22nd, 2007, 09:24 AM
Changed the call to the ANSI version and can now read the output in notepad.

GetVolumeInformationA((LPCSTR)"C:\\", (LPSTR)szVolumeNameBuffer, 12, &dwVolumeSerialNumber, &dwMaximumComponentLength, &dwFileSystemFlags, (LPSTR)szFileSystemNameBuffer, 10);

Tried the unicode version, opened in notepad++ with UTF-8 encoding, but got gibberish still. How can I fix this as the unicode version would be best for me?