Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-26-2009, 08:39 PM   PM User | #1
spadez
Regular Coder

 
Join Date: Oct 2006
Posts: 197
Thanks: 9
Thanked 0 Times in 0 Posts
spadez is an unknown quantity at this point
Intergrating my console program into a windows application

Hi,

This is my code for my CSV file reader:

Code:
#include <stdio.h>
#include <stdlib.h>

#define NAME 1
#define DATE 2

#define NOQUOTE            '\0'
#define SINGLEQUOTE '\''
#define DOUBLEQUOTE '\"'

typedef struct record_t {
    char name[6];
    char date[10];
    struct record_t * next;
} record;

record * newRecord() {
    record * temp = (record *)malloc(sizeof(record));
    temp->next == NULL;
    // all other fields should be set to NULL;
    return temp;
}

void deleteList(record * a) {
    record * b;    
    while (a->next != NULL) {
        b = a->next;
        free(a);
        a = b;
    }
}

void append(char * string, char character) {
    while(* string != '\0') {
        string++;
    }
    * string = character;
    string++;
    * string = '\0';
}

int main(int argc, char ** argv){
    if(argc < 2){
        printf("Usage:\n\t%s filename\n", argv[0]);
        return -1;
    }
    
    FILE * fp;
    if((fp = fopen(argv[1],"r")) == NULL){
        printf("Can't open %s\n",argv[1]);
        return -2;
    }
    
    record * root;
    record * current;
    root = newRecord();
    current = root;
    
    char quote = NOQUOTE;
    int field = 1;
    int c;
    while((c = fgetc(fp)) != EOF ) {
        if(c == '\n' ) {
            field = 1;
            quote == NOQUOTE;
            current->next = newRecord();
            current = current->next;
        }
        else if(c == '\"') {
            if(quote == NOQUOTE) {
                quote = DOUBLEQUOTE;
            }
            else if(quote == DOUBLEQUOTE) {
                quote = NOQUOTE;
            }
        }
        else if(c == '\'') {
            if(quote == NOQUOTE) {
                quote = SINGLEQUOTE;
            }
            else if(quote == SINGLEQUOTE) {
                quote = NOQUOTE;
            }
        }
        else if(c == ',' && quote == NOQUOTE) {
                field++;
        }
        else switch(field) {
            case NAME:
                append(current->name, c);
                break;
            case DATE:
                append(current->date, c);
                break;
        }    

    }
    fclose(fp);

    current = root;
    int length = 0;
    do { 
        printf("%s, %s\n", current->name, current->date);
        length++;
        current = current->next;
    } while(current->next->next != NULL);
    
    free(current->next);
    current->next = NULL;

    deleteList(root);
    return 0;
}
And this is my code for the window:

Code:
#include <windows.h>

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}
Since this is my first C program I have no idea how to intergrate the two so that my CSV contents are displayed in the window instead of the console.

Can anyone give me some pointers please? Would I literally be able to post my console code into a section of the window code, or does it have to be modified in order to work?
spadez is offline   Reply With Quote
Old 02-27-2009, 12:39 AM   PM User | #2
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
You'll have to modify your WndProc so that during the WM_PAINT event, you write any text you want displayed to the window. How you want your GUI to look is really up to you.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 02-27-2009, 05:29 AM   PM User | #3
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
maybe this will not help you but I would prefere another approach since c/c++ part of a gui is very complicated in my opinion.
1. use tcl/tk to build the gui
2. use swig to bind your c/c++ part with tcl/tk

this approach is portable, I use it to develop app on linux which work without problem on windows( and other os's) and is development process is fast and simple(few lines of code and complicated gui stuff is hidden by tcl/tk).
what is wrong with this is that you must learn to use tcl/tk but this is not so hard in my opinion.

http://www.activestate.com/activetcl/tcl8_6/
http://www.swig.org/tutorial.html

of course if you feel more confortable with python you can use it instead of tcl/tk in same way.

best regards
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:42 PM.


Advertisement
Log in to turn off these ads.