标签:
收藏
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include<stdio.h> #include<string.h> int main( void ) { FILE *stream; char msg[]= "this is a test" ; char buf[20]; if ((stream= fopen ( "DUMMY.FIL" , "w+" ))==NULL) { fprintf (stderr, "Can not open output file.\n" ); return 0; } /*write some data to the file*/ fwrite (msg,1, strlen (msg)+1,stream); /*sizeof(char)=1 seek to the beginning of the file*/ fseek (stream,0,SEEK_SET); /*read the data and display it*/ fread (buf,1, strlen (msg)+1,stream); printf ( "%s\n" ,buf); fclose (stream); return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#include<stdio.h> int main( void ) { FILE *stream; charlist[30]; inti,numread,numwritten; /*Open file in text mode:*/ if ((stream= fopen ( "fread.out" , "w+t" ))!=NULL) { for (i=0;i<25;i++) list[i]=( char )( ‘z‘ -i); /*Write 25 characters to stream*/ numwritten= fwrite (list, sizeof ( char ),25,stream); printf ( "Wrote %d items\n" ,numwritten); fclose (stream); } else printf ( "Problem opening the file\n" ); if ((stream= fopen ( "fread.out" , "r+t" ))!=NULL) { /*Attempt to read in 25 characters*/ numread= fread (list, sizeof ( char ),25,stream); printf ( "Number of items read=%d\n" ,numread); printf ( "Contents of buffer=%.25s\n" ,list); fclose (stream); } else printf ( "File could not be opened\n" ); } |
1
2
3
4
5
6
7
8
|
<?php $handle = fopen ( "test.txt" , "rb" ); $contents = "" ; while (! feof ( $handle )){ $contents .= fread ( $handle ,8192); } fclose( $handle ); ?> |
标签:
原文地址:http://www.cnblogs.com/AAA-li/p/5891858.html