Write in C

The last couple of months has been all about Calculus, Agorithms and Datastructures, Data modelling with Object Role Modelling and SQL.

After finally getting familiar with the basics of PostgreSQL I found myself curious about using the MySQL databases supplied by my web hosting service, and today I got around to making a program to insert .csv files (with whitespace as delimiter) into my own tables.

As an added challenge I thought I’d use C rather than Java, which means I had to use sscanf instead of split to collect individual integers from an input file separated by whitespace.

This bit of code reads an input file, calculates the size of the file, the number of rows and columns and then proceeds to read the entire file. It then uses sscanf to collect each integer now that we know the number of rows and columns and then creates an sql script based on that information.

There’s probably an easier way of doing this, but in my experience, always looking for shortcuts takes time, so I might as well just use what I know already to get the job done:

 


#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define pline printf("\n")

void tsize(char filename[], int ans[]);

int main(int argc, char *argv[]) {
if(argc < 4) {
   printf("too few arguments");
   return -1;
}
char *typename = argv[3];
int tablesize[4];
bzero(tablesize, sizeof(int) * 4);
tsize(argv[1], tablesize);
int row, col, i, q, inp, siz, pos, totalpos;
row = col = i = q = inp = siz = pos = totalpos = 0;
col = tablesize[0];
row = tablesize[1];
siz = tablesize[2];
int table[row][col];
FILE *in = fopen(argv[1], "r");
char *text = malloc(sizeof(char) * siz);
bzero(text, sizeof(char) * siz);
while((inp = fgetc(in)) != EOF) {
   text[i++] = inp;
}
fclose(in);
for(i = 0; i < row; i++) {
   for(q = 0; q < col; q++) {
      int val = 0;
      sscanf(text + totalpos, "%d%n", &val, &pos);
      totalpos += pos;
      table[i][q] = val;
}
}
FILE *out = fopen(argv[2], "w");
fprintf(out, "CREATE TABLE rammepris (\n");
fprintf(out, "meterpris INT NOT NULL,\n");
fprintf(out, "halvmaal INT NOT NULL,\n");
fprintf(out, "utpris INT NOT NULL,\n");
fprintf(out, "rammetype VARCHAR NOT NULL UNIQUE,\n");
fprintf(out, "PRIMARY KEY(meterpris, halvmaal)\n");
fprintf(out, ");\n");
for(i = 1; i < row; i++) {
   for(q = 1; q < col; q++) {
      int meterpris = table[i][0];
      int halvmaal = table[0][q];
      fprintf(out, "INSERT INTO rammepris(meterpris, halvmaal, utpris, rammetype) \   
      values(%d, %d, %d, %s);\n", meterpris, halvmaal, table[i][q], typename);
   }
}
fclose(out);
}

void tsize(char filename[], int ans[]) {
FILE *in = fopen(filename, "r");
int col, inp, siz;
col = inp = siz = 0;
while((inp = fgetc(in)) != EOF) {
   ans[2]++;
   if(inp == ' ' || inp == '\n')
      col++;
   if(inp == '\n') {
      ans[0] = max(col, ans[0]);
      col = 0;
      (ans[1])++;
   }
}
fclose(in);
}

Read More …

Piping to stdout and file simultaneously

Just another note to self here:

./[program] [arguments] | tee [filename.txt]

Run program with arguments, use pipe “|” and the program named “tee” to output do both stdout and a particular file. Great for logging terminal programs.

Flags:

  • -a Appends the output to the end of File instead of writing over it.
  • -i Ignores interrupts.

Since it’s a GNU-core utility, “man tee” gives a manual of the program.

 

Ljusterö

I just recently spent a few days at the island of Ljusterö just outside of Stockholm.

Since I’m still waiting for my DSLR-charger I chose not to bring it, and as luck would have it, I had to take these shots using my cell phone camera.

Still I find myself pleased with the results, so here goes…

Sudoku

Finished the Sudoku-puzzle solver about two weeks ago, I might publish part of the solution later on when the course is finished, but at the moment here’s a few snapshots of how it looked like.

Bubbles

It’s been over a year. So long, even though I’ve been working on this page several times.

So what’s new?

I’ve started studying computer science at the University of Oslo, and spent most of the year learning about Java.

Only recently did I read up on applets in a book on game programming and found it so much easier than the troublesome JSF stuff I was reading up on, but I guess it’s probably like that for a reason, as I noticed applets are a real pain on the user side.

I’ve also registered for VPS to get a platform for programming experiments

As I haven’t had a chance to secure my server with SSL yet it’s probably “impossible” to run for most people (took me at least five minutes to get my Macbook to accept the applet).

After that I thought I’d try and play with it in photoshop. to my surprise a simple filter managed to turn the letters into bubbles:

Hopefully, later on I’ll be making some more interesting applications or applets. Looking forward to the next challenge at uni where we’re supposed to make an automatic Sudoku-puzzle solver.

Of sound mind …

Yes yes, not much happening as usual. It’s Sunday in August and just browsing my hard drive for old music projects and noticed I haven’t shared much in a while, so why not upload a short little theme I wrote for the piano a while ago.

What’s up?

“Twenty-five years I’m alive here still
Trying to get up that great big hill of hope
For a destination”

 

OK then, I admit it, I’ve had a bit of a 90’s alternative rock phase on Spotify lately.

 

And what else? Well, I just heard I’ll be writing my master’s degree in the oldest university of Scandinavia (Uppsala Universitet) which is fun seeing as I’m a history student… of sorts.

 

And I also wrote this little track in FruityLoops – nothing like a little modular synthesis and digital automation:

Pen and ink

I guess it’s been a little while since I last did anything to update my page here, and in part I think it’s because I’ve spent a lot of time working on applications and portfolios for higher education.

Now that that’s finally finished I just found the time to upload a few samples of what I’ve been doing lately under my “Pen and Ink” page.