⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filedemo.c

📁 这是一些例程
💻 C
📖 第 1 页 / 共 2 页
字号:
                    err = 0;
                if( !err )
                {
                    if( !(err = GetFileTime( handle, date_time )) )
                    {
                        clear_scrn( disp_attr, 2, 17 );
                        SetCurPos( 12, 10 );
                        printf( "File's date and time stamp:  %s", date_time );
                        CloseFile( handle );
                        press();
                    }
                }
                break;

            /* Rename File */
            case 'J':
                strcpy( source, get_spec( 2 ) );
                err = RenameFile( source, get_spec( 3 ) );
                break;

            /* Delete File */
            case 'K':
                err = DelFile( get_spec( 3 ) );
                break;

            /* Create File with Unique Name */
            case 'L':
                strcpy( source, get_spec( 1 ) );
                handle = UniqueFile( 0, source );   /* Normal file attr = 0 */
                if( handle >= 0 )
                {
                    printf( "\n\nDOS creates file %s", source );
                    press();
                    err = 0;
                }
                else err = 1;
                break;

            /* Quick Move from one directory to another */
            case 'M':
                clear_scrn( disp_attr, 2, 17 );
                SetCurPos( 8, 0 );
                puts( move_msg );
                strcpy( source, get_spec( 2 ) );
                err = RenameFile( source, get_spec( 3 ) );
                break;

            /* Search files for specified text */
            case 'N':
                clear_scrn( disp_attr, 2, 17 );
                buffer = (char *) malloc( BUFFSIZE + 1 );
                if( buffer == NULL )
                {
                    SetCurPos( 12, 26 );
                    puts( "Insufficient memory for option" );
                    err = 1;
                    break;
                }
                SetCurPos( 7, 0 );
                puts( grep_msg );
                SetCurPos( 18, 0 );
                printf( "Enter search text:  " );
                GetStr( text, MAXSEARCH );

                /* Find first data file. */
                if( err = FindFirst( 0, get_spec( 3 ), &file ) )
                {
                    clear_scrn( disp_attr, 2, 17 );
                    SetCurPos( 12, 24 );
                    puts( "No files found matching specification" );
                }

                /* If file found, initialize screen coordinates and
                 * open file for reading.
                 */
                else
                {
                    clear_scrn( disp_attr, 2, 17 );
                    row = 6;
                    col = 0;
                    do
                    {
                        if( (handle = OpenFile( 0, file.filename )) != -1 )
                        {

                            /* If file opened successfully, read a block
                             * of BUFFSIZE bytes. If end-of-file encountered
                             * (number of bytes read not equal to BUFFSIZE)
                             * or read error, set error flag to break loop.
                             * Terminate block with a NULL character to 
                             * make it an ASCIIZ string.
                             */
                            err = 0;
                            while( !err )
                            {
                                len = ReadFile( handle, BUFFSIZE, buffer );
                                if( (len == 0)  ||  (len != BUFFSIZE) )
                                    ++err;
                                ptr = buffer;
                                *( ptr + len ) = 0;

                                /* Search block for first character in text */
                                while( spec = StrFindChar( text[0], ptr, 0 ) )
                                {

                                    /* If initial character found, compare
                                     * remaining characters in search text.
                                     * If all characters match, display file
                                     * name and break out of loop.
                                     */
                                    ptr = StrCompare( ++spec, &text[1],
                                          (strlen( text ) - 1) );
                                    if( !ptr )
                                    {
                                        SetCurPos( row++, col );
                                        puts( file.filename );
                                        if( row == 16)
                                        {
                                            row  = 6;
                                            col += 20;
                                        }
                                        err  = 1;
                                        break;
                                    }
                                }
                            }
                            CloseFile( handle );
                        }
                        else
                        {
                            err = 1;
                            break;
                        }
                    } while( !FindNext( &file ) );

                    if( (row == 6)  &&  (col == 0) )
                    {
                        SetCurPos( 12, 22 );
                        puts( "Text not found in specified file(s)" );
                    }
                    press();
                    err = 0;
                }
                free( buffer );             /* Free allocated block */
                break;

            default:
                continue;
        }

        if( err )
        {
            clear_scrn( disp_attr, 24, 24 );
            SetCurPos( 24, 0 );
            printf( "***  Error  ***\a" );
            press();
        }

    } while( ch != ESCAPE );            /* Exit if ESC key pressed    */

    clear_scrn( disp_attr, 0, 24 );     /* Clear screen before exit   */
    SetCurPos( 23, 0 );                 /*   and set cursor to bottom */
    return( 0 );
}


/* get_spec - Prompts for file or path specifications.
 *
 * Params:  n - Prompt number
 *
 * Return:  Pointer to specification
 */

char *get_spec( int n )
{
    static char spec[MAXSPEC];

    switch( n )
    {
        case 1:
            SetCurPos( 18, 0 );
            printf( "Enter path:  " );
            break;
        case 2:
            SetCurPos( 18, 0 );
            printf( "Enter source file:  " );
            break;
        case 3:
            SetCurPos( 20, 0 );
            printf( "Enter target file:  " );
            break;
    }
    GetStr( spec, MAXSPEC );
    if ( *spec == '\0' && n == 1 )
    {
        strcpy ( spec, "\\" );      /* Use current directory for <return> */
        strcat ( spec, source );
    }
    return spec;
}


/* list_dir - Lists specified directory by invoking DOS Function 4Eh (Find
 * First Matching Entry), then DOS Function 4Fh (Find Next Matching Entry).
 *
 * Params:  spec - Pointer to path specification
 * 
 * Return:  err  - Error code returned from FindFirst procedure:
 *                 0 = successful         1 = error
 */

int list_dir( char *spec, int disp_attr )
{
    int row, col;
    int err, end;
    clear_scrn( disp_attr, 2, 23 );

    if ( spec[strlen(spec) - 1] == '\\' )       /*Remove trailing backslash*/
         spec[strlen(spec) - 1] = '\0';
    strcat( spec, "\\*.*" );
    err = FindFirst( 16, spec, &file ); /* Find directories, normal files */
    if( !err )
    {
        for( end = 0, col = 0; (col < 80)  &&  (end == 0); col += 22 )
        {
            for( row = 5; (row < 20)  &&  (end == 0); row++ )
            {
                SetCurPos( row, col );
                puts( file.filename );
                end = FindNext( &file );
            }
        }
    }
    return err;
}


/* press - Prompt for keyboard signal to continue.
 *
 * Params:  None
 */

void press( void )
{
    SetCurPos( 24, 49 );
    printf( ". . . press a key to continue" );
    SetCurPos( 24, 47 );
    getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -