Pages

Monday, October 23, 2017

Reading a line in C++





C++ program to read a line using getline() function.

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string my_str;
    
    cout << "Enter your name:";
    getline (cin, my_str);
    cout << "Your name is " << my_str;
    
    return 0;
}

Output of program

Enter your name:SCS AU
Your name is SCS AU

Friday, October 20, 2017

Decimal to Hexadecimal and Octal in C++

Decimal to Hexadecimal and Octal in C++.
This program will display decimal number into Hexadecimal and Octal.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  int number=100;
  cout << "Hexadecimal of 100 is: ";
  cout << hex << number << endl;
  cout << "Octal of 100 is: ";
  cout << oct << number << endl;
  return 0;
}

Output of program

Hexadecimal of 100 is: 64
Octal of 100 is: 144


Formatted output in C++


Formatted output in C++
#include <iostream>
using namespace std;
int main()
{
  cout.precision(5) ;
  cout.width(10);
  cout << 12.345678 << "\n"; // output 12.346

  cout.fill('-');
  cout.width(10);
  cout << 12.345678 << "\n"; // output ----12.346

cout.width(10);
  cout << "Hello!" << "\n"; // Output ----Hello!
  cout.width(10);

cout.setf(ios::left); // left justify
  cout << 12.345678; // Output 12.346----
  return 0;
}

Output of program

    12.346
----12.346
----Hello!
12.346----

Sunday, October 15, 2017

Read one word from a file.



Following C++ program will read a word from data.txt file stored in the same location where source program is stored.

#include <fstream>
#include <iostream>
using namespace std;

int main () {
   
   char word[20];

   ifstream fin;    //open file to read.
   fin.open("data.txt");
 
   cout << "Reading a word from a file...\n";
   
   fin >> word;
   cout << word;    //write data to screen.
 
   fin.close();     //close the opened file.
   return 0;
}

Output of program

Reading a word from a file...
hello

Tuesday, October 10, 2017

FTP server client simulation using socket program

This JAVA socket program will download webpage (HTML file) from server to client. Downloaded file will be opened into web browser.

SERVER side program

import java.io.*;
import java.net.*;

public class FTPServer {
  public static void main(String[] args) throws IOException {
    //server will start at port number 12345.
    ServerSocket servsock = new ServerSocket(12345);

    //server will send index1.htm file to client.
    File myFile = new File("D:\\index1.htm");
    System.out.println("Waiting for Client Request...");
    while (true) {
      Socket sock = servsock.accept();
      byte[] mybytearray = new byte[(int) myFile.length()];
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
      bis.read(mybytearray, 0, mybytearray.length);
      OutputStream os = sock.getOutputStream();
      os.write(mybytearray, 0, mybytearray.length);
      os.flush();
      sock.close();
    }
  }
}

SERVER side Output:

Waiting for Client Request...



CLIENT side program

import java.awt.Desktop;
import java.io.*;
import java.net.*;

public class FTPClient {
  public static void main(String[] argv) throws Exception {

    //socket will be created local IP: 127.0.0.1 and port 12345.
    Socket sock = new Socket("127.0.0.1", 12345);
    byte[] mybytearray = new byte[1024];
   
    InputStream is = sock.getInputStream();
    FileOutputStream fos = new FileOutputStream("D:\\index2.htm");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
   
    int bytesRead = is.read(mybytearray, 0, mybytearray.length);
    bos.write(mybytearray, 0, bytesRead);
   
    System.out.println("File downloaded...");
   
    Following code will open index2.htm file in browser.

    String url="D:\\index2.htm";
    File htmlFile=new File(url);
    Desktop.getDesktop().browse(htmlFile.toURI());

    bos.close();
    sock.close();
  }
}

CLIENT side output:

File downloaded...
BUILD SUCCESSFUL (total time: 1 second)