Sunday, December 6, 2009

My copy command

This can only handle a line of 1500 characters...

#include
#include
#define LINESIZE 1500+1

int main(int argc, char *argv[]) {
FILE *src;
FILE *dest;
src = fopen (argv[1], "r");
dest = fopen (argv[2], "w");
char sf[LINESIZE];

if (src) {
while (1 == fscanf(src, "%[^\n]\n", sf)) {
sf[strlen(sf)+1] = '\0';
fprintf(dest, "%s\n", sf);
}
fclose(src);
}
else {
printf("Cannot open source file.\n");
}
fclose(dest);
return 0;
}

Printing a file backwards

int main(){
Student S;
fstream fback("std.bin",ios::in| ios::binary);
fback.seekg(-(sizeof(Student)), ios::end);
while (!fback.read((char *)&S, sizeof(Student)).fail()) {
cout << S << endl;
fback.seekg(-2 * (sizeof(Student)), ios::cur);
}
return 0;
}

Sunday, November 29, 2009

++, --: yet another prove why they should be avoided

I was doing a walk through from one the old tests:

x = -2.1;

after the line...

x = -x++;

3 compilers assigned 3 different values to x:

VC++: 3.1 - incremented, negated (not necessarily in this order), assigned
linux: -1.1 - incremented, assigned (why never negated?)
borland: 2.1 - negated, assigned (never incremented)

Monday, November 23, 2009

io_checklist with comments

I added some comments to setItems() in io_checklist:

void IO_CheckList::setItems(const char *str){
int i;
unsigned int maxlen = 0;
int strLen= strlen((char*)str);
_itemstr = new char[strLen+1];
strcpy(_itemstr, (char*)str); // copy str into _itemstr
for(_len = 0,i=0;i if(_itemstr[i] == '\n'){
_len++; // number of items separated by '\n'
}
}
_items = new char*[_len+1]; //_items is pointer to an array of pointer
_status = new bool[_len+1];
for(i=0;i<_len+1;i++){
_status[i] = false;
}

/* Following lines of code:
_itemstr contains a string of items in the checklist, each one separated by '\n';
_items is made to point to the beginning of the string before the for loop;
Through the for loop, _itemstr is cycled through character by character.
Each time a '\n' is reached, _item is made to point to the character after the '\n'.
The '\n' is replaced with NULL and thus truncating the rest of the string so _item pointer in the array of pointers points to only 1 item in the checklist. */

_items[0] = _itemstr; // point to where _itemstr is pointing to
for(_len = 1,i=0;i if(_itemstr[i] == '\n'){
_items[_len]=&_itemstr[i+1]; // when '\n' is reached, _item points to first character after '\n'
_itemstr[i] = 0; // replace '\n' with NULL
if(strlen(_items[_len-1])> maxlen){
maxlen = strlen(_items[_len-1]); // keep track of the longest string copied
}
_len++; // keep track of number of items
}
}
if(strlen(_items[_len-1])> maxlen){
maxlen = strlen(_items[_len-1]);
}
IO_Frame::set(IO_Field::getRow(), IO_Field::getCol(), maxlen + 6, _len+2);
_titlelen = maxlen;
}

Tuesday, November 17, 2009

Assignment 2 progress

Just committed io_field (probably the easiest class for this assignment =P)- it's now done according to spec on wiki. The only kinks I ran into was trying to get the value of

_owner->getTop()
_owner->getLeft()

Since getTop() and getLeft() are functions of io_frame, _owner is an io_form pointer but inherits from io_frame I had to make sure that the inheritance is declared.

My next step is to look through io_checklist because Fardad said that there may be some extra functions that need to be added to io_field.

Thursday, November 5, 2009

Delayed blog - first 2 IRC meetings

This is a little delayed...not that good with words. =) Better late than never!
We had 2 irc meetings with Fardad during study break. We learnt what it means and how to update and commit on SVN.
oh and one important rule NEVER COMMIT SOMETHING THAT'S NOT BUILDABLE.
At this point we are in the process of committing our assigned classes(menubar and field for me).

My command line SVN doesn't work (not that it matters that much coz GUI works just fine). I checked PATH in my environmental variables and it's included. Anyone else having the same problem?

On a lighter note...Does anyone else find the SVN tortoise adorable?? I think I'll make a T-shirt out of it one day.

Sunday, October 11, 2009

OOP344 Assignment progress 2

With Jonathan's help I was able to pass all tests on VCC and LINUX (Thank you Jonathan!)
But now I'm having trouble compiling in borland.
I compiled with the command
bcc32 main.c ciol.c (I hope this is the right command)
But I got a whole bunch of "no prototype" warnings:
call to function "io_init" with no protype in function main
or
call to function "test 1" with no protype in function main

I've included the .h file in both main and ciol.c
Anyone has any idea what's going on?

Saturday, October 10, 2009

OOP344 Assignment progress

My program has successfully passed all tests except for 2:

Test 9.14:
Complaint about curpos. It says that curpos should be at 20, mine returned 19. But fieldlen of 20 is passed to io_edit. Shouldn't curpos be 1 less than fieldlen?
Anyone else having this problem?

Test 9.23:
Complaint about curpos and offset. It seems that curpos and offset should not be moved in this function. But IsTextEditor is set to false we should be able to move curpos and offset as long as the string returned is the orginal one and not the modified one.
Any thoughts on that?

Sunday, September 27, 2009

oops posted the wrong io_display!

Posted the old one I was playing around with...the current one is this:

for (io_move(row, col), len<=0 ? len=strlen(str) : len; len > 0; len--, *str++) {
*str ? io_putch(*str) : io_putch(' ');
}

Still missing one space though...

io_display in one line

This does not fully work.
It's a success against cases where strlen(str) > len and len <= 0.
However when strlen(str) < len it fills up the remaining field with all but one space (eg. if str is "abc" and len is 6, it would only print "abc" plus 2 spaces). I've given up on it, at least for tonight.

for (io_move(row, col), len<=0 ? len=strlen(str) : len; len > 0; len--, *str++) {
len>0 && *str=='\0' ? io_putch(' ') : io_putch(*str);
}

Saturday, September 19, 2009

My solution to OOP344 Challenge#1

Here's my solution, I have a feeling that it's a little more complicated than it needs to be but it works...If anyone can help me make it simpler I'd appreciate it. =)

void GetInt(char *strint, int val){
/* sprintf(strint,"%d", val); */
int i, valcpy = val, n = 1, j;
for (i=0; valcpy>0; i++) {
valcpy /= 10;
n *= 10;
}
n /= 10;
for (i,j=0; i>0 ; i--,j++) {
valcpy = val / n;
strint[j] = valcpy + 48;
val -= valcpy * n;
n /= 10;
}
strint[j] = '\0';
}