C语言将文件写入到堆内存中

我是黑体字
我是微软雅黑
我是华文彩云

color=#0099ff size=72 face=“黑体”
color=#00ffff
color=gray

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    char buf[1024];
    FILE *pf = fopen("11.txt","r+");
    if(pf == NULL)
        exit(-1);

    int line = 0;
    while(fgets(buf,1024,pf) != NULL)
        line++;
    rewind(pf);

    char **p = (char**)malloc(sizeof(char*)*(line+1));
    memset(p,0,sizeof(char*)*(line+1));

    char **sp = p;
    int len = 0;
    while(fgets(buf,1024,pf) != NULL)
    {
        len = strlen(buf);
        *sp = (char *)malloc(len+1);
        strcpy(*sp,buf);
        *sp++;
    }
    *sp = NULL;
    fclose(pf);
    sp = p;

    while(*sp)
    {
        printf("%s",*sp++);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值