Friday, July 30, 2010

Programa juguete para recordar comportamiento de buffers en C.

//toy_buffers.c
//A toy program to test buffer behaviours.
//30-JUL-2010


//include libraries
#include
#include
#include
#include

#include
#include

#include


//define constants and macros
#define BUFFERSIZE 1024


//define functions prototypes
void fPutGetBuffer(char[], char[]);


//
// Note: this simplified sample assumes the file to read is an ANSI text file
// only for the purposes of output to the screen. CreateFile and ReadFile
// do not use parameters to differentiate between text and binary file types.
//


//main() function
void __cdecl _tmain(int argc, TCHAR *argv[])
{
//variable declarations for main function
char PutBuffer[] = "This is a string to send."; //buffer to send and concatenate in invoked function
char GetBuffer[BUFFERSIZE] = {0}; //buffer to fill from the invoked function with the new concatenated data

fPutGetBuffer(PutBuffer, GetBuffer); //call to fPutGetBuffer(). parameters are: 1. buffer with data. 2. buffer to receive data from inside the function

printf("%s\n", GetBuffer); //print and check received data from inside invoked function
}


//fPutGetBuffer() function declaration
void fPutGetBuffer(char inBuffer[], char outBuffer[])
{
char LocalData[] = "This is a LOCAL string inside fPutGetBuffer() function."; //local buffer with data
char LocalNewBuffer[BUFFERSIZE] = {0}; //local buffer to fill with data

strcpy(LocalNewBuffer, LocalData); //copy local data in local buffer to fill

strcat(LocalNewBuffer, inBuffer); //build local buffer using passed data as parameter from outside function

strcpy(outBuffer, LocalNewBuffer); //copy local builded buffer to outside buffer passed as parameter
}



//EOS

No comments: