#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <fstream>
using namespace std;
const int MAX_SIZE = 256;
struct node
{
char *str;
struct node *next;
};
void InsertWord( char * str, node **head)
{
int i=0;
struct node *p,*element;
p = *head;
int len = strlen(str);
char * strp = (char*)malloc(sizeof(len +1));
assert( (strp != NULL)&&(str != NULL));
strcpy(strp,str);
if(p->next == NULL && p->str == NULL)
{
p->str = strp;
}
else
{
element=(struct node*)malloc(sizeof(struct node));
assert(element != NULL);
memset(element, 0, sizeof(struct node));
element->str = strp;
element->next = p->next;
p->next=element;
p = element;
}
}
void GetWords( node **head)
{
char str[ MAX_SIZE];
ifstream InFile("E:\\test.txt");
while(!InFile.eof())
{
InFile.getline(str,MAX_SIZE);
const char *split = " ,;*.";
char *p = strtok(str,split);
while(p != NULL)
{
InsertWord(p,head);
p = strtok(NULL,split);
}
}
}
node * Init()
{
node * p = (node*)malloc(sizeof(node));
p ->str = NULL;
p->next = NULL;
return p;
}
void print( node*head){
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("%s\n",temp->str);
temp=temp->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
node *head;
head = Init();
GetWords(&head);
print(head);
return 0;
}