🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #4127

Need help with unbuffered writes

#4127
From: Viraf Bankwalla
Date: Sat, 05 Jan 1991 00:03
102 lines
2774 bytes
Hi,

  I've got a question regarding file I/O in MS-DOS.

  I've noticed that although I may do a fflush (buffered I/O),
  or write, or _dos_write, the data is not always written out
  to file.

  On speaking with Miscrsoft, they informed me that the write
  may be committed by using INT 0x21, Function 0x68, on DOS 3.3
  or later.  Is there a way around this for earlier versions of
  dos, without opening and closing the file each time.

  In the below example, if USE_DOS_COMMIT is not defined,
  and I type in a string (say TEST), and power down the
  machine, the file has a size of 0 bytes.

  Thanks in advance.

  viraf bankwalla.
  viraf@seas.gwu.edu
  ...!uunet!gwusun!viraf.


/*----------------- SAMPLE CODE ------------------*/

#include <dos.h>
#include <fcntl.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

void main()
{
	char prompt[] = "Target exists. Overwrite? ", newline[] = "\n\r";
	int out_file, ch;
	unsigned ret, count;
	char str[256];
	char *out_name = "copy2.out";
	char _far *buf = str;
	union REGS inregs, outregs;

	ret = _dos_creatnew( out_name,	NORMAL, &out_file);
	if( ret == EXIST )
	{
		/* Use _dos_write to display prompts. Use bdos to call
		 * function 1 to get and echo keystroke.
		 */
		_dos_write( 1, prompt, sizeof( prompt ) - 1, &ch );
		ch = bdos( 1, 0, 0 ) & 0x00ff;
		if( (ch == 'y') || (ch == 'Y') )
			ret = _dos_creat( out_name, NORMAL, &out_file);
		_dos_write( 1, newline, sizeof( newline ) - 1, &ch );
	}
	if( ret )
		return;

	/*****
	**
	** Get one string at a time, and write
	** it out.
	**
	*****/
	for(;;)
	{
		cprintf("Enter string : "); cscanf("%s", str);

		if ((count = (int) strlen(str)) == 0)
			continue;

		if (stricmp(str, "quit") == 0)
		{
			printf("User quit\n");
			break;
		}

		if( (ret = _dos_write( out_file, buf, count, &count )) != 0)
		{
			printf(("Error writing to file"));
			return;
		}

#ifdef USE_DOS_COMMIT
		/*
		** Call the dos COMMIT interrupt
		*/
		inregs.h.ah = 0x68;		/* Dos commit function */
		inregs.x.bx = out_file;	/* Dos file handle	   */
		(void) intdos(&inregs, &outregs);

		cprintf("write status = %u\n", outregs.x.cflag);
#endif

	}

	/* Close files and free memory. */
	_dos_close( out_file );
	return;

}

/*----------------- END SAMPLE CODE ------------------*/
Technical Details
Message-ID: <2498@sparko.gwu.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!seas.gwu.edu!viraf

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!seas.gwu.edu!viraf
From: viraf@seas.gwu.edu (Viraf Bankwalla)
Newsgroups: comp.os.msdos.programmer
Subject: Need help with unbuffered writes
Message-ID: <2498@sparko.gwu.edu>
Date: 5 Jan 91 00:03:23 GMT
Reply-To: viraf@seas.gwu.edu ()
Organization: The George Washington University, Washington D.C.
Lines: 102