have this code and when i run it, i get the output on the screen, what i would like to do is that i can see the output on the screen and
send it to an xls of cvs file. is there anyone
who could chage the code and let it working.
thanks for help
the code is :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
typedef struct link
{
struct link *next;
char word[24];
int numTimesAppear;
}Link;
int CreateAllNodes(Link **, Link **);
void WalkNode(Link *);
int DestroyAllNodes(Link *);
int AddToList(Link **pTheHead, char *Word);
/*********************************************************************************
This program is designed to read in a file, and sort the data into
two linked lists aplhabetically and display it a screen at a time.
LEGAND FOR MAIN
Dhead pointer to struct
Ahead pointer to struct
************************************************************************************/
int main(int argc, char *argv[])
{
Link *Dhead = NULL; // caps on struct and deleted struct key word
Link *Ahead = NULL;
//call function to creat linked list nodes
CreateAllNodes(&Dhead, &Ahead); // used the &
WalkNode(Ahead);
WalkNode(Dhead);
//Delete lists using DestroyAllNodes call; call twice, once for each list
DestroyAllNodes(Ahead);
DestroyAllNodes(Dhead);
printf("\n\tdone with the program\n\n");
return 0;
}
/*************************************************************************************
This function reads in the file and determines which link list the word should be
added to based on the beginning letter
LEGAND FOR CREATEALLNODES
NameBuffer char array
WordBuffer char array
myFile File name
Dhead Dhead in main
Ahead Ahead in main
*************************************************************************************/
int CreateAllNodes(Link **Dhead, Link **Ahead)
{
char NameBuffer[50];
char WordBuffer[24];
long iEndFile, iFSize;
FILE *MyFile, *outfile;
//request what file to open
printf("What File would you like to open?");
scanf("%s", NameBuffer );
getchar();
/*****
** @fopen check for file, open file
*******/
if ((MyFile = fopen( NameBuffer, "r")) == NULL)
{
printf ("\ncould not open the file '%s'\n", NameBuffer);
printf ("I have failed you... I'm sorry.\n\n");
return 0;
}
//@size get file size
fseek(MyFile, 0L, SEEK_END);
iEndFile = ftell(MyFile); // the end of the file is the size of the file
iFSize = iEndFile;
rewind(MyFile);
//read file
while (ftell(MyFile) < iEndFile)
{
//read data from file
fscanf (MyFile,"%s",WordBuffer);
//load nodes
//Compare characters to load in proper node using an if-else statement
if (WordBuffer[0] == 'z' || WordBuffer[0] == 'Z')
{
AddToList(Dhead, WordBuffer);
}
else
{
AddToList(Ahead, WordBuffer);
}
}
fclose(MyFile);
return 1;
}
/*************************************************************************************
This function is designed to walk along the nodes and output the data as it is stored
in the link list. It prints the data to the screen a screen size at a time (assuming
screen size to be 20 lines)
@LEGAND FOR WALKNODES
@headNode Ahead or Dhead in main
@currentNode equal to headNode
l@ines int
**************************************************************************************/
void WalkNode(Link *headNode)
{
Link *currentNode = headNode;
int lines = 0;
int i;
/*******
** @while loop through nodes and print out the info
*******/
while (currentNode != NULL)
{
printf("%-24s %5d\n", currentNode->word, currentNode->numTimesAppear);
lines++;
if (lines %20 == 19)
{
printf("Press enter for more data\n");
getchar();
}
currentNode = (Link *)currentNode->next;
}
//testing to make sure I am out of the loop
printf("\nEnd of list. Press Enter\n");
getchar();
}
/**************************************************************************
This function deletes all the nodes at the conclusion of the program
@LEGAND FOR DESTROYALLNODES
@headNode Ahead or Dhead in main
@CurrentNode pointer to head node
@tmpNodePtr temporary pointer for nodes
******************************************************************************/
int DestroyAllNodes(Link *headNode)
{
Link *tmpNodePtr = NULL;
Link *currentNode;
currentNode = headNode;
printf("\nClearing out the memory");
while (currentNode->next != NULL)
{
//Delete the nodes
tmpNodePtr = (Link *)currentNode->next;
free(currentNode);
currentNode = tmpNodePtr;
}
return 0;
}
/*************************************************************************************
This function creates the nodes in the linked list and sorts them alphabetically
@LEGAND FOR ADDTOLIST
@pTheHead head in Link
@Word word in Link
@newNode new node in List
@previousNode points to previous node
@theHead points to pTheHead
***********************************************************************************/
int AddToList(Link **pTheHead, char *Word)
{
Link *newNode = NULL;
Link *previousNode = NULL;
Link *theHead = *pTheHead;
while(1)
{
switch(theHead==NULL?-1:strcmp(Word, theHead->word))
{
case 0:
//equal value, add to count
theHead->numTimesAppear++;
return 0;
case 1:
//greater value, goto next node
previousNode = theHead;
theHead = theHead->next;
break;
case -1:
//lesser value, add node here
newNode = (Link *)malloc(sizeof(Link));
newNode->numTimesAppear = 1;
strncpy(newNode->word,Word,sizeof(newNode->word));
newNode->next = theHead;
if (previousNode == 0)
{
*pTheHead = newNode;
return 0;
}
previousNode->next = newNode;
return -1;
default:
printf("Sorry I'm dumb...");
break;
}
}
return 2;
}