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?