Wednesday, 25 November 2009

Input/Output with files

The following is a simple program to show reading, writing and appending to files. You can read more about this feature at cplusplus.com.




//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include <iostream>
#include <fstream>
#include <string>

using namespace
std;

int
main()
{

string line;
cout<<"\nFile 1 operations - Writing to Hello.txt"<<endl;
ofstream file1; //OUTPUT file Stream
file1.open("Hello.txt"); //Open the file
file1 << "Writing something something to a file"<<endl;
file1 << "This is enough for the time being"<<endl;

if
(file1.is_open()) //Check if open
{
/*
while (!file1.eof())
{
getline (file1,line); - NOT POSSIBLE because of ofstream
cout << line << endl;
}
*/

}

file1.close(); //Close file1

cout<<"\nFile 2 operations - Reading from Hello.txt"<<endl;
ifstream file2; //INPUT file stream
file2.open("Hello.txt"); //Open the file
if(file2.is_open()) //Check if open
{
while
(!file2.eof())
{

getline (file2,line);
cout << line << endl;
}
}

file2.close(); //Close file2


cout<<"\nFile 3 operations - Appending and Reading from Hello.txt"<<endl;
fstream file3; //INPUT file stream
file3.open("Hello.txt", ios::in | ios::out); //Open the file
if(file3.is_open()) //Check if open
{
//We want to put a new line at the end of the file
file3.seekp(0, ios::end);
file3 << "This is new line added file 3"<<endl;
//Now reset the file pointer to the start of the file
file3.seekg(0, ios::beg);
while
(!file3.eof())
{

getline (file3,line);
cout << line << endl;
}
}

file3.close(); //Close file3

return
0;
}








The output is as follows:

Monday, 16 November 2009

C String and C++ Strings: Similarities and Differences

I came across this scenario recently when a C style string (as some people refer to char[] as) had to be compared to a C++ string and even though this is straightforward, i fell in the same trap as a lot of people about forgetting that there is a Nul charachter as the end of C style string. Anyway, here is an example to demonstrate both the strings.




//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Program to demonstrate length and size of char* and string
//and to show similarity and difference between them
#include<iostream>

using namespace
std;

int
main()
{

cout<<endl;
char
a[10]="Hello";
cout<<"Size of a = "<<sizeof(a)<<endl;
a[5]='\0'; //Terminating the C string in between with Nul character
cout<<"Size of a = "<<sizeof(a)<<endl;

cout<<endl;
char
b[]="Hello";
cout<<"Size of b = "<<sizeof(b)<<endl; //Note the size
//Automatic Nul charachter added

cout<<endl;
string c("Hello");
cout<<"Size of c = "<<sizeof(c)<<endl;
cout<<"Length of c = "<<c.length()<<endl;

c += " Zahid";
cout<<"Size of modified c = "<<sizeof(c)<<endl;
cout<<"Length of modified c = "<<c.length()<<endl;

//Add an extra NULL charachter
cout<<endl;
char
d[]="Hello\0";
cout<<"Size of d = "<<sizeof(d)<<endl;
for
(int i = 0; i < sizeof(d); i++)
{

cout<<"d["<<i<<"] = "<<d[i]<<endl;
}


//When is a char[] similar to string?
cout<<endl;
char
e[]="Zahid";
string f = "Zahid\0";
const
char* temp = f.c_str();
if
(strcmp(e , temp) == 0)
{

cout<<"e == f"<<endl;
}

else

{

cout<<"e != f"<<endl;
}


return
0;
}







The output is as follows:

Tuesday, 3 November 2009

Creating specialised strings using 'find_first_not_of'

Sometimes we want to create specialised strings that have a particular characteristic. For example a BitString that can only contain '0' and '1'. Or we may want to create a Hex String that can contain '0-9' and 'a-f'. We can also define other specialised types like numeric strings, octal strings, etc. The main thing would be to check to make sure that the string only contains valid characters. We can do this basic check using find_first_not_of function.

Here is a simple program to show its use:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Program to test specialised strings

#include <iostream>
#include <string>

using namespace
std;

bool
checkIfBitString(string testString)
{

return
(testString.find_first_not_of("01?") == std::string::npos);
}


bool
checkIfHexString(string testString)
{

return
(testString.find_first_not_of("0123456789aAbBcCdDeEfF?") == std::string::npos);
}


int
main()
{

cout<<endl;
string str1("010011000111010101");

if
(checkIfBitString(str1))
cout<<"str1 is a BitString"<<endl;
else

cout<<"str1 is not a BitString"<<endl;

if
(checkIfHexString(str1))
cout<<"str1 can also be a HexString"<<endl;
else

cout<<"str1 is not a HexString"<<endl;

cout<<endl;
string str2("010aBcDeF345678010101");

if
(checkIfBitString(str2))
cout<<"str2 is a BitString"<<endl;
else

cout<<"str2 is not a BitString"<<endl;

if
(checkIfHexString(str2))
cout<<"str2 is a HexString"<<endl;
else

cout<<"str2 is not a HexString"<<endl;

cout<<endl;
string str3("Banana");

if
(checkIfBitString(str3))
cout<<"str3 is a BitString"<<endl;
else

cout<<"str3 is not a BitString"<<endl;

if
(checkIfHexString(str3))
cout<<"str3 is a HexString"<<endl;
else

cout<<"str3 is not a HexString"<<endl;

return
0;
}






The output is as follows: