回 帖 发 新 帖 刷新版面

主题:急死了还有二天要交大作业了,一个结点赋值却总是要出错!

老师布置的大作业,一共有main.c, fileADT.c, fileADT.h三个文件组成(代码附后面了),好不容易开通宵整出来了,编译也通过了,可是在"命令提示符"状态下一运行(book-unformatted和book-formatted是老师指定的):

main book-unformatted book-formatted

windows就提示main.exe遇到问题要关闭.
现在发现问题是在文件关闭时,要把头结点的值赋给行结点的值,就是下面这句出错:
linenodeptr = fileADT->line1;

二个结点的定义是这样的:
typedef struct fileADT
{
    int size;
    struct linenode * line1;
} FileADT; 



typedef struct linenode
{
    struct chnode * chhead;
    struct linenode *nextline;
} LINENODE; 

打开和关闭文件的函数如下:
int FileOpen(FileADT* fileADT, char* filename)
{
    FILE *stream;
    char readch;
    LINENODE * linenodeptr;
    CHNODE * chnodeptr = NULL;
    
    stream = fopen( filename, "r" );
    if( stream == NULL )
    {
        return -1;
    }

    fileADT = (FileADT*) malloc (sizeof(FileADT*));
    
    //if (fileADT==NULL)
    //{printf("Error!");
     //return 0;
    //}


    linenodeptr =(LINENODE*) malloc( sizeof(LINENODE) );
    if( linenodeptr != NULL )
    {
        linenodeptr->nextline = NULL;
        linenodeptr->chhead = NULL;
        fileADT->line1 = linenodeptr;
        printf("%d", fileADT->line1);

    }

    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;

int FileClose(FileADT* fileADT, char* filename)
{
    FILE *stream;
    LINENODE* linenodeptr;
    CHNODE * chnodeptr;

    printf("%d", fileADT->line1);
    linenodeptr = fileADT->line1;

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

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



     FileOpen(fileADT, argv[1]);

     FileClose(fileADT, argv[2]);

    return 0;
}


急死了还有三天要交作业了,现在还不知道函数写得对不对.
哪位高人教我几招?感激不尽啊[em10]



回复列表 (共6个回复)

沙发

// fileADT.h 




板凳

// fileADT.c 

3 楼

h

4 楼

/

5 楼

江湖救急啊[em8]

6 楼

江湖救急啊

我来回复

您尚未登录,请登录后再回复。点此登录或注册