help formating old floppies

This is a discussion on help formating old floppies within the Networking/Device Communication forums, part of the General Programming Boards category; i have an whole bunch of old floppy discs with RAW format on them and i want to format them ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    44

    help formating old floppies

    i have an whole bunch of old floppy discs with RAW format on them and i want to format them to ntfs or fat but windows wont for some reason so i am venturing to create my own formating program what i need to know is how to write to specific memory and how to put the ntfs or fat format on these floppies. thank you in advanced

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    It seems you are wanting to a lot of OS-centered programming. You can only format the disk if you know how to put the FAT file system on the disk. NTFS AFAIK will not work on floppies.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    44
    how do you put FAT format on a floppy i checked wotsit.com but it had nothing on FAT do you know any other places that would tell me how

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by kwm32
    how do you put FAT format on a floppy i checked wotsit.com but it had nothing on FAT do you know any other places that would tell me how
    There is probably a reason that Windows can't format your floppies. Are they standard 31/2" 1.44MB disks?

    Anyway, probably the easiest way to do this would be to copy the raw contents of an existing, just formatted, floppy to a file. Then copy that file to the disk that you want to format.

    You will end up with more than one disk with the same volume id but I don't think that will be a problem. If it works you can always reformat them with Windows.

    Code:
    // Note: You may need admin priviliges to get raw access to A:
    
    hDrive = CreateFile(TEXT("\\.\A:"),GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
    
    ReadFile(hDrive, ...); // into buffer
    WriteFile(hFile, ...); // from buffer
    You would then reverse that to write the file to the floppy to be formatted.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    OK, I was bored. You don't need admin priviliges. I could only test on a formatted floppy(where it worked without a hitch) as I don't have an unformatted one available. However, assuming the disk is viable, it should work.

    If you really want info on the FAT format check http://www.pjrc.com/tech/8051/ide/fat32.html. Note that floppy disks are FAT12.

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    
    /* NOTE: This program is for NT/2000/XP only. */
    
    VOID WinError(LPCTSTR szMsg)
    {
    	_tprintf(_T("%s\nError Code: %d\n"), szMsg, GetLastError());
    	exit(GetLastError());
    }
    
    
    BOOL DoesFileExist(LPCTSTR szFile)
    {
    	return (GetFileAttributes(szFile) != INVALID_FILE_ATTRIBUTES);
    }
    
    
    INT CreateImageFromFloppy(LPCTSTR szFile)
    {
    	HANDLE hDrive, hFile;
    	BOOL   bRet;
    	DWORD  dwRead, dwWritten;
    	BYTE   buf[16384];
    
    	SetLastError(ERROR_SUCCESS);
    
    	hDrive = CreateFile(TEXT("\\\\.\\A:"), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
    	                    NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
    
    	if (hDrive != INVALID_HANDLE_VALUE)
    	{
    		hFile = CreateFile(szFile, GENERIC_WRITE, 0,
    		                   NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    
    		if (hFile != INVALID_HANDLE_VALUE)
    		{
    			do
    			{
    				bRet = ReadFile(hDrive, buf, sizeof(buf), &dwRead, NULL);
    
    				if (!bRet)
    				{
    					WinError(TEXT("Failed while reading from disk."));
    					break;
    				}
    
    				bRet = WriteFile(hFile, buf, dwRead, &dwWritten, NULL);
    
    				if (!bRet)
    				{
    					WinError(TEXT("Failed while writing to image file."));
    					break;
    				}
    
    			} while (dwRead != 0);
    
    			CloseHandle(hFile);
    		}
    		else
    		{
    			WinError(TEXT("Failed to open image file."));
    		}
    
    		CloseHandle(hDrive);
    	}
    	else
    	{
    		WinError(TEXT("Failed to open floppy drive."));
    	}
    
    	return GetLastError();
    }
    
    
    INT CreateFloppyFromImage(LPCTSTR szFile)
    {
    	HANDLE hDrive, hFile;
    	BOOL   bRet;
    	DWORD  dwRead, dwWritten;
    	BYTE   buf[16384];
    
    	SetLastError(ERROR_SUCCESS);
    
    	hDrive = CreateFile(TEXT("\\\\.\\A:"), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
    	                    NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
    
    	if (hDrive != INVALID_HANDLE_VALUE)
    	{
    		hFile = CreateFile(szFile, GENERIC_READ, 0,
    		                   NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    
    		if (hFile != INVALID_HANDLE_VALUE)
    		{
    			do
    			{
    				bRet = ReadFile(hFile, buf, sizeof(buf), &dwRead, NULL);
    
    				if (!bRet)
    				{
    					WinError(TEXT("Failed while reading from image file."));
    					break;
    				}
    
    				bRet = WriteFile(hDrive, buf, dwRead, &dwWritten, NULL);
    
    				if (!bRet)
    				{
    					WinError(TEXT("Failed while writing to disk."));
    					break;
    				}
    
    			} while (dwRead != 0);
    
    			CloseHandle(hFile);
    		}
    		else
    		{
    			WinError(TEXT("Failed to open image file."));
    		}
    
    		CloseHandle(hDrive);
    	}
    	else
    	{
    		WinError(TEXT("Failed to open floppy drive."));
    	}
    
    	return GetLastError();
    }
    
    
    int main(int argc, char * argv[])
    {
    	if (!DoesFileExist(TEXT("floppy.img")))
    	{
    		printf("Please insert source floppy and press ENTER...\n");
    		getchar();
    
    		CreateImageFromFloppy(TEXT("floppy.img"));
    	}
    
    	printf("Please insert target floppy and press ENTER...\n");
    	getchar();
    
    	CreateFloppyFromImage(TEXT("floppy.img"));
    
    	printf("Press ENTER to continue...\n");
    	getchar();
    	return 0;
    }
    P.S What has this got to do with networking anyway?

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    18
    This is also a "device communication" forum.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    OK, for completeness I "unformatted" a floppy using this program:

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    
    /* NOTE: This program is NT/2000/XP only. */
    
    VOID WinError(LPCTSTR szMsg)
    {
    	_tprintf(_T("%s\nError Code: %d\n"), szMsg, GetLastError());
    	exit(GetLastError());
    }
    
    
    INT UnformatFloppy(VOID)
    {
    	HANDLE hDrive;
    	BOOL   bRet;
    	DWORD  dwWritten;
    	BYTE   buf[16384] = { 0 };
    
    	SetLastError(ERROR_SUCCESS);
    
    	hDrive = CreateFile(TEXT("\\\\.\\A:"), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
    	                    NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
    
    	if (hDrive != INVALID_HANDLE_VALUE)
    	{
    		do
    		{
    			bRet = WriteFile(hDrive, buf, sizeof(buf), &dwWritten, NULL);
    
    			if (!bRet)
    			{
    				WinError(TEXT("Failed while writing to disk."));
    				break;
    			}
    		} while (dwWritten != 0);
    
    		CloseHandle(hDrive);
    	}
    	else
    	{
    		WinError(TEXT("Failed to open floppy drive."));
    	}
    
    	return GetLastError();
    }
    
    
    int main(int argc, char * argv[])
    {
    	printf("Please insert floppy to unformat and press ENTER...\n");
    	getchar();
    
    	UnformatFloppy();
    
    	printf("Press ENTER to continue...\n");
    	getchar();
    	return 0;
    }
    and reformatted it with the program I posted above and it worked perfectly.

    >>This is also a "device communication" forum.<<

    Thanks. I forgot that.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Just a final note. I successfully resurrected an old fuggered floppy with this program. However, this method of "formatting" does not find and mark bad sectors.

    Therefore, before you put any data on the newly formatted floppy, you should run:
    Code:
    chkdsk A: /r
    This will find and mark bad sectors.

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I added some code to set the volume ID and volume label. It is quick and dirty, so uses magic numbers instead of constants and structures. Error handling is minimal.

    Code:
    #define FLOPPY_SECTOR_SIZE 512
    
    BOOL ReadFloppySector(HANDLE hDrive, UINT nSector, LPBYTE buf)
    {
    	DWORD dwRead;
    	LONG nPosition = nSector * FLOPPY_SECTOR_SIZE; /* Don't overflow me! */
    
    	if (SetFilePointer(hDrive, nPosition, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER ||
    	    !ReadFile(hDrive, buf, FLOPPY_SECTOR_SIZE, &dwRead, NULL))
    		return FALSE;
    
    	return TRUE;
    }
    
    
    BOOL WriteFloppySector(HANDLE hDrive, UINT nSector, LPBYTE buf)
    {
    	DWORD dwWritten;
    	LONG nPosition = nSector * FLOPPY_SECTOR_SIZE; /* Don't overflow me! */
    
    	if (SetFilePointer(hDrive, nPosition, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER ||
    	    !WriteFile(hDrive, buf, FLOPPY_SECTOR_SIZE, &dwWritten, NULL))
    		return FALSE;
    
    	return TRUE;
    }
    
    
    BOOL SetFloppyVolumeInformation(HANDLE hDrive, LPCSTR szVolumeLabel)
    {
    	BYTE   buf[FLOPPY_SECTOR_SIZE];
    	UINT16 nResvdSecCnt, nFATSz16;
    	UINT8  nNumFATs;
    	UINT   nRootSector, i;
    	typedef struct {
    		CHAR Name[11];
    		BYTE Attr;
    		BYTE NotImportantNow[20];
    	} DIR_ENTRY;
    
    	if (!ReadFloppySector(hDrive, 0, buf))
    	{
    		WinError(TEXT("Failed to read first sector of drive."));
    		return FALSE;
    	}
    
    	*((UINT32 *) &buf[39]) = (UINT32) time(NULL); // Volume ID.
    
    	memset(&buf[43], ' ', 11);  // Volume Label.
    	memcpy(&buf[43], szVolumeLabel, min(strlen(szVolumeLabel), 11));
    
    	if (!WriteFloppySector(hDrive, 0, buf))
    	{
    		WinError(TEXT("Failed to write first sector of drive."));
    		return FALSE;
    	}
    
    	nResvdSecCnt = *((UINT16 *) &buf[14]);
    	nNumFATs     = *((UINT8  *) &buf[16]);
    	nFATSz16     = *((UINT16 *) &buf[22]);
    
    	nRootSector = nResvdSecCnt + (nNumFATs * nFATSz16);
    
    	if (!ReadFloppySector(hDrive, nRootSector, buf))
    	{
    		WinError(TEXT("Failed to read root directory."));
    		return FALSE;
    	}
    
    	/* Traverse root directory to find volume label */
    	for (i = 0; i < FLOPPY_SECTOR_SIZE; i += 32)
    	{
    		DIR_ENTRY * pDir = (DIR_ENTRY *) &buf[i];
    
    		printf("## NAME: %.11s\n",  pDir->Name);
    		printf("## ATTR: 0x%x\n\n", pDir->Attr);
    
    		if (pDir->Name[0] == 0x00) break;    // End of root directory.
    		if (pDir->Name[0] == 0xE5) continue; // Empty directory entry.
    
    		if (pDir->Attr == 0x08) // ATTR_VOLUME_ID
    		{
    			memset(pDir->Name, ' ', 11);
    			memcpy(pDir->Name, szVolumeLabel, min(strlen(szVolumeLabel), 11));
    		}
    	}
    
    	if (!WriteFloppySector(hDrive, nRootSector, buf))
    	{
    		WinError(TEXT("Failed to write root directory."));
    		return FALSE;
    	}
    
    	return TRUE;
    }
    Note that the volume label is found in two places. At offset 43 from the start of the disk and as an entry in the root directory.

    You can call SetFloppyVolumeInformation() from the end of the CreateFloppyFromImage() function.

    Complete fat code is available:
    http://www.google.com/search?q=ATTR_VOLUME_ID
    http://mona.sourceforge.jp/document/...pp-source.html

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    So kwm32, did it work?

    (kwm32 has PMs turned off)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf formating!
    By kamoda_pawel in forum C Programming
    Replies: 2
    Last Post: 01-18-2007, 05:40 AM
  2. cin and formating
    By Hexxx in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2004, 01:11 PM
  3. scanf and formating data
    By xddxogm3 in forum C Programming
    Replies: 23
    Last Post: 04-16-2004, 03:50 PM
  4. Need help formating Win XP
    By gqchynaboy in forum Tech Board
    Replies: 6
    Last Post: 11-13-2003, 12:42 PM
  5. screen formating
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-15-2001, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21