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;
}

If you want the output to go to two places (screen and file) then you will have to print it twice. Put the code in a function then pass it the stream object (either an opened FILE* pointer or stdout)

Edited 7 Years Ago by Ancient Dragon: n/a

Hi Ancient Dragon,

Thank you for you reaction but would you like to give me more detail about the founctie i have to do, i tried to make a new pointer with the name *OutPutfile but it doesn't work, please how could i do that.

thanks,

simple example:

#include <stdio.h>

void foo( FILE* out )
{
    fprintf(out,"Hello World\n");

}

int main()
{
    FILE* fp;
    foo(stdout);
    fp = fopen("test.txt", "wt");
    foo(fp);
    fclose(fp);
    return 0;
}

Hi Ancient Dragon,
thanks again, but i don't like to print the out on a txt file or csv file,
what i would like to do is :
open or red a txtfiel, when the txtfile is open, i can see the content,
now i like to write a function to save the output to another file
for example txtfile.
in the code i have to open a textfile and read the content and sort the words and then save it to .csv file.
the code i have wrote, opens a textfile and reads the content of the textfiel and sorts the words but don't save the output. so that what i want to do.

This article has been dead for over six months. Start a new discussion instead.