Hmm...I dont really get it...Sorry bout that~ Does that mean i should post the source code for my whole program? Then i think i'll post it~ And i think i'll also provide a link so that you can DL the whole program..
FileADT
Small explaination:
What this program does is read in text file through command line arguement and store each character in a character node. After every "\n" or new line, it moves on to the next line node and store characters again and the whole process is repeated again..After that it will left justify the text and display the output...
fileadt.h
Code:
/* Header files. */
#include <stdio.h>
#include <stdlib.h>
typedef struct charStruct
{
char alphabet;
struct charStruct* nextChar;
} CharacterNode;
typedef struct lineHeaderStruct
{
CharacterNode* firstChar;
struct lineHeaderStruct* nextLine;
} LineHeaderNode;
typedef struct mainHeaderStruct
{
int fileSize;
LineHeaderNode* firstLine;
} FileADT;
/* Function prototypes. */
int FileOpen(FileADT *, char *);
void FileFormat(FileADT *);
void FileDisplay(FileADT *);
fileadt.c
Code:
/* Header Files */
#include "fileadt.h"
#include <stdio.h>
#include <stdlib.h>
/* Function Prototypes */
void checkArguments(int);
/* Main Program which takes in arguments from Unix*/
int main(int argc, char *argv[])
{
FileADT fileNode;
int status;
checkArguments(argc);
status = FileOpen(&fileNode, argv[1]);
if(status <= 0)
{
return EXIT_FAILURE;
}
FileFormat(&fileNode);
FileDisplay(&fileNode);
return EXIT_SUCCESS;
}
options.c
Code:
/* Header Files */
#include "fileadt.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* Define Constants */
#define LINESIZE 69
/* Function prototypes */
int checkFileExists();
CharacterNode* createNewCharNode(char);
LineHeaderNode* createNewLineNode();
int FileOpen(FileADT *fileNode, char *filename)
{
FILE *input;
LineHeaderNode *newLineNode, *currentLineNode;
CharacterNode *newCharNode, *currentCharNode;
int success, character;
int totalChar = 0;
newLineNode = malloc(sizeof(LineHeaderNode));
newCharNode = malloc(sizeof(CharacterNode));
currentLineNode = malloc(sizeof(LineHeaderNode));
currentCharNode = malloc(sizeof(CharacterNode));
if(newLineNode == NULL || newCharNode == NULL || currentLineNode == NULL || currentCharNode == NULL)
{
printf("Not enough memory to create new node!!");
printf("\nTerminating...\n");
exit(-1);
}
/* Opening file for reading */
input = fopen(filename, "r");
success = checkFileExists(input);
if(success >= 0)
{
/* Initialising ADT */
fileNode->firstLine = NULL;
fileNode->fileSize = 0;
currentLineNode = fileNode->firstLine;
/* Gets character from input file */
while((character = fgetc(input)) != EOF)
{
/*If firstLine of node is already created */
if(currentLineNode != NULL)
{
currentCharNode = currentLineNode->firstChar;
newCharNode = createNewCharNode(character);
/* If there are no characters yet in the lineNode */
if(currentCharNode == NULL)
{
currentLineNode->firstChar = newCharNode;
}
/* If there are already characters in the lineNode */
else
{
/*Traverse character node until the last character */
while(currentCharNode->nextChar != NULL)
{
currentCharNode = currentCharNode->nextChar;
}
currentCharNode->nextChar = newCharNode;
}
}
/* If firstLine of node is not yet created */
else
{
newLineNode = createNewLineNode();
fileNode->firstLine = newLineNode;
newCharNode = createNewCharNode(character);
newLineNode->firstChar = newCharNode;
currentLineNode = newLineNode;
}
/* If character is ENTER then create a newLineNode and move to the newly created line node */
if(character == '\n')
{
newLineNode = createNewLineNode();
currentLineNode->nextLine = newLineNode;
currentLineNode = newLineNode;
}
totalChar++;
}
/* Close file */
fclose(input);
return 1;
}
else
{
return -1;
}
}
void FileDisplay(FileADT *fileNode)
{
LineHeaderNode *currentLineNode;
CharacterNode *currentCharNode;
currentLineNode = malloc(sizeof(LineHeaderNode));
currentCharNode = malloc(sizeof(CharacterNode));
if(currentLineNode == NULL || currentCharNode == NULL)
{
printf("Not enough memory to create new node!!");
printf("\nTerminating...\n");
exit(-1);
}
currentLineNode = fileNode->firstLine;
currentCharNode = fileNode->firstLine->firstChar;
while(currentCharNode != NULL)
{
printf("%c", currentCharNode->alphabet);
if(currentCharNode->alphabet == '\n')
{
currentLineNode = currentLineNode->nextLine;
currentCharNode = currentLineNode->firstChar;
}
else
{
currentCharNode = currentCharNode->nextChar;
}
}
}
void FileFormat(FileADT *fileNode)
{
LineHeaderNode *newLineNode, *currentLineNode, *nextLineNode;
CharacterNode *newCharNode, *currentCharNode, *previousCharNode;
int lineLength = 1;
newLineNode = malloc(sizeof(LineHeaderNode));
newCharNode = malloc(sizeof(CharacterNode));
currentLineNode = malloc(sizeof(LineHeaderNode));
currentCharNode = malloc(sizeof(CharacterNode));
nextLineNode = malloc(sizeof(LineHeaderNode));
previousCharNode = malloc(sizeof(CharacterNode));
if(newLineNode == NULL || newCharNode == NULL || currentLineNode == NULL ||
currentCharNode == NULL || nextLineNode == NULL || previousCharNode == NULL)
{
printf("Not enough memory to create new node!!");
printf("\nTerminating...\n");
exit(-1);
}
currentLineNode = fileNode->firstLine;
currentCharNode = fileNode->firstLine->firstChar;
while(currentCharNode != NULL)
{
if(lineLength > LINESIZE)
{
newCharNode = createNewCharNode('\n');
newLineNode = createNewLineNode();
currentLineNode->nextLine = newLineNode;
newLineNode->nextLine = nextLineNode;
previousCharNode->nextChar = newCharNode;
newLineNode->firstChar = currentCharNode;
currentLineNode = newLineNode;
nextLineNode = currentLineNode->nextLine;
lineLength = 0;
}
else
{
if(currentCharNode->alphabet == '\n')
{
currentLineNode = currentLineNode->nextLine;
nextLineNode = currentLineNode->nextLine;
currentCharNode = currentLineNode->firstChar;
lineLength = 0;
}
else
{
previousCharNode = currentCharNode;
currentCharNode = currentCharNode->nextChar;
}
}
lineLength++;
}
}
utility.c
Code:
/* Header Files */
#include "fileadt.h"
#include <stdlib.h>
#include <stdio.h>
/* Function prototypes */
void checkArguments(int);
int checkFileExists();
CharacterNode* createNewCharNode(char);
LineHeaderNode* createNewLineNode();
/* Checks if total arguments are correct */
void checkArguments(int argc)
{
if (argc != 3)
{
fprintf(stderr,"Usage: fileadt <inputfile> <outputfile>\n");
exit(EXIT_FAILURE);
}
}
/* Checks if external file exists */
int checkFileExists(FILE *file)
{
if(file == NULL)
{
fprintf(stderr,"Failed to open input file!! \n");
fprintf(stderr,"Exiting program....\n");
return -1;
}
else
{
printf("Input file opened successfully!!\n");
return 1;
}
}
CharacterNode* createNewCharNode(char character)
{
CharacterNode *newCharNode;
newCharNode = malloc(sizeof(CharacterNode));
if(newCharNode == NULL)
{
printf("Not enough memory to create new node!!");
printf("\nTerminating...\n");
exit(-1);
}
newCharNode->alphabet = character;
return newCharNode;
}
LineHeaderNode* createNewLineNode()
{
LineHeaderNode *newLineNode;
newLineNode = malloc(sizeof(LineHeaderNode));
if(newLineNode == NULL)
{
printf("Not enough memory to create new node!!");
printf("\nTerminating...\n");
exit(-1);
}
return newLineNode;
}
And here's the text file, the first one works but the second one doesnt..
Code:
What A Wonderful Weekend
It was Thursday. We were looking forward to Friday and the weekend that follows.
The wind continued to blow from Sumatra and the
air pollution index (API) went up and up. In the afternoon, the ministry announced that
all schools be closed for the rest of the day as well as Friday. Cancellation of
classes has always been the best thing that can ever happen to students and lecturers.
On this day,
the API
told us that it was unhealthy to be happy at all.
Friday evening, when i was sitting down for dinner at the poolside, thinking that we
were finally outside the house, a horrifying sight hit me, our beautiful pools had
turned brownish green!
Needless to say, for the rest of the weekend, we had to stay indoor watching even the
silliest TV programmes.
This one doesnt work...
Code:
Today, computer software is the single most important technology on the world stage.
And it is also a prime example of the law of unintended consequences.
No one in the 1950s could have predicted that software would become an indispensable technology for business, science, and engineering;
that software would enable the creation of new technologies (e.g., genetic engineering), the extension of existing technologies (e.g., telecommunications),
and the demise of older technologies (e.g., the printing industry); that software would be the driving force behind the personal computer revolution;
that shrink-wrapped software products would be purchased by consumers in neighborhood malls;
that a software company would become larger and more influential than the vast majority of industrial-era companies;
that a vast software-driven network called the Internet would evolve and change everything from library research to consumer shopping to the dating habits of young (and not-so-young) adults.
Thanks for not having a go with me (yet??), Salem~ Hope i didnt do anything wrong this time..And hope you all can help me out with this! Thanks again~