标题:C语言读写文件(转)
取消只看楼主
lateraware
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2012-2-9
结帖率:66.67%
 问题点数:0 回复次数:1 
C语言读写文件(转)
// 读一个字符
#include "stdio.h"
#include "string.h"
main()
{
    FILE *fp;
    char c;
    if((fp=fopen("s.c","r"))==NULL)
    {
        printf("File can not open!");
        getch();
        exit(0);
    }
    while(!feof(fp))
    {
        c=fgetc(fp);
        putchar(c);
    }
    fclose(fp);
    getch();
    system("pause");
}

// 写一个字符
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
main()
{
    FILE *fp;
    char c;
    if((fp=fopen("d:\\data.txt","w"))==NULL)
    {
        printf("\nFile can not open!");
        getchar();
        exit(0);
    }
    while((c=getchar())!='#')
    {
        fputc(c,fp);
    }
    fclose(fp);
}

/*读写字符串*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
main(){
    FILE *fp;
    char c[80]={""},s[80]={""};
    /*写字符串*/
    if((fp=fopen("d:\\data.txt","w"))==NULL)
    {
        printf("\nFile can not open!");
        exit(0);
    }
    gets(c);
    fputs(c,fp);
    fclose(fp);
    /*读字符串*/
    if((fp=fopen("d:\\data.txt","r"))==NULL)
    {
        printf("\nFile can not open!");
        exit(0);
    }
    while(!feof(fp))
    {
        fgets(s,80,fp);
        puts(s);
    }
    fclose(fp);
    getchar();
}
搜索更多相关主题的帖子: C语言 system include 
2012-02-09 14:40
lateraware
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2012-2-9
得分:0 
回复 2楼 weipeng1217
新来的,不懂规矩,还望兄弟多多指点。我只是转一篇帖子,实用,论坛上是不是都只提问而不可以这样啊?
2012-02-09 16:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-360640-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.776408 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved