Wednesday, 9 January 2013

Static Data Member

Today, I am going to write a post on static data members in C++. I am writing this post after feeling so bad about a debugging competition which i failed once again.

Let’s see about static first.

“static”  means fixed literally. Its value is not lost even after the scope is lost. static variables.

Eg:
[sourcecode language="cpp" padlinenumbers="true"]
#include<iostream>
using namespace std;
int func()
{
static int a;
a++;
return a;
}
int main()
{
cout<< func()<<" "<<func()<<" "<<func();

}
[/sourcecode]


Output:

static

Even though the variable is declared inside func(), it is not intialized again and again. It is initialized only once.  But why the output is 3,2,1 and not 1,2,3 ?

The reason is “ Stack Evaluation”. Stack means LIFO(Last in First Out). So, the third func() will be executed first, then second, and finally first.

Rest we can see tomorrow :)

Sunday, 6 January 2013

Scribd API

This is going to my first post. I am currently building a site for checking plagiarism among projects uploaded in my site.

When building a site, i encountered a problem for viewing files of different formats like “.docx”, .”pptx”, “.doc”, “.ppt”, .pdf” and many. First i tried to read contents from a word document (“doc”) which i was able to succeed using anti-word. Then i tried to read contents form “.docx” and then i came to know that “.docx” is a zip and we can read contents from “word/document.xml” using PHP.

PHP has facility to read a .zip file. But in all these cases, i was able to get only the plain text and could display with their formatting. Indeed, i don’t know about and i couldn’t find a solution. Then in web i goggled for “ embedding the files “ in our webpage. Through that i came know about “scribd API” and several other API’s for “embedding the files”.

About Scribd API


I honestly don’t know much about this API. I just saw in http://www.scribd.com/developers how to integrate in our website. For using API, we need to signup for a free API account. After signing up, under the account setting in the API tab look up your “API key” and “API Secret” id.

Second Step:

Now download Scribd.php  from the site. There is a nice tutorial available in scribd developers site also about using scribd. php. Here, i will just say how to upload the doc using scribd API and how to retrieve using javascript.

Uploading a doc using Scribd API:


1) include scribd.php in your php file

2) Create a scribd object using “scribd(api_key,api_secret)”

3) Then use scribd->upload(“filename”,”file_ext”,”access”,”rev_id”);

4) On successful upload scribd API will return “doc_id” and “access_key”
[sourcecode language="php"]</pre>
<?php

require_once 'scribd.php';
<pre>$api=&quot;API Key&quot;;
$secret=&quot;API Secretj&quot;;

$scribd=new Scribd($api,$secret);</pre>
$api="7fs5j960mfy4seft72lbj";
$secret="7fs5j960mfy4seft72lbj";

$scribd=new Scribd($api,$secret);
//Object Created

//print_r($scribd);

$doc_type=$fileExt;
$access=null;
$rev_id=null;

$data=$scribd->upload($fileLoc,$doc_type,$access,$rev_id);
<pre>
/* $fileExt - Type of file to be uploaded
$fileLoc - Completer path of file
*/

?>
[/sourcecode]


5) After uploading $doc will contain an array consisting of “doc_id” and “access_key”.

Note: file Name should be complete. Your PHP version must support curl. Download the latest version of PHP.