Intergrating console program into windowed app

This is a discussion on Intergrating console program into windowed app within the C Programming forums, part of the General Programming Boards category; Hi, This is my code for my CSV file reader: Code: #include <stdio.h> #include <stdlib.h> #define NAME 1 #define DATE ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    Intergrating console program into windowed app

    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?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,182
    this is my first C program
    Don't even think about GUI then.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Sorry I was unclear, the CSV is file in C is the first project ive been researching in C, im now looking to branch out to to intergrate it into a gui

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by spadez View Post
    Sorry I was unclear, the CSV is file in C is the first project ive been researching in C, im now looking to branch out to to intergrate it into a gui
    GUI programming is HARD compared to writing console applications. This is the point of Cyberfish's comment.

    As to "how you merge the two sources" is not entirely straight forward. You would have to throw up a file-chooser dialog (OpenFileDialog), and then use the filename from that to do your "csv read" functionality. However, before you do that, I would start by separating your "read csv file" portion of code from main() in your console app, into it's own function that takes a filename and returns a list of records.

    If you want to be a little bit clever, you should actually make the CSV reading into a separate function that you can use from EITHER a GUI or Console application.

    Then I would make a static set of records (start with 2-3 records with constant content) for your Windows app, and make it DRAW those records in the way you want to.

    Once you have the drawing working, work on getting the file opened and to display the content of a file, using the function that reads a CSV from a file.

    This will certainly take several hours if not days to get working, and you will almost certainly have to ask questions about how you do things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Ok, first things first. I have my code that will create a basic window. Im not entirely sure what you meant about the seperate function.

    Im assuming it isnt as simple as simply pasting the console code within the window code somewhere and have it work in that way. Im not interested in the apprearance yet, I just want the text contents to be displayed within a window not console.

    Forgetting the file browse to fetch the file for a moment, how much of the rest of the CSV code would have to be modified to work within a windowed application?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Console program to Win32
    By Ducky in forum Windows Programming
    Replies: 3
    Last Post: 02-25-2008, 11:46 AM
  3. How to avoid console program crash when terminated too early
    By Xargo in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-03-2007, 04:43 PM
  4. Get the PID of a console program
    By BianConiglio in forum Windows Programming
    Replies: 6
    Last Post: 05-24-2004, 05:24 AM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21