当前位置: 代码迷 >> C语言 >> 实现把文件中内容读取到一个字符串中?
  详细解决方案

实现把文件中内容读取到一个字符串中?

热度:201   发布时间:2008-04-09 11:14:08.0
实现把文件中内容读取到一个字符串中?
下面这样写运行有误(文件中的内容很短,小于100个字符时也运行异常),并且其长度事先定义为100也不合理,要怎么做比较合理呢?
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

void readdat(char *s,char *t)
{
    FILE *fp;
    char str[100];
    int i;
    fp=fopen(s,t);
    if(fp=NULL)
    {
        printf("can't open the file");
        exit(1);
    }
    i=0;
    while(!feof(fp))
    {
        *(str+i)=fgetc(fp);
            i++;
    }
    printf("%s",str);
}
void main()
{
    readdat("in.txt","r");
}
搜索更多相关的解决方案: 字符  文件  

----------------解决方案--------------------------------------------------------
看了下fp==NULL. 其它没怎么看
----------------解决方案--------------------------------------------------------
等于之过错。
----------------解决方案--------------------------------------------------------
  相关解决方案