几个能用的C语言IO读写数据的例子

int fgetc(FILE *stream)
int fputc(int ch,FILE *stream)
fgetc( )函数从输入流的当前位置返回一个字符,并将文件指针指示器移到下一个字符处,如果已到文件尾,函数返回EOF,此时表示本次操作结束,若读写文件完成,则应关闭文件。
fputc()函数完成将字符c h的值写入所指定的流文件的当前位置处,并将文件指针后移一位。fputc()函数的返回值是所写入字符的值,出错时返回EOF。

int fscanf(FILE *stream,char *format,arg_list)
int fprintf(FILE *stream,char *format,arg_list)
对文件的格式化读写就是在上述函数的前面加一个字母f成为fscanf( )和fprintf( ) 
其中,stream为流文件指针,其余两个参数与scanf( )和printf( )用法完全相同。

char *fgets(char *str,int num,FILE *stream) // 感觉是在读一行数据
int fputs(char *str,FILE *stream)
fgets() 函数从流文件stream中读取至多num-1个字符,并把它们放入str指向的字符数组中。读取字符直到遇见回车符或EOF(文件结束符)为止,或读入了所限定的字符数。
fputs( )函数将str指向的字符串写入流文件。操作成功时,函数返回0值,失败返回非零值。 

int fread(void *buf,int size,int count,FILE *stream)
int fwrite(void *buf,int size,int count,FILE *stream)
fread()函数从stream 指向的流文件读取count (字段数)个字段,每个字段为size(字段长度)个字符长,并把它们放到b u f(缓冲区)指向的字符数组中。
fread()函数返回实际已读取的字段数。若函数调用时要求读取的字段数超过文件存放的字段数,则出错或已到文件尾,实际在操作时应注意检测。
fwrite( )函数从buf(缓冲区)指向的字符数组中,把count(字段数)个字段写到stream所指向的流中,每个字段为size个字符长,函数操作成功时返回所写字段数。
关于成块的文件读写,在创建文件时只能以二进制文件格式创建。

 

通常,对于输入数据的格式较为复杂的话,我们可采取将各种格式的数据当做字符串输入,然后将字符串转换为所需的格式。C提供函数:
int atoi(char *ptr)
float atof(char *ptr)
long int atol(char *ptr)
它们分别将字符串转换为整型、实型和长整型。使用时请将其包含的头文件math.h或stdlib.h写在程序的前面。

 

#include "stdafx.h"
#include <stdio.h>

// 将存放于磁盘的指定文本文件按读写字符方式逐个地从文件读出,然后再将其显示到屏幕上。
int main(int argc, char* argv[])
{
char ch;
FILE *fp;
int i;

if((fp=fopen(argv[1],"r"))==NULL) /* 打开一个由argv[1]所指的文件*/
{
printf("not open");
return 0;
}

while ((ch=fgetc(fp))!=EOF) /* 从文件读一字符,显示到屏幕*/ {
putchar(ch); // 如果同时打印 printf("(%d)",ch); putchar就不能正确显示中文了
}

fclose(fp);
printf("Hello World!\n");
return 0;
}
#include "stdafx.h"
#include <stdio.h>
#include <string.h>

// 向磁盘写入字符串,并写入文本文件test.txt:
int main(int argc, char* argv[])
{
FILE *fp;
char str[128];

if((fp=fopen("test.txt","w"))==NULL)
{
printf("not open");
return 0;
}

while((strlen(gets(str)))!=0) /*若串长度为零,则结束*/
{
fputs(str,fp); /*写入串*/
fputs("\n",fp); /*写入回车符*/
}

fclose(fp);
printf("Hello World!\n");
return 0;
}
#include "stdafx.h"
#include <stdio.h>

// 将一些格式化的数据写入文本文件,再从该文件中以格式化方法读出显示到屏幕上,
// 其格式化数据是两个学生记录,包括姓名、学号、两科成绩。
int main(int argc, char* argv[])
{
FILE *fp;
int i;
struct stu{ /*定义结构体类型*/
char name[15];
char num[6];
float score[2];
} student; /*说明结构体变量*/

if((fp=fopen("test.txt","w"))==NULL) /*以文本只写方式打开文件*/
{
printf("cannot open file");
return 0;
}
printf("input data:\n");
for( i=0;i<2;i++)
{
scanf("%s %s %f %f",student.name, student.num,
&student.score[0],
&student.score[1]); /*从键盘输入*/
fprintf(fp,"%s %s %7.2f %7.2f\n", student.name, student.num,
student.score[0],
student.score[1]); /* 写入文件*/
}
fclose(fp); /*关闭文件*/

if((fp=fopen("test.txt","r"))==NULL) /*以文本只读方式重新打开文件*/
{
printf("cannot open file");
return 0;
}

printf("output from file:\n");
// fscanf 扫描的特点是,只按固定格式扫描,不管换行不换行。如果一行只扫描了一半,后面的数据下面接着扫描
while (fscanf(fp,"%s %s %f %f\n", student.name, student.num, &student.score[0], &student.score[1])!=EOF) /*从文件读入*/ {
printf("%s %s %7.2f %7.2f\n", student.name, student.num, student.score[0], student.score[1]); /* 显示到屏幕*/
}

fclose(fp); /*关闭文件*/

printf("Hello World!\n");
return 0;
}

 

#include "stdafx.h"

int main(int argc, char* argv[])
{
FILE *fp1;
int i;
struct stu{ /*定义结构体*/
char name[15];
char num[6];
float score[2];
} student;

if((fp1=fopen("test.txt","wb"))==NULL) /*以二进制只写方式打开文件*/
{
printf("cannot open file");
return 0;
}
printf("input data:\n");
for( i=0;i<2;i++)
{
scanf("%s %s %f %f",student.name,student.num,&student.score[0],&student.score[1]); /* 输入一记录*/
fwrite(&student, sizeof(student), 1, fp1); /* 成块写入文件*/
}
fclose(fp1);

if((fp1=fopen("test.txt","rb"))==NULL) /*重新以二进制只写打开文件*/
{
printf("cannot open file");
return 0;
}
printf("output from file:\n");
for (i=0;i<2;i++)
{
fread(&student,sizeof(student),1,fp1); /* 从文件成块读*/
printf("%s %s %7.2f %7.2f\n", student.name, student.num, student.score[0], student.score[1]); /* 显示到屏幕*/
}
fclose(fp1);

printf("Hello World!\n");
return 0;
}


还有一个例子死活不行,请各位指教:

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

// 向磁盘写入字符串,并写入文本文件test.txt:
int main(int argc, char* argv[])
{
FILE *fp1,*fp2;
char str[128];
if ((fp1=fopen("test1.txt","r"))==NULL) /* 以只读方式打开文件1 */
{
printf("cannot open file\n");
return 0;
}

if((fp2=fopen("test2.txt","w"))==NULL) /*以只写方式打开文件2 */
{
printf("cannot open file\n");
return 0;
}

while ((strlen(fgets(str,128,fp1)))>0)
{
fputs(str,fp2); /* 从文件1读字符串并写入文件2 */
printf("%s",str); /*在屏幕显示*/
}
fclose(fp1);
fclose(fp2);
}





posted @ 2012-04-01 22:33  findumars  Views(5092)  Comments(0Edit  收藏  举报