#include "stdio.h"
#include "conio.h"
struct filesys_superblk
unsigned long fs_size/*整个李灶分区的总磁盘物理块数 */
unsigned long fs_freesize/*分区的所有空闲磁盘物理块数 */
unsigned int fs_blocksize/*文件系统的物理块大小(字节)*/
unsigned int fs_fat_start/*FAT的起始磁盘物理块号 */
unsigned int fs_fat_size/*FAT占用的磁盘物理块数*/
unsigned int fs_dir_start/*根目录的起始磁盘物理块号*/
unsigned int fs_dir_size/*根目录占用的磁盘物理块数*/
unsigned int fs_data_start/*数据区起始磁盘物理块号*/
unsigned long fs_data_size/敬罩*数据区的磁盘物理块数*/
}
struct FILE_FCB
{ /*文件控制块结构 */
char f_name[16]/*文件名(16B)*/
short f_mode/*文件属性,-1表示未用 0表示目录 1表示文件*/
unsigned int f_asize/*文件分配的大小(物理块数)*/
unsigned long f_rize/*文件占用的实际大小(字节)*/
unsigned long f_addr/*文件分配的第一个物理块的块号*/
unsigned int f_ctime/*文件创建时间*/
unsigned int f_mtime/*文件修改时间*/
}
#include <stdio.h>#include <stdlib.h>//为了使用exit()
int main()
{
char ch
FILE* fp
char fname[50]//用于存放文件名
printf("输入文件名:")
scanf("%s",fname)
fp=fopen(fname,"r")//只供读取
if(fp==NULL) //如果失败了
{
printf("错误灶和键!"棚此)
exit(1)//中止程序
}
//getc()用于在打开文件中获取一个字符
while((ch=getc(fp))!=EOF)
putchar(ch)
fclose(fp)//关闭文件
return 0
}
注意!初学者往往会犯一个错误,即在输入文件名时不加后缀名隐巧,请注意加上!
程序示例2[2]
#include <stdio.h>
FILE *stream, *stream2
int main( void )
{
int numclosed
// Open for read (will fail if file "crt_fopen.c" does not exist)
if( (stream = fopen( "crt_fopen.c", "r" )) == NULL ) // C4996
// Note: fopen is deprecatedconsider using fopen_s instead
printf( "The file 'crt_fopen.c' was not opened\n" )
else
printf( "The file 'crt_fopen.c' was opened\n" )
// Open for write
if( (stream2 = fopen( "data2", "w+" )) == NULL ) // C4996
printf( "The file 'data2' was not opened\n" )
else
printf( "The file 'data2' was opened\n" )
// Close stream if it is not NULL
if( stream)
{
if ( fclose( stream ) )
{
printf( "The file 'crt_fopen.c' was not closed\n" )
}
}
// All other files are closed:
numclosed = _fcloseall( )
printf( "Number of files closed by _fcloseall: %u\n", numclosed )
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)