main book-unformatted book-formatted
windows就提示main.exe遇到问题要关闭.
急死了还有三天要交作业了,现在还不知道函数写得对不对.
哪位高人教我几招?感激不尽啊
/*main.c*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "fileADT.h"
int main(int argc, char* argv[])
{
FileADT* fileADT=NULL;
char*string;
char*find;
char*replace;
char*filename;
int line, cutLine, pasterLine;
if (argc!=3)
{
printf ("Usage: nroffGold <input_file> <output_file>\n");
return EXIT_FAILURE;
}
/*Read in two file names as command line arguments: the first argument is the input (unformatted)
filename and the second argument is output (formatted) filename. To test your program, you may copy
the sample file /public/courses/AdvProgrammingTechniques/asg1/book-unformatted into your working
directory and use its name as the first argument. The first output from this sample input should look
like: /public/courses/AdvProgrammingTechniques/asg1/book-formatted file. */
/*Initialise an instance of FileADT ADT instance and store the entire file in the ADT. */
FileOpen(fileADT, argv[1]);
/*Format the contents in the FileADT ADT instance according to the specifications given above. */
FileFormat(fileADT);
/*Display the formatted file on screen. */
FileDisplay(fileADT);
/*Insert the following text as the line 5 of the file.
Better to remain silent and be thought a fool than to speak out and remove all doubt. */
FileInsertLine(fileADT, string, line);
/*Format the contents in the FileADT ADT instance according to the specifications given above. */
FileFormat(fileADT);
/*Display the formatted file on screen. */
FileDisplay(fileADT);
/*Append the following line to the end of the file.
I don't know who my grandfather was; I am much more concerned to know who his grandson will be.*/
FileAppend(fileADT, string);
/*Format the contents in the FileADT ADT instance according to the specifications given above. */
FileFormat(fileADT);
/*Display the formatted file on screen. */
FileDisplay(fileADT);
/*Display the lines which contain the word "fool". */
FileGrep(fileADT, string);
/*Cut the third line from the file and paste it as the 5th line.*/
FileFindAndReplace(fileADT, find, replace);
/*Format the contents in the FileADT ADT instance according to the specifications given above. */
FileFormat(fileADT);
/*Display the formatted file on screen. */
FileDisplay(fileADT);
/*Replace all the occurances of the word "Real" with the word "Genuine". */
FileCutAndPasteLine(fileADT, cutLine, pasterLine);
/*Format the contents in the FileADT ADT instance according to the specifications given above. */
FileFormat(fileADT);
/*Display the formatted file on screen. */
FileDisplay(fileADT);
/*Delete the third line of the file. */
FileDeleteLine(fileADT, line);
/*Format the contents in the FileADT ADT instance according to the specifications given above. */
FileFormat(fileADT);
/*Display the formatted file on screen. */
FileDisplay(fileADT);
/*Save the contents of the FileADT ADT instance in the disk file given in the second command line argument, free up the memory used by the ADT instance and destroy the entire ADT instance.*/
FileClose(fileADT, filename);
return 0;
}
第 1 楼
// fileADT.h /*The entire ADT is made up of three types of nodes: A main-header node, line-header nodes, and character nodes. The main-header node is considered as the public portion of the ADT, where the client program has the access. The main-header node comprises of a pointer to the first line-header node and an integer representing the file size. Each line-header node consists of a pointer to the first character node in that line and a pointer to the next line-header. Of course, the character nodes comprises of a character and a pointer to next character node. So, you can safely assume that the entire structure is a linked list of linked lists. */ typedef struct henode { int size; struct henode * line1; } FileADT; typedef struct chnode { char ch; struct chnode * nextch; } CHNODE; typedef struct linenode { struct chnode * chhead; struct linenode *nextline; } LINENODE; /* FileOpen - Initialises a FileADT instance. Then, it opens the disk file specified in the second argument and loads up the ADT instance with data (meaning lines and characters) in the disk file. Finally, it fixes the file size (i.e. number of characters loaded to the FileADT) on the header node, closes the disk file and returns the success (or failure) status to the client. */ int FileOpen(FileADT* fileADT, char* filename); /*Format the contents in the FileADT ADT instance according to the specifications given above. */ int FileFormat(FileADT* fileADT); /* FileDisplay - displays the entire FileADT, as appeared in the example in Introduction section. This function will do no changes into the FileADT, but to keep the interface uniform across all functions, the first argument (name of the FileADT instance) is passed as a reference, i.e. a pointer to the FileADT). */ int FileDisplay(FileADT* fileADT); /* FileInsertLine - inserts the text passed in the second argument (char*) as a line above the line specified in the third argument. If these arguments are invalid (for various different reasons), the function will do nothing and returns failure status. If the client wants to insert the new text as line 5, it will be inserted as new line 5, pushing rest of the lines downwards; old 5h line becomes 6th, and so on. */ int FileInsertLine(FileADT* fileADT, char*string, int line); /* FileAppend - inserts the text passed in the second argument (char*) below the last line of the existing FileADT. */ int FileAppend(FileADT* fileADT, char* string); /* FileGrep - this function searches the FileADT for the pattern specified in the second argument and displays the lines containing the given pattern. The behaviour of this function should be very similar to the standard UNIX grep command when used without any advanced options. In unix, $ grep hackers book-formatted will return the following line. themselves `hackers', either, or anything in particular; the This function should also produce a similar output. Note that, this function do not make any changes to the FileADT ADT instance. */ int FileGrep(FileADT* fileADT, char* string); /* FileDeleteLine - as its name explains, it deletes an entire line, specified by the line number in the second argument. It first checks if that line exists, if so, first removes the character nodes in the line, and then the line-header node will be removed. Make sure you do not leave any stale nodes leading to memory leaks. Success or failure will be returned to the client. */ int FileDeleteLine(FileADT* fileADT, int line); /* FileFindAndReplace - as its name explains, this function searches for all occurances of the word specified in the second argument and replaces all such occurances with the word specified in the third argument. */ int FileFindAndReplace(FileADT* fileADT, char* find, char* replace); /* FileCutAndPasteLine - as its name explains, it removes the line specified in the second argument and insert the contents of the removed line into the line specified in the third argument. Pasting part is done in a very similar way to the FileInsertLine() function. */ int FileCutAndPasteLine(FileADT* fileADT, int cutLine, int pasteLine); /* FileClose - this function opens a disk file (specified in the second argument) in the write mode, copies the entire FileADT into the disk file, closes the disk file, and finally destroys the entire FileADT ADT instance by systematically eliminating character nodes, line-header nodes and main-header node. */ int FileClose(FileADT* fileADT, char* filename); |
[此贴子已经被作者于2006-4-3 22:27:14编辑过]