Monday, June 1, 2009

C++: Reading from and writing to a file

Me being the idiot that I am, I am not satisfied that I have just recently finally really gotten my random wallpaper script working with Python. Nooo, now I feel like a sell-out since I'd already written it in C++ once and unfortunately lost that somehow.
So now that it works in Python, of course I just had to decide to write it in C++ again too.
I found, though, that I am severely lacking in C++ knowledge. All the tutorials I followed almost a year ago now (I think it was at least 3) have all been next to lost from my mind.

Fortunately I had decided on completing a very simple task for today, since it was already 10pm when I started on this: Read all lines from a file and be able to write a line to a file, the rest will come later.

I started out with creating a header file that looked like this:
#include <string>

class FileRW {
public:
void add(std::string filename);
void read();
};
Declaring a class called FileRW and 2 public functions: add, read

Now for the simplest of implementations of these functions we're going to read a file with read() and add a line with write()

first we have to include
#include <iostream>
#include <fstream>
#include <string>
#include "filerw.h"
That last one is, of course, to include the header file I wrote before, which I called filerw.h

to keep it simple we'll also be
using namespace std;
otherwise we'd be writing std:: everywhere, which is just not so much fun really...

then the functions:
void PaperConfig::read() {
string line; // string where a line will be stored

// Open the file for reading
ifstream myfile("test.txt");
// If the file was opened
if (myfile.is_open()) {
// Until it reaches the end of the file
while (!myfile.eof()) {
// Get the next line
getline(myfile, line);

// If it's not an empty line, print it
if (line != "")
cout << line << endl;
}

// Close the file
myfile.close();
}
// If the file was not opened
else
cout << "Unable to open file";
}
We use ifstream (in file stream?) to open the file for reading.
We use the line variable as a temporary variable to store each line we read in so that we can write it to the console (or do something else with it). We use myfile.is_open() to check if the file could be opened, if not then it'll tell us and we use myfile.eof() to check if we have anything left to work with.
If the currently read line does not equal "" (an empty string).
When we're done we close the file.
void PaperConfig::add(string filename) {
// Open file for writing and set the append flag
ofstream myfile("test.txt", ios::app);

// Write filename to file
myfile << filename << endl;

// Close the file
myfile.close();
}
Here we use the ofstream (out file stream?) to open the file for writing and the ios::app flag to tell the stream to append whatever we write to the end of the file.
Then we write our parameter string to the myfile object like we write a lot of other things to cout and we add an endline character (endl)
Finally we close the file, this way we don't keep things in memory we don't need to.

Now I hope I have explained enough in this post, I don't often write things like this (in case you don't get it this is my version of a tutorial, but even I can't really call it that because it is writting in about 30 minutes) so if you see room for improvement please let me know. I will try to write more things like this in the future and hope to get better at writing, C++ and software development in general.

Saturday, May 23, 2009

My walk around the block

About a week ago I got fed-up with Zenwalk. I had a stint of trying to install software from source and I had some less-then-encouraging results, since many things just didn't build and some didn't build and didn't even give a reason why.



I started looking for alternatives. I tried to avoid Fedora in the beginning, but that didn't last long, since it is one of few mainstream distributions that features gnome as it's standard window manager.



I tried installing Mandriva with gnome first. Though since it got stuck in the initial phase of the installation, before I even selected a single package or anything, that effort was put a stop to quite quickly.



Then I thought about Arch Linux, since I'd already downloaded the install CD anyway, but as I started installing it I changed my mind.

I ended up installing Fedora 10 at that moment, which of course gave me grief over my video card, not allowing me to go beyond a 1024x786 resolution. Eventually I got it fixed though, but then after a few days I wanted to install MP3 and other non-free things such as win32 codecs and such, but then I rebooted my computer and it wouldn't start anymore. So I gave up on fedora too.



I then tried Gentoo, I have been working with Linux for a little over a year now and I thought it might be interesting to build it from near-scratch, and it was. But after going through the entire installation process with documentation and failing to get Xorg to work, I stepped off that train and moved on again.



I got to Arch Linux again, this time I actually went through the trouble of installing it and running it. Again though, the Xorg setup is what killed it, since it's documentation was not up to par with Gentoo.



I also tried g:Noblin, but since it didn't recognise my very generic 3-button mouse, it got thrown in the trash bin too...



Finally I got back to Zenwalk again. I installed it, it feels very comfortable to be back. This time I installed the gnome version, only to find out that it has just been cut-off from support since the guy maintaining the gnome parts had a fight with another guy and got kicked off. I hope he comes back or someone takes his place because I just can't deal with xfce.

During install I did have a few fatal errors installing certain packages, but fortunately I know enough to seemingly fix most of them.



The grass always seems greener on the other side, but when you actually go there you find out that it wasn't so bad where you were.

If I get fed up with Zenwalk again, then my only other option will become Ubuntu most likely, and even though it's very user friendly and all, but I feel it is too easy, I don't feel I will learn much from it. Besides, having it on my work laptop is enough for me, I'd prefer to use something else on my home computer.

Wednesday, March 4, 2009

Zenwalk: Logitech Quickcam Pro for Notebooks

After days of searching and dead end results I have finally been able to install my Logitech Quickcam Pro for Notebooks in Zenwalk.
It is surprising what kind of (old) junk you have to go through (like qc-usb, pwc and gspca) to find out that there is actually a universal driver that is integrated in most modern linux kernels.

So, today I finally found out that UVC is the way to go for my 0x46d:0x991 webcam. I checked compatibilities here.
If you're not quite sure which camera exactly you have you can always check by opening a terminal and typing:
/sbin/lsusb
Which will result in all devices that are hooked up to your USB hubs to be listed. This would be my webcam, and yours most likely would be similar:
Bus 005 Device 002: ID 046d:0991 Logitech, Inc. QuickCam Pro for Notebooks
if you take a look at this line you will see the name and the ID of your webcam.



All I had to do was download the uvc drivers from here, and then type:
tar -xzvf uvcvideo-5ba3bf58b52d.tar.gz
cd uvcvideo-5ba3bf58b52d
make
su
[password]
make install
modprobe uvcvideo
after which I saw that I finally had a /dev/video0 file, meaning succes!



Skype did not yet respond to it though, so I edited my /etc/rc.d/rc.modules file to modprobe uvcvideo every time zenwalk starts and I rebooted, and it seems to work fine now, Skype finds it and shows an image when I press test which even moves.



You might need to install the kernel source packages for this to work, you can find these with netpkg, called kernelsource. I've tried 3 other drivers before finding this one so it got a bit messy in the end.

Tuesday, February 24, 2009

Zenwalk: Intel 82801G NIC

I have recently made a switch from Fedora to Zenwalk 5.2 Linux and today I got my internet connection (YAY).



Turned on my computer and to my surprise Zenwalk didn't recognize my ethernet card (an Intel Corporation 82801G (ICH7 Family) LAN Controller, as I found out by starting an old Live Fedora cd I still had lying around, though later I found out I could have used the command 'lspci -v' aswell).



I looked around a little and found out I had to start the mii and e100 modules in order to get it working, which worked, but then I still needed them to be loaded at startup.
This time around for as much and as long as I can I want to do thing right, I don't want this linux ending up the same way that my Ubuntu has and my Fedora had where I didn't know what was installed where or how.



Looking around a little I found out that you should edit the file /etc/rc.d/rc.modules to include these 2 modules at startup.



So, if you're having this same trouble that I'm having, all you need to do is edit the /etc/rc.d/rc.modules file:
su
[enter password]
vi /etc/rc.d/rc.modules

(I use vi but you can of course use any editor you like)

and then add or uncomment the lines:
/sbin/modprobe e100
/sbin/modprobe mii

And that should get it working again for you.

Tuesday, January 20, 2009

Telerik Controls in Web Custom Controls

I work at a .Net Application Development company and the other day I didn't have anything to do, so I decided to change one of my User Controls into a Web Custom Control (ASP.Net Server Control) because I was told that we might want to use it in some of the other projects we have or are going to start in the near future.



I had never made a server control before and from what I had seen here and there I thought that this might be somewhat of a challenge. Thankfully it was easier then I thought it would be and I was successful fairly quickly.



Then one of my co-workers came up with the brilliant idea to make it's 'parent' into a server control, so I started work on that.

This 'parent' was a collection of a Fieldset with a legend, 2 radiobuttons, 2 instances of my new server control and 2 rad controls.



When I started building this control I did it all (as I was taught) in the RenderContents(HtmlTextWriter output) function.

The problem was that when it got to the point where the Telerik controls were being Rendered, they liked to throw some ReferenceNullExceptions my way.



After some looking around though I found an article in the Telerik Knowledge Base that said that I should add the Telerik controls in the CreateChildControls() function, so I did, and it works.

Check out the article here