Originally Posted by
BCWarrior
Ok thats the way I immagine that
Code:
struct item //name of the structure
{char name[20];int prc; int col; //structure of the thread
struct item *prev;
struct item *next;
};
void build(void); //builds new list
void view(item *);//view list
void add(item *); //add to list
void save(FILE *);//Save list to file prototype
struct item *head,*tail;
Then in function main I define a variable for file
Code:
main(){int c;
FILE *in;
And here the save function itself
Code:
void save(FILE *in)
{
p==head; //p -mean pointer
in=fopen("data.txt","wb"); //create or open file data.txt
while (p != NULL) //and untill the NULL of the list
{
fwrite(&item,sizeof(item),1,in); //write one structure size of item to file in binary mode
p=p->next; //move next structure
}
getchar();
}
What to send as a parameters? THe code I've posted before is the whole code exept of menu.
I dont know why you are still confused. Its simple as many people told you, i worked with text files so i can easily see the file without using a special program to read binary files or without afterwards loading it into memory to see if it succeeded. So the answer is the last function in my previous code.
Code:
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#define BUFSZ 512
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
typedef struct Rec
{
char *name;
int prc;
int col;
}Record;
typedef struct node
{
Record r;
struct node *previous;
struct node *next;
}Node;
typedef struct list
{
Node *head;
Node *tail;
}List;
List *CreateList(void)
{
List *l = calloc(1, sizeof(List));
if(l == NULL)
return NULL;
else
{
l->head = NULL;
l->tail = NULL;
return l;
}
}
Node *CreateNode(char *name, int Prc, int Col)
{
Node *n = calloc(1, sizeof(Node));
if(n == NULL)
return NULL;
else
{
n->next = NULL;
n->previous = NULL;
if(name == NULL)
{
free(n);
return NULL;
}
else
{
//You may use strdup here.
/*n->r.name = strdup(name);*/
//Init.
n->r.name = malloc((strlen(name) + 1) * sizeof(char));
strncpy(n->r.name, name, strlen(name));
n->r.name[strlen(name)] = '\0';
n->r.prc = Prc;
n->r.col = Col;
//Return the node.
return n;
}
}
}
//Add a new node to the end of the list
int AddNode(List *l, char *Nname, int Prc, int Col)
{
Node *n = NULL;
n = CreateNode(Nname, Prc, Col);
if(n)
{
if(l->head == NULL)
l->head = l->tail = n;
else
{
n->previous = l->tail;
l->tail->next = n;
l->tail = n;
}
return 0;
}
else
return -3;
}
void PrintList(List *l)
{
Node *temp = l->head;
printf("Printing List:\n");
while(temp)
{
printf("%s\t%d\t%d\n", temp->r.name, temp->r.prc, temp->r.col);
temp = temp->next;
}
}
void PrintListToFile(List *l, char *filename)
{
FILE *f = fopen(filename, "w");
if(f == NULL)
{
printf("Error, can not open '%s' file.\n", filename);
return;
}
else
{
Node *n = l->head;
char *Message = "Printing List Nodes.....";
fprintf(f,"%s\n", Message);
while(n)
{
if(fprintf(f,"%s\t%d\t%d\n", n->r.name, n->r.prc, n->r.col) > 0)
n = n->next;
}
fclose(f);
}
}
int main(int argc, char *argv[])
{
List *l = NULL;
l = CreateList();
AddNode(l, "Nick", 24,45);
AddNode(l, "Tom", 26,85);
AddNode(l, "Snow", 56,42);
PrintList(l);
PrintListToFile(l, "list.txt");
printf("Hit any Key to continue........\n");
getch();
return 0;
}
//---------------------------------------------------------------------------