Click here to Skip to main content
15,896,063 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: java Pin
Mohibur Rashid8-Apr-12 17:49
professionalMohibur Rashid8-Apr-12 17:49 
AnswerRe: java Pin
Malli_S8-Apr-12 19:51
Malli_S8-Apr-12 19:51 
AnswerRe: java Pin
Chuck O'Toole9-Apr-12 8:06
Chuck O'Toole9-Apr-12 8:06 
QuestionLinked List Pin
djgmad8-Apr-12 3:37
djgmad8-Apr-12 3:37 
AnswerRe: Linked List Pin
Randor 8-Apr-12 4:32
professional Randor 8-Apr-12 4:32 
GeneralRe: Linked List Pin
djgmad8-Apr-12 5:43
djgmad8-Apr-12 5:43 
GeneralRe: Linked List Pin
Randor 8-Apr-12 6:52
professional Randor 8-Apr-12 6:52 
GeneralRe: Linked List Pin
enhzflep8-Apr-12 7:24
enhzflep8-Apr-12 7:24 
You may well be lucky that I can't sleep tonight. Dead | X|

I'm too buggered to provide an overview or description. My efforts in crafting the code carefully are likely to be similarly short-changed..

You should use the debugger to step through the code and a hex-editor to review the created data-file. You'll notice I use (modified) pascal-style strings in the data file since it simplifies loading.


C++
#include <stdio.h>
#include <string.h>

typedef struct node
{
    char *text;
    int num;
    node *next;
};

void addNode(node* &listHead,char *newText, int newInt)
{
    node *curPos;

    curPos = listHead;
    if (curPos == NULL)
    {
        listHead = new node;
        curPos = listHead;
    }

    else
    {
        while (curPos->next != NULL)
            curPos = curPos->next;
        curPos->next = new node;
        curPos = curPos->next;
    }

    curPos->next = NULL;
    curPos->num = newInt;
    curPos->text = strdup(newText);
}

void dispList(node *firstNode)
{
    node *curNode;
    curNode = firstNode;

    while (curNode != NULL)
    {
        printf("Text: %s\n", curNode->text);
        printf("Num: %d\n", curNode->num);
        curNode = curNode->next;
        if (curNode)
            printf("\n");
    }
}

void saveListToFile(node *firstNode, char *szFilename)
{
    node *curNode;
    FILE *fp;
    char asciiNull = 0;
    int sLen;

    curNode = firstNode;
    fp = fopen(szFilename, "wb");

    while (curNode != NULL)
    {
        sLen = strlen(curNode->text);

        // write length of string (house keeping)
        fwrite(&sLen, sizeof(int), 1, fp);
        // write string (node member)
        fwrite(curNode->text, 1, sLen, fp);
        // write number (node member)
        fwrite(&curNode->num, sizeof(curNode->num), 1, fp);
        curNode = curNode->next;
    }
    fclose(fp);
}

node *loadListFromFile(char *szFilename)
{
    FILE *fp;
    node *result = NULL;
    char strBuffer[32];
    int intBuffer, sLen;
    char tmpChar;

    fp = fopen(szFilename, "rb");
    printf("filePos: %d\n", ftell(fp));

    fread(&sLen, sizeof(int), 1, fp);
    strBuffer[sLen] = 0;
    fread(strBuffer, 1, sLen, fp);
    fread(&intBuffer, sizeof(int), 1, fp);
    addNode(result, strBuffer, intBuffer);

    while (!feof(fp))
    {
        fread(&sLen, sizeof(int), 1, fp);
        strBuffer[sLen] = 0;
        fread(strBuffer, 1, sLen, fp);
        fread(&intBuffer, sizeof(int), 1, fp);
        addNode(result, strBuffer, intBuffer);
    }

    fclose(fp);

    return result;
}

int main()
{
    char buffer[32];
    int i;
    node *listHead, *loadedList;
    listHead = NULL;

    for (i=0; i<10; i++)
    {
        sprintf(buffer, "item_%d", i+1);
        addNode(listHead, buffer, i+1);
    }
    saveListToFile(listHead, "list.dat");
    loadedList = loadListFromFile("list.dat");

    printf("Created list:\n");
    dispList(listHead);

    printf("Loaded list:\n");
    dispList(loadedList);

    return 0;
}

AnswerRe: Linked List Pin
Erudite_Eric8-Apr-12 7:01
Erudite_Eric8-Apr-12 7:01 
SuggestionRe: Linked List Pin
David Crow8-Apr-12 16:33
David Crow8-Apr-12 16:33 
Questionc Pin
siddharth00077-Apr-12 17:23
siddharth00077-Apr-12 17:23 
AnswerRe: c Pin
Rajesh R Subramanian7-Apr-12 22:43
professionalRajesh R Subramanian7-Apr-12 22:43 
AnswerRe: c Pin
Luc Pattyn8-Apr-12 5:19
sitebuilderLuc Pattyn8-Apr-12 5:19 
AnswerRe: c Pin
Chris Losinger8-Apr-12 6:08
professionalChris Losinger8-Apr-12 6:08 
AnswerRe: c Pin
Wes Aday8-Apr-12 6:18
professionalWes Aday8-Apr-12 6:18 
AnswerRe: c Pin
enhzflep8-Apr-12 7:11
enhzflep8-Apr-12 7:11 
AnswerRe: c Pin
Stephen Hewitt8-Apr-12 21:34
Stephen Hewitt8-Apr-12 21:34 
AnswerRe: c Pin
scramjetter14-Apr-12 15:57
scramjetter14-Apr-12 15:57 
QuestionI want to write a code for dns redirect Pin
cosharp7-Apr-12 14:18
cosharp7-Apr-12 14:18 
QuestionRe: I want to write a code for dns redirect Pin
Randor 8-Apr-12 4:39
professional Randor 8-Apr-12 4:39 
QuestionPointers - help please Pin
jenbren7-Apr-12 12:24
jenbren7-Apr-12 12:24 
AnswerRe: Pointers - help please Pin
Maximilien7-Apr-12 13:41
Maximilien7-Apr-12 13:41 
GeneralRe: Pointers - help please Pin
jenbren7-Apr-12 15:05
jenbren7-Apr-12 15:05 
Question#define question Pin
_Flaviu7-Apr-12 0:36
_Flaviu7-Apr-12 0:36 
AnswerRe: #define question Pin
Richard MacCutchan7-Apr-12 1:36
mveRichard MacCutchan7-Apr-12 1:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.