| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 978 人关注过本帖
标题:急死了还有三天要交大作业了,一个指针链表操作文件的大题就是无法运行
取消只看楼主 加入收藏
空明七心
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-3-18
收藏
 问题点数:0 回复次数:1 
急死了还有三天要交大作业了,一个指针链表操作文件的大题就是无法运行
老师布置的大作业,一共有main.c, fileADT.c, fileADT.h三个文件组成(代码附后面了),好不容易开通宵整出来了,编译也通过了,可是在"命令提示符"状态下一运行(book-unformatted和book-formatted是老师指定的):

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编辑过]

搜索更多相关主题的帖子: 链表 交大 指针 作业 文件 
2006-04-03 22:22
空明七心
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-3-18
收藏
得分:0 

// fileADT.c

#include "fileADT.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

/*
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)
{
FILE *stream;
char readch;
LINENODE * linenodeptr;
CHNODE * chnodeptr = NULL;

stream = fopen( filename, "r" );
if( stream == NULL )
{
return -1;
}

linenodeptr =(LINENODE*) malloc( sizeof(LINENODE) );
if( linenodeptr != NULL )
{
linenodeptr->nextline = NULL;
linenodeptr->chhead = NULL;
fileADT->line1 = linenodeptr;
}

while (fread( &readch, sizeof(char), 1, stream )>0 )
{
CHNODE * chnode;

chnode = (CHNODE *) malloc( sizeof(CHNODE) );

chnode->ch = readch;
chnode->nextch = NULL;

if( chnodeptr == NULL )
{
LINENODE * linenode;
linenode = (LINENODE *) malloc( sizeof(LINENODE) );

linenode->nextline = NULL;
linenode->chhead = chnode;
chnodeptr = chnode;

linenodeptr->nextline = linenode;
linenodeptr = linenode;
}
else
{
chnodeptr->nextch = chnode;
chnodeptr = chnode;
}
if( readch == '\n' )
{
chnodeptr = NULL;
}
}
fclose( stream );

return 0;
}
/*This function formats a raw FileADT ADT instance into a
formatted FileADT ADT instance similar to the formatting done by
standard UNIX nroff utility. You are not required to mimic the
behaviour of GNU nroff utility. */

int FileFormat(FileADT* fileADT)
{

LINENODE * linenodeptr;
LINENODE * linenode = NULL;
LINENODE * linenode1 = NULL;
CHNODE * chnode = NULL;
CHNODE * chnode1 = NULL;
CHNODE * chnode2 = NULL;
CHNODE * chnode3 = NULL;
CHNODE * chnodeptr = NULL;
int i1=0,i2=0,k;

linenodeptr = fileADT->line1;
chnodeptr = linenodeptr->chhead;
linenode = linenodeptr;
linenode1 = linenodeptr;
chnode = chnodeptr;
chnode1 = chnodeptr;
chnode2 = chnodeptr;
chnode3 = chnodeptr;

while (linenodeptr->nextline != NULL && chnodeptr->nextch != NULL )
{ if (i1<56 && chnodeptr->nextch != NULL){
chnodeptr = chnodeptr->nextch;
i1++;}

if (linenodeptr->nextline = NULL){
linenode = (LINENODE *) malloc( sizeof(LINENODE) );
linenodeptr->nextline = linenode;
linenode->nextline = NULL;}

linenodeptr = linenodeptr->nextline;
chnode = linenodeptr->chhead;
linenodeptr->chhead = chnodeptr->nextch;
chnode1 = chnodeptr;
if (chnodeptr->nextch != NULL){
chnodeptr = chnodeptr->nextch;}
chnodeptr->nextch = chnode;
chnode1->nextch = NULL;


for (k=0; k < (66-i1); k++){

if (chnode2->ch == " "){
chnode = (CHNODE *) malloc(sizeof(CHNODE));
chnode->ch = " ";
chnode->nextch = chnode2->nextch;
chnode2->nextch = chnode;}
if (chnode2->nextch == NULL){chnode2 = chnode3;}
}

i1=0;
chnodeptr = linenodeptr->chhead;
linenode = linenodeptr;
linenode1 = linenodeptr;
chnode = chnodeptr;
chnode1 = chnodeptr;
chnode2 = chnodeptr;
chnode3 = chnodeptr;

}


return 0;
}

/*
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)
{
LINENODE * linenodeptr = fileADT->line1;
CHNODE * chnodeptr;

while( linenodeptr != NULL )
{
chnodeptr = linenodeptr->chhead;
while( chnodeptr != NULL )
{
printf("%c", chnodeptr->ch);
chnodeptr = chnodeptr->nextch;
}
linenodeptr = linenodeptr->nextline;
}

return 0;
}

/*
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)
{
LINENODE * linenodeptr;
LINENODE * linenode;

CHNODE * chnodeptr = NULL;
int i;

linenodeptr = fileADT->line1;
while( linenode != NULL && i < line )
{
linenode = linenodeptr->nextline;
i++;
}


while (*string != 0 )
{
CHNODE * chnode;

chnode = (CHNODE *) malloc( sizeof(CHNODE) );

chnode->ch = *string;
chnode->nextch = NULL;

if( chnodeptr == NULL )
{
linenodeptr = (LINENODE *) malloc( sizeof(LINENODE) );
if( linenode != NULL )
{
linenode->chhead = chnode;
linenode->nextline = linenodeptr->nextline;
linenodeptr->nextline = linenode;
}
}
else
{
chnodeptr->nextch = chnode;
chnodeptr = chnode;
}
if( *string == '\n' )
{
chnodeptr = NULL;
}
string ++;
}
return 0;
}

/*
FileAppend - inserts the text passed in the second argument (char*)
below the last line of the existing FileADT.
*/
int FileAppend(FileADT* fileADT, char* string)
{
LINENODE * linenodeptr;
LINENODE * linenode;
CHNODE * chnodeptr = NULL;

linenodeptr = fileADT;

while( linenodeptr->nextline != NULL )
{
linenodeptr = linenodeptr->nextline;
}


while (*string != 0 )
{
CHNODE * chnode;

chnode = (CHNODE *) malloc( sizeof(CHNODE) );

chnode->ch = *string;
chnode->nextch = NULL;

if( chnodeptr == NULL )
{
linenode = (LINENODE *) malloc( sizeof(LINENODE) );
if( linenode != NULL )
{
linenode->chhead = chnode;
linenode->nextline = linenodeptr->nextline;
linenodeptr->nextline = linenode;
}
}
else
{
chnodeptr->nextch = chnode;
chnodeptr = chnode;
}
if( *string == '\n' )
{
chnodeptr = NULL;
}
string++;
}
return 0;
}

/*
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)
{
LINENODE * linenodeptr;
LINENODE * linenode = NULL;
CHNODE * chnode = NULL;
CHNODE * chnodeptr = NULL;
char* string1 = string;

linenodeptr = fileADT->line1;
chnodeptr = linenodeptr->chhead;
linenode = linenodeptr;
chnode = chnodeptr;

while (*string != 0 )
{

while (chnode->ch == *string)
{chnode=chnode->nextch;
*string1++;
}

if (*string1==0)
{ string1 = string;
while (*string != 0 )
{ printf("%c",*string);
*string++;
}
} else {string1 = string;}


if( chnode->nextch == NULL )
{ if (linenodeptr->nextline != NULL){
chnodeptr=linenodeptr->nextline;
chnode = chnodeptr;
linenodeptr=linenodeptr->nextline;}
else {string=0;}
}
}
return 0;
}

/*
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)
{
LINENODE * linenodeptr;
LINENODE * linenode;
int i;
CHNODE * chnodeptr = NULL;
CHNODE * chnode = NULL;

linenodeptr = fileADT->line1;
while( linenodeptr != NULL && i < line-1 )
{
linenodeptr = linenodeptr->nextline;
i++;
}

linenode = linenodeptr->nextline;
linenodeptr->nextline = linenode->nextline;
chnodeptr = linenode->chhead;

while (chnodeptr != NULL)
{
chnode = chnodeptr;
chnodeptr = chnodeptr->nextch;
free(chnode);
}
free( linenode );

return 0;
}

/*
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)

{
LINENODE * linenodeptr;
LINENODE * linenode;
CHNODE * chnode;
CHNODE * chnode1;
CHNODE * chnodeptr = NULL;
char* find1 = find;
char* replace1 = replace;

linenodeptr = fileADT->line1;
chnodeptr = linenodeptr->chhead;
linenode = linenodeptr;
chnode = chnodeptr;


while (*find1 != 0 )
{
do{
while (chnode->ch == *find1)
{chnode=chnodeptr->nextch;
*find1++;
}

if (*find1==0)
{ while (*replace != 0)
{
chnode1 = (CHNODE *) malloc( sizeof(CHNODE) );
chnode1->ch = *replace;
chnode1->nextch = NULL;
chnodeptr->nextch=chnode1;
chnodeptr=chnode1;
*replace++;}


chnodeptr->nextch=chnode;
chnodeptr=chnode;
}

if( chnodeptr->nextch == NULL )
{ chnodeptr=linenodeptr->nextline;
chnode = chnodeptr;
linenodeptr=linenodeptr->nextline;
}



*find1=*find;} while (linenodeptr->nextline != NULL);

}
return 0;
}

/*
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)
{
LINENODE * linenodeptr;
LINENODE * linenode = NULL;

CHNODE * chnodeptr = NULL;
int i1, i2;

linenodeptr = fileADT->line1;

while( linenode == NULL && i1 < cutLine )
{
linenodeptr = linenodeptr->nextline;/*right????*/
i1++;
}

while( linenode != NULL && i2 < pasteLine )
{
linenode = linenode->nextline;/*right????*/
i2++;
}

linenode->chhead = linenodeptr->chhead;
linenodeptr->chhead = NULL;



return 0;
}

/*
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)
{
FILE *stream;
LINENODE* linenodeptr;
CHNODE * chnodeptr;

stream = fopen( filename, "w" );
if( stream == NULL )
{
return -1;
}

while( linenodeptr != NULL )
{
chnodeptr = linenodeptr->chhead;
while( chnodeptr != NULL )
{
fwrite(&(chnodeptr->ch), sizeof(char), 1, stream );
chnodeptr = chnodeptr->nextch;
}
linenodeptr = linenodeptr->nextline;
}

fclose( stream );

linenodeptr = fileADT->line1;
while( linenodeptr->nextline !=NULL )
FileDeleteLine(linenodeptr, 1);
free(linenodeptr);

return 0;
}

2006-04-03 22:28
快速回复:急死了还有三天要交大作业了,一个指针链表操作文件的大题就是无法运行 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029611 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved