|
|
Is there a specific way of writing/reading a linked list to and from a random access file in c++....if so could someone please give me an example
|
|
|
|
|
djgmad wrote: Is there a specific way of writing/reading a linked list to and from a random access file in c++....if so could someone please give me an example
In computer science there isn't a standard way of doing much of anything. There is always somebody out there thinking outside the box and doing it completely different.
What you are looking for is called: serialization[^]. For a linked list... just keep in mind that you cannot dump pointers into a file and expect the memory addresses to be valid when you read them back. You could either dump the raw data into a file from memory... or perhaps use a serialization format[^]. There are alot of techniques and different opinions... so you would be well served by using your favorite search engine.
Best Wishes,
-David Delaune
|
|
|
|
|
Hey thanks Mr. David Delaune for your assistance , I've spent the last half an hour or so googling but still hasn't find an example to help...I'm really new to the whole OOP world and C++. Hope I'm not asking for much but is it possible for you to provide some examples please......thanks in advance.
|
|
|
|
|
djgmad wrote: Hope I'm not asking for much but is it possible for you to provide some examples please......thanks in advance.
Sorry I am probably not going to write any code because I don't know what the data in your linked list looks like. It is more work than I am willing to invest to create a serialization class that works for all data/object types. Anyway is the data you want to serialize dynamic in size or static? Does the data contain pointers to other objects in memory? Could you show me what the data looks like?
Best Wishes,
-David Delaune
|
|
|
|
|
You may well be lucky that I can't sleep tonight.
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.
#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);
fwrite(&sLen, sizeof(int), 1, fp);
fwrite(curNode->text, 1, sLen, fp);
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;
}
|
|
|
|
|
No.
You have already posted this here: http://www.codeproject.com/Messages/4214402/read-writing-Linked-List-to-a-random-access-file-i.aspx
Have a think about the problem and see what you come up with. Your problem is not the code, it is the concept. Come back when you have a clearly defined problem to solve.
==============================
Nothing to say.
|
|
|
|
|
djgmad wrote: Is there a specific way of writing/reading a linked list to and from a random access file in c++....
This is one large problem that should be broken down into four smaller ones. Start by creating code to manipulate a linked list. The items to be added can be hardcoded rather than come from a file. Dump the list to the screen rather than to a file. Once you have these two steps done, then start fiddling around with the file.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
|
|
great subject line; was there a question?
|
|
|
|
|
|
Insert link to google here
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
siddharth0007 wrote: c
link list implementation
Here, let me fix the punctuation/grammar.
siddharth0007 wrote: See! (stands and points)
Link list implementation.
Gee it irks me when people forget the link/attachment...
|
|
|
|
|
|
|
I want to block some domain names in dns server.
Server os is linux 2.6.
Anybody give me some hint ?
Thanks.
|
|
|
|
|
Hi,
Your thread title says block but the thread body says redirect. Which is it?
Do you need to do this with code? If not you could redirect/block with the /etc/hosts file on the gateway DNS server.
For example to block the code project from /etc/hosts just add:
127.0.0.1 www.codeproject.com
Most properly configured DNS servers will check the hosts file before performing the query.
Best Wishes,
-David Delaune
|
|
|
|
|
Hello World
I was wondering if someone could explain STEP BY STEP how pointers work. I know what they are but I'm having trouble conceptualizing different situations. For example, What does each of these do?
1. pointer1 = pointer2->next;
2. pointer4 = pointer1;
3. pointer4-> data = pointer1->data;
4. pointer4->next->data = pointer1->data
I dont really understand what "->" does or what it means. I also don't quite understand the concept of "next";. I would be so grateful if someone could break it down to the basics for me because I'm trying to move ahead in my data structures class but really can't wrap my brain around this.
Thanks in advance,
Jennifer
|
|
|
|
|
This looks like a question about linked list; a linked list is a simple data structure (http://en.wikipedia.org/wiki/Linked_list[^])
A list is a series of node, each node contains 2 things, some data and a pointer to the next node.
1. pointer2 is the current node, and next is the pointer (or better, address) of the the next node in the list.
2. pointer4 will point to the same address that pointer1 (it's just a simple variable assignation) (and in that case, pointer1 and pointer4 will point to the same thing).
3. it says that the data in the node pointer4 will point to the data of pointer1 (in that case, same data)
4. just weird... (saturday evening, and bad headache).
I find that if you start drawing (pen and paper) box with the data and the pointer on paper it makes things clearer.
good luck.
Watched code never compiles.
|
|
|
|
|
|
I work in VC6, on my job I have installed SDK 2003, at home I haven't ... my question is : there is a way to compiler knowing if is SDK installed or not ? I mean an #define SOMETHING or anything else ? Thnak you.
|
|
|
|
|
I think the version defines (WIN32_WINNT etc) will determine what features you can use. There is nothing specific that will identify the presence of the SDK.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Hi,
You can check for the existence of the platform SDK by checking for the VER_PRODUCTBUILD definition. It is defined in the ntverp.h header.
For VC6 it would be something like:
#if !defined(VER_PRODUCTBUILD) || VER_PRODUCTBUILD < 3790
#pragma message ("Error: You need the Microsoft Platform SDK!")
#endif
Best Wishes,
-David Delaune
|
|
|
|
|
Seems to work well at my home, but I want to try it on my job too ... I will tell you further what's going on ... thank you very much all of you guys !
|
|
|
|