1. 文件编程小应用之修改程序的配置文件(407.10)
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <stdio.h>
# include <unistd.h>
# include <string.h>
# include <stdlib.h>
int main ( int argc, char * * argv) {
int fdSrc;
char * readBuf= NULL ;
if ( argc != 2 ) {
printf ( "pararm error\n" ) ;
exit ( - 1 ) ;
}
fdSrc = open ( argv[ 1 ] , O_RDWR) ;
int size = lseek ( fdSrc, 0 , SEEK_END ) ;
readBuf= ( char * ) malloc ( sizeof ( char ) * size + 8 ) ;
lseek ( fdSrc, 0 , SEEK_SET ) ;
int n_read = read ( fdSrc, readBuf, size) ;
char * p = strstr ( readBuf, "LENG=" ) ;
if ( p== NULL ) {
printf ( "not found\n" ) ;
exit ( - 1 ) ;
}
p = p+ strlen ( "LENG=" ) ;
* p = '5' ;
lseek ( fdSrc, 0 , SEEK_SET ) ;
int n_write = write ( fdSrc, readBuf, strlen ( readBuf) ) ;
close ( fdSrc) ;
return 0 ;
}
2. 写一个整数到文件(408.11)
* p = 5 ;
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <stdio.h>
# include <unistd.h>
# include <string.h>
# include <stdlib.h>
int main ( ) {
int fd;
int data = 100 ;
int data2;
fd = open ( "./file1" , O_RDWR) ;
int n_write = write ( fd, & data, sizeof ( int ) ) ;
lseek ( fd, 0 , SEEK_SET ) ;
int n_read = read ( fd, & data2, sizeof ( int ) ) ;
printf ( "read %d \n" , data2) ;
close ( fd) ;
return 0 ;
}
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <stdio.h>
# include <unistd.h>
# include <string.h>
# include <stdlib.h>
struct Test {
int a;
char c;
} ;
int main ( ) {
int fd;
struct Test data = { 100 , 'a' } ;
struct Test data2;
fd = open ( "./file1" , O_RDWR| O_TRUNK) ;
int n_write = write ( fd, & data, sizeof ( struct Test ) ) ;
lseek ( fd, 0 , SEEK_SET ) ;
int n_read = read ( fd, & data2, sizeof ( struct Test ) ) ;
printf ( "read %d,%c \n" , data2. a, data2. c) ;
close ( fd) ;
return 0 ;
}
3. 写结构体数组到文件(409.12)
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <stdio.h>
# include <unistd.h>
# include <string.h>
# include <stdlib.h>
struct Test
{
int a;
char c;
} ;
int main ( )
{
int fd;
struct Test data[ 2 ] = { { 100 , 'a' } , { 101 , 'b' } } ;
struct Test data2[ 2 ] ;
fd = open ( "./file1" , O_RDWR) ;
int n_write = write ( fd, & data, sizeof ( struct Test ) * 2 ) ;
lseek ( fd, 0 , SEEK_SET ) ;
int n_read = read ( fd, & data2, sizeof ( struct Test ) * 2 ) ;
printf ( "read %d,%c \n" , data2[ 0 ] . a, data2[ 0 ] . c) ;
printf ( "read %d,%c \n" , data2[ 1 ] . a, data2[ 1 ] . c) ;
close ( fd) ;
return 0 ;
}
4. 标准C库对文件操作引入(410.13)
总结open与fopen的区别(*面试会问)
https://www.cnblogs.com/NickyYe/p/5497659.html
5. 标准C库打开创建文件读写文件光标移动(411.14)
Linux下open与fopen的区别
https://www.cnblogs.com/hnrainll/archive/2011/09/16/2178706.html FILE/demo19.c
# include <stdio.h>
# include <string.h>
int main ( ) {
FILE * fp;
char * str = "cui jia qi." ;
char readBuf[ 128 ] = { 0 } ;
fp = fopen ( "./cui.txt" , "w+" ) ;
fwrite ( str, sizeof ( char ) , strlen ( str) , fp) ;
fseek ( fp, 0 , SEEK_SET ) ;
fread ( readBuf, sizeof ( char ) , strlen ( str) , fp) ;
printf ( "read data: %s\n" , readBuf) ;
fclose ( fp) ;
return 0 ;
}
6. 标准C库写入结构体到文件(412.15)
FILE/demo21.c(观察nwrite的值和第三个参数有关,而nread取决于真实字节大小)
# include <stdio.h>
# include <string.h>
int main ( )
{
FILE * fp;
char * str = "chenlichen hen shuai" ;
char readBuf[ 128 ] = { 0 } ;
fp = fopen ( "./chen.txt" , "w+" ) ;
int nwrite = fwrite ( str, sizeof ( char ) , 100 , fp) ;
fseek ( fp, 0 , SEEK_SET ) ;
int nread = fread ( readBuf, sizeof ( char ) * strlen ( str) , 100 , fp) ;
printf ( "read data: %s\n" , readBuf) ;
printf ( "read=%d,write = %d\n" , nread, nwrite) ;
return 0 ;
}
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <stdio.h>
# include <unistd.h>
# include <string.h>
# include <stdlib.h>
struct Test {
int a;
char c;
} ;
int main ( ) {
FILE * fp;
struct Test data = { 100 , 'a' } ;
struct Test data2;
fp = fopen ( "./file1" , "w+" ) ;
int n_write = fwrite ( & data, sizeof ( struct Test ) , 1 , fp) ;
fseek ( fp, 0 , SEEK_SET ) ;
int n_read = fread ( & data2, sizeof ( struct Test ) , 1 , fp) ;
printf ( "read %d,%c\n" , data2. a, data2. c) ;
fclose ( fp) ;
return 0 ;
}
7. 文件其它函数讲解及文件收尾(413.16)
C 库函数 - fgetc()
https://www.runoob.com/cprogramming/c-function-fgetc.html FILE/demo24.c
# include <stdio.h>
# include <string.h>
int main ( ) {
FILE * fp;
int i;
char * str = "chenlichen hen shuai o!" ;
int len = strlen ( str) ;
fp = fopen ( "./test.txt" , "w+" ) ;
for ( i= 0 ; i< len; i++ ) {
fputc ( * str, fp) ;
str++ ;
}
fclose ( fp) ;
return 0 ;
}
# include <stdio.h>
# include <string.h>
int main ( ) {
FILE * fp;
int i;
char c;
fp = fopen ( "./test.txt" , "r" ) ;
while ( ! feof ( fp) ) {
c = fgetc ( fp) ;
printf ( "%c" , c) ;
}
fclose ( fp) ;
return 0 ;
}