c 文件操作(例子)

#include <stdio.h>
#include 
<iostream>
#include 
<cstring>

int main(int argc, char *argv[])
{
    
char arr[10] ;
    
char *ap = "hello!" ;
    FILE 
*fp ;
    
if ((fp = fopen("hello.txt""wt+")) == NULL)
    
{
        printf(
"error!") ;
        exit(
1) ;
    }

    fputs(ap, fp) ;
    
    rewind(fp) ; 
    
    fgets(arr, strlen(ap) 
+ 1, fp) ;
    
    
    printf(
"%s\n", arr) ;
    fclose(fp) ;
    
return 0 ;
}


#include <stdio.h>
#include 
<iostream>
#include 
<cstring>

int main(int argc, char *argv[])
{
    
char arr[10] ;
    
char *ap = "hello!" ;
    FILE 
*fp ;
    
if ((fp = fopen("hello.txt""wt+")) == NULL)
    
{
        printf(
"error!") ;
        exit(
1) ;
    }

    
    fwrite(ap, strlen(ap), 
1, fp) ;
    
    rewind(fp) ; 
    
    fread(arr, strlen(ap), 
1, fp) ;
    arr[strlen(ap)] 
= '\0' ;

    printf(
"%s\n", arr) ;
    fclose(fp) ;
    
return 0 ;
}
posted @ 2007-04-24 01:10  Edward Xie  阅读(270)  评论(0)    收藏  举报