C++ Stickies : snprintf() [Safe Version of sprintf() that doesn’t suffer from buffer overruns]

   int snprintf(char *str, size_t size, const char *format, ...);
    int vsnprintf(char *str, size_t size, const char *format, va_list args);

Writes output to the string str, under control of the format string format, that specifies how subsequent arguments are converted for output. It is similar to sprintf(3), except that size specifies the maximum number of characters to produce. The trailing nul character is counted towards this limit, so you must allocate at least size characters for str.

If size is zero, nothing is written and str may be null. Otherwise, output characters beyond the n-1st are discarded rather than being written to str, and a nul character is written at the end of the characters actually written to str. If copying takes place between objects that overlap, the behaviour is undefined.

Example,
#include <slack/std.h> #ifndef HAVE_SNPRINTF #include <slack/snprintf.h> #endif int main(int ac, char **av) { char buf[16]; char *str = buf; char *extra = NULL; int len; if (!av[1]) return EXIT_FAILURE; if ((len = snprintf(buf, 16, "%s", av[1])) >= 16) if (extra = malloc((len + 1) * sizeof(char))) snprintf(str = extra, len + 1, "%s", av[1]); printf("%s\n", str); if (extra) free(extra); return EXIT_SUCCESS; }
1 note

Using Stat Function to Read Stats of the file

#include 
<iostream>
using namespace std;
int main()
{

struct stat FileInfo;
char filename[20] = "clock.cpp";
//Attempt to get the attribute of the file
int Stat = stat(filename  , &FileInfo );
if (Stat == 0){
    cout << "Success\n";
    cout << "File Info: \n";
    cout << "Size in Bytes : " << FileInfo.st_size << endl;
    cout << "File's Owner Identification Number : " << FileInfo.st_uid << endl;
    }
else{
    cout << "Fail";
    }

return 0;
}

0 notes

Shell Scripting : Using Case Statement

Here’s an example, implementing case statement in shell scripting to build a menu.

6 notes

Computer Jargons


Code Monkey

Code Monkey

An insulting term to describe a poor programmer, usually who does not grasp basic or common programming concepts, and sometimes whose best coding capabilities can be described as “GoogleCut&Paste”.

Magic Number

Magic Number

A seemingly unusual and unexplained value that is used in a program and “makes it work”. 

(Source: )

1 note

Shell Scripting : Making Shell Based WPM Counter

Shell Scripting is fun and useful. It’s like Swiss Army Knife.. very handy in needy times. Here’s a small one, like an intro.

Save the below code to speed.sh and do

chmod 771 speed.sh

and run using

./speed.sh

Code:

#/bin/sh
#speed.sh: A very tiny utility to measure typing speed.

prompt=”Start typing a piece of text. Press Ctrl+D twice to finish”
echo $prompt
start_time=`date +%s`
words=`cat|wc -w`
end_time=`date +%s`
speed=`echo “scale=2; $words / ( ( $end_time - $start_time ) / 60 )”|bc`
echo “You have a typing speed of $speed words per minute.”

(Source: linuxjournal.com)

Notes

IEEE Paper Competition Region 4 2011

Final Version of IEEE Region 4 Paper Competition. It was a wonderful opportunity to meet student from other universities, share my ideas, get insight on others ideas and project.

The IEEE conference was held at Minnesota State University, Mankato on May 15, 2011. Three groups participated from St. Cloud State University. The Final Version for IEEE Paper Competition “RFID Shopping System”. This was a nice experience getting started some Whitepapers. Would like to thank Dr. Hossain (Our Senior Design advisor) for the support on this initiative.

RFID Shopping System

3 notes

Digital Oscilloscope using PIC18F4455

Final Project Report for my ECE 422 : Micro-controller Design. Our final design was to design an oscilloscope with Graphical User Interface using Windows Form and USB-CDC as appropriate communication protocol.

PIC 18F4455

1 note

dos2unix

Spent too much hours removing those ^M when you get your file from windows system to UNIX like system. Didn’t knew there was this utility while taking CSCI 311 : Operating System Concepts using MINIX; nevertheless better late than never.

(Source: linuxcommand.org)

3 notes