Pages

Sunday, November 18, 2012

VC++ Receive a stream of text from a system(Command) process

more at MSDN 
 
// crt_popen.c
/ This program uses _popen and _pclose to receive a 
// stream of text from a system process.
//

#include <stdio.h>
#include <stdlib.h>

int main( void )
{

   char   psBuffer[128];
   FILE   *pPipe;

        /* Run DIR so that it writes its output to a pipe. Open this
         * pipe with read text attribute so that we can read it 
         * like a text file. 
         */

   if( (pPipe = _popen( "dir *.c /on /p", "rt" )) == NULL )
      exit( 1 );

   /* Read pipe until end of file, or an error occurs. */

   while(fgets(psBuffer, 128, pPipe))
   {
      printf(psBuffer);
   }


   /* Close pipe and print return value of pPipe. */
   if (feof( pPipe))
   {
     printf( "\nProcess returned %d\n", _pclose( pPipe ) );
   }
   else
   {
     printf( "Error: Failed to read the pipe to the end.\n");
   }
}

VC++ Clearing the Screen

more at MSDN 
 
#include <windows.h>

void cls( HANDLE hConsole )
{
   COORD coordScreen = { 0, 0 };    // home for the cursor 
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi; 
   DWORD dwConSize;

// Get the number of character cells in the current buffer. 

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

   // Fill the entire screen with blanks.

   if( !FillConsoleOutputCharacter( hConsole,        // Handle to console screen buffer 
                                    (TCHAR) ' ',     // Character to write to the buffer
                                    dwConSize,       // Number of cells to write 
                                    coordScreen,     // Coordinates of first cell 
                                    &cCharsWritten ))// Receive number of characters written
   {
      return;
   }

   // Get the current text attribute.

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   // Set the buffer's attributes accordingly.

   if( !FillConsoleOutputAttribute( hConsole,         // Handle to console screen buffer 
                                    csbi.wAttributes, // Character attributes to use
                                    dwConSize,        // Number of cells to set attribute 
                                    coordScreen,      // Coordinates of first cell 
                                    &cCharsWritten )) // Receive number of characters written
   {
      return;
   }

   // Put the cursor at its home coordinates.

   SetConsoleCursorPosition( hConsole, coordScreen );
}

int main( void )
{
    HANDLE hStdout;

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    cls(hStdout);
    
    return 0;
}

Saturday, November 17, 2012

VC++ Important pre-define pre-processor

_UNICODE
Is unicode set or not set

_DEBUG
Is configuration set to debug or release

Wednesday, September 12, 2012

C++ CODE Set File Attributes to Read or Write

Include following header files

#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <io.h>


//Code Start


#include "stdafx.h"

void printMyMsg();

int setRead(wchar_t *);
int setWrite(wchar_t *);

char * wcharToChar(wchar_t *);

int _tmain(int argc, _TCHAR* argv[])
{
    if(argc < 3)
    {
        printMyMsg();
        exit(2);
    }
    if(!_wcsicmp(argv[1],L"-r"))
    {
        setRead(argv[2]);
        exit(2);
    }
    else if(!_wcsicmp(argv[1],L"-w"))
    {
        setWrite(argv[2]);
        exit(2);
    }
    else
    {
        printMyMsg();
        exit(2);
    }

    return 0;
}

int setRead(wchar_t * fname)
{
    return(_chmod(wcharToChar(fname),_S_IREAD));
}
int setWrite(wchar_t * fname)
{
    return(_chmod(wcharToChar(fname),_S_IWRITE));
}
void printMyMsg()
{
    printf("\nUsage: SetFileAttribute -R|-W <filename>");
    printf("\n");
    system("pause");
}
char * wcharToChar(wchar_t * inStr)
{
    size_t len = wcslen(inStr);
    char * tchar = (char *)malloc(len+1);
    wcstombs_s(NULL,tchar,len+1,inStr,len+1);
    return(tchar);
}

C++ Convert Between Various String Types

online tool to convert from one datatype to another

http://www.convertdatatypes.com/Language-CPlusPlus.html

Friday, June 15, 2012

MSDN usefull Links

C++ UNREFERENCED_PARAMETER

Let's start with UNREFERENCED_PARAMEER. This macro is defined in winnt.h, like so:

#define UNREFERENCED_PARAMETER(P) (P)
 

In other words, UNREFERENCED_PARAMETER expands to the parameter or expression passed.Its purpose is to avoid compiler warnings about unreferenced parameters.Many programmers, including yours truly,
like to compile with the highest warning level,Level 4 (/W4).Level 4 warnings fall into the category of "things that can be safely ignored." Little infelicities that won't break your code, though they might make you look bad.For example, you might have some line of code in your program like this

int x=1;

but you never use x. Perhaps this line is left over from a time when you did use x,but then you removed the code and forgot to remove the variable.Warning Level 4 can find these minor mishaps.So why not let the compiler help you achieve the highest level of professionalism possible? Compiling with Level 4 is a way to show pride in your work. Level 4 is de rigueur if you're writing a library for public consumption. You don't want to force your developers to use a lower level to compile their code cleanly.

MSDN Link