Read From File In C++

June 7th, 2008 | No Comments | Posted in C/C++

In the previous tutorial we explained how to do that in C#, now we will do that in C++.
So, create a Win32 C++ Console Application. This tutorial will be similar to the tutorial for writing to file in c++,
So, write this code

#include <iostream>   //cout
#include <fstream>    //ifstream
 
using namespace std;
 
int main()
{
	ifstream in;
	in.open("text.txt");
 
	char line[256];
 
	while(in.getline(line, 256))
	cout << line << endl;
 
	in.close();
}

Here we read the file line by line, and write its content to the console.
We have one char variable where we will store that lines, it will be long 256 characters. We are using the ifstream class, and creating an object from it, opening the file, do our work, and in the end we are closing the handle from our application to the file we read.
That's it. Very simple.

Tags:

Read From File Using StreamReader Class

May 26th, 2008 | 2 Comments | Posted in C#

In this post we explained how to write text to a file in C#. Today we will explain reading text from a file using the StreamReader class.
So create a file "text.txt" in the same directory where is the executable of the project. We will create a C# Console Application.
First, we must include this namespace, to use the StreamReader class

using System.IO;

Then write this code in the place where you want your program to read the file

StreamReader sr = new StreamReader("text.txt");
string line;
 
while ((line = sr.ReadLine()) != null)
{
    Console.WriteLine(line);
}

With this code we read the text written in the file "text.txt" line by line, and print it on the console.
Simple as that.

Tags: ,

Using the Timer Class

May 22nd, 2008 | No Comments | Posted in C#

The timer class is often used in many applications. It's very simple for using, like all the classes implemented in .Net Framework.

So create a C# Console project, we will try this class in a console application.

Include this namespace, so we can use the Timer class

using System.Timers;

Then in the Main() function put this code

Timer tm = new Timer();
tm.Interval = 1000;
tm.Elapsed += new ElapsedEventHandler(tm_Elapsed);
tm.Enabled = true;
 
Console.WriteLine("To exit hit the \"Enter\" key.");
Console.ReadLine();

In the above code we are creating an object from the Timer class, set his interval so 1000 miliseconds, create an event which will be invoked every 1000 miliseconds and enable the timer.
Then we write some text to the console, just for information for the user how to exit from the application. And the last line stops the program to end, and waits for user input.

Below the main function create this event

static void tm_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine(DateTime.Now);
}

In the end you can exclude the unused usings, showed in this post.

Now start the application, it will write on the console the actual time every second. Press Enter to exit.

You can put diferrent code in the tm_Elapsed function, so that code will execute every second or the interval you will set.

Download the project from here.

Tags: ,

C/C++ Trigraphs and Digraphs

May 19th, 2008 | No Comments | Posted in C/C++

Trigraph is a sequence of three characters. The first two always are question marks "??".
Trigraphs are equivalent to some single characters. Here is a table from trigraphs and their equivalents

Trigraph

Equivalent

??!

|

??<

{

??>

}

??/

\

??'

^

??-

~

??=

#

??(

[

??)

]

This can cause some problems for new programmers. See this example

// This will be executed???????????/
i++;

This line of code will not be executed, because is used a trigraph "??/" the compiler will not compile the line.

Digraphs are used for the same issues as the trigraphs, see the digraphs

Digraph

Equivalent

<:

[

:>

]

<%

{

%>

}

%:

#

%:%:

##

Tags:

Adding Items to ListView Control

May 15th, 2008 | No Comments | Posted in C#

Adding items to list view is more difficult than adding items to list box. And in general the ListView control is more complex than the ListBox, and that makes it more difficult to work with.

So, in a C# project, create this form

ListView form

The controls have the default names in Visual Studio 2008.

Than click on the little button in the top right corner of the ListView

ListView properties

Than from the options of the View dropdown menu select Details. Now, click on the Edit Columns link and create two columns

You can optimize the column width.

Now, in the button1 click event write this code

string[]str={textBox1.Text, textBox2.Text};
ListViewItem lvItem = new ListViewItem(str);
listView1.Items.Add(lvItem);

We are creating a string that contains the text written in the text boxes, and it will be inserted in the list view control. Then we create a ListViewItem object and assign the values to it, and in the end we insert the ListViewItem object in the listView1 control.

Run the application and write something ni the text boxes, then click the button. The values will be entered in the list view control.

Download the project

Tags: ,

Remove Unused Using Statements

May 12th, 2008 | No Comments | Posted in C#

When we create a new C#.Net project (or other .Net project, , the editor includes the following using statements in the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

But most of this included namespaces are unused, and you don't know which of them you don't need.
So, in Visual Studio 2008 there is a new feature for excluding the unused usings from your project.
In the code editor, click the right mouse button and in the context menu go to Organize Usings -> Remove and Sort

Remove unused usings

If you have not written additional code, the editor will leave this two using statements

using System;
using System.Windows.Forms;
Tags: , ,

Adding Items to ListBox and ComboBox Controls

May 10th, 2008 | 2 Comments | Posted in C#

Adding items to this controls in .Net is very easy. To demonstrate that we will create a simple project with the following controls:

  • ListBox (name: listBox1)
  • ComboBox (name: comboBox1)
  • TextBox (name: textBox1)
  • Button (text: Add)

So, drag this controls from the toolbox and drop them on the main form one by one. Make a form like this

List box and combo box

Now double click on the button, it will create a button click event. Insert this code there

listBox1.Items.Add(textBox1.Text);
comboBox1.Items.Add(textBox1.Text);
textBox1.Clear();

Now, write something in the text box and then click the button. The text you entered in the textBox1 will be inserted as item in the listBox1 and comboBox1, and the textBox1 text will be cleared.

List box and combo box with data

Tags: ,

Usage Of RegistryKey Class

May 8th, 2008 | 2 Comments | Posted in C#

The usage of RegistryKey class is very huge in many applications in which is needed to "remember" some settings of the application, and then load them when starting the application.
Today we will use the RegistryKey class for that issue. The application will save this informations in the registry when exiting:

  • Left
  • Top
  • Height
  • Width
  • FirstStart

And on start the application will load them.

Don't forget do add this code everywhere where you are using the RegistryKey class (see more here)

using Microsoft.Win32;

So, create a C# Windows Forms Application, and make a double click on the form it will create the Form1_Load event, it is executed when application starts.

Then, in the Form1_Load event paste this code

RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true).
CreateSubKey("Registry Test");
if (key.GetValue("FirstStart") == null)
    MessageBox.Show("The application is started for the first time.\n"+
                    "I will not load the settings.", "Info");
else
{
    this.Left = (int)key.GetValue("Left");
    this.Top = (int)key.GetValue("Top");
    this.Height = (int)key.GetValue("Height");
    this.Width = (int)key.GetValue("Width");
}
key.Close();

With the above code we create an object from the RegistryKey class and create a sub key "Registry Test" in the HKEY_CURRENT_USER/Software key. Then we check if the application is started for the first time. If the "FirstStart" value does not exist then the application is started for the first time, and that means that the other values ("Left", "Top", "Height", "Width") are not saved, and when the program tries to load them it will fall.
So what we do, if the application is started for the first time, we do not load them, and popup a message to tell the user that. When loading the setting the value is casted to integer and then assigned to the appositely property. And then we close the key object.

Now go to solution explorer and open the Form1.Designer.cs file.

Form Designer class=

You will see the Dispose function there, so in that function above this code

base.Dispose(disposing);

write the following code

RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true).
CreateSubKey("Registry Test");
key.SetValue("FirstStart", 0, RegistryValueKind.DWord);
key.SetValue("Left", this.Left, RegistryValueKind.DWord);
key.SetValue("Top", this.Top, RegistryValueKind.DWord);
key.SetValue("Height", this.Height, RegistryValueKind.DWord);
key.SetValue("Width", this.Width, RegistryValueKind.DWord);
key.Close();

This piece of code is called when we close the application, it saves the current values of the properties (Left, Top, Height, Width) and to value FirstStart assigns 0.

Build the application and start it. For the first time, it will popup a message box. Then move the application somewhere on your desktop, change its size and then close it, and the values will be recorded in the registry. The next time you start the application it will be on the same place you exited and its size will be the same.

Download the project

Tags: