社区
C语言
帖子详情
如何将一个文本文件的所有内容读出来?
chido
2002-11-05 09:12:29
如何将一个文本文件的所有内容读出来,放到一个char型数组或者指针里?
试了好几个函数,都是到空格或者回车就会终止.
...全文
87
6
打赏
收藏
如何将一个文本文件的所有内容读出来?
如何将一个文本文件的所有内容读出来,放到一个char型数组或者指针里? 试了好几个函数,都是到空格或者回车就会终止.
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
topikachu
2002-11-05
打赏
举报
回复
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
string read_file_into_string(const string&);
int main()
{
string strFile;
cout<<"pls input a filename with path"<<endl;
cin>>strFile;
string FileContext=read_file_into_string(strFile);
cout<<FileContext<<endl;
}
string read_file_into_string(const string& strFile)
{
ifstream ifile(strFile.c_str());
ostringstream buf;
char ch;
while (buf && ifile.get(ch))
buf.put(ch);
return buf.str();
}
用的是标准库,不用考虑delete,不用考虑平台依赖性,不用担心缓冲是否不够:)
wzt2000
2002-11-05
打赏
举报
回复
char buffer[8096];
char *str=new char[256];
memset(buffer,0,8096);
while(!feof(fp))
{
if(fgets(str,256,fp)==NULL)
break;
strcat(buffer,str);
}
delete []str;
gucs
2002-11-05
打赏
举报
回复
gz
GZCompiler
2002-11-05
打赏
举报
回复
用fread()函数。
zxm954712
2002-11-05
打赏
举报
回复
#include "stdio.h"
#include "stdlib.h"
#include "memory.h"
#include "string.h"
void main()
{
FILE *fp;
char *p;
char caBuf[256];
char caTemp[8092];
fp = fopen("a.txt", "r");
if (fp == NULL) {
printf("open file error\n");
exit(1);
}
memset(caBuf, 0x00, sizeof(caBuf));
memset(caTemp, 0x00, sizeof(caTemp));
p = fgets(caBuf, 256, fp);
if (p != NULL) {
memcpy(caTemp, caBuf, strlen(caBuf));
}
memset(caBuf, 0x00, sizeof(caBuf));
p = fgets(caBuf, 256, fp);
while(p != NULL) {
strcat(caTemp, caBuf);
memset(caBuf, 0x00, sizeof(caBuf));
p = fgets(caBuf, 256, fp);
}
caTemp[strlen(caTemp)] = 0x00;
printf("caTemp is %s", caTemp);
fclose(fp);
}
black_snail
2002-11-05
打赏
举报
回复
#include <stdio.h>
#define MAX 100000;
char cContent[MAX];
int main(void)
{
FILE *stream;
int i=0;
/* open a file for reading */
stream = fopen("DUMMY.FIL", "r");
while (!feof(stream))
{
/* read a character from the file */
cContent[i]=fgetc(stream);
i++;
}
/* close the file */
fclose(stream);
return 0;
}
编写
一个
程序,将
一个
文本文件
的
内容
按行读出,每读出一行就顺序加上行号,并写入 到另
一个
文件中。
实验九 Java 语言的输入输出与文件处理 实验目的 1.了解流式输入输出的基本原理。 2.掌握 File、FileInputStream、FileOutputStream 类的使用方法。 3.掌握 FileReader、FileWriter、BufferedReader 类的使用方法。 主要仪器设备及耗材 安装了 JDK1.8 的 PC 一台 实验
内容
2. 编写
一个
程序,将
一个
文本文件
的
内容
按行读出,每读出一行就顺序加上行号,并写入 到另
一个
文件中。 package com.temp; i...
使用随机文件流类RandomAccessFile将
一个
文本文件
倒置读出
在练习使用随机文件流类RandomAccessFile将
一个
文本文件
倒置读出
一个
.txt文件时遇到两个问题,特此记录一下 1.读取utf-8编码格式的文件是,前三位会有特殊字符占用。 2.GBK编号
一个
中文字符占用2个字节,UTF-8编码
一个
中文字符占用3个字节 //不同编码格式占用字节长度 System.out.println("中".getBytes("UTF-8").length);...
C++读取
文本文件
所有
内容
用C++代码,如何一次读取文件所有
内容
? 代码示例如下: #include <fstream> #include <string> int main(int argc, char* argv[]) { std::ifstream inputStream(argv[2]); const std::string text((std::istreambuf_iterator<char>(inputStream)), (std::istreambuf_itera
C++
文本文件
读写操作详解
C++
文本文件
读写操作详解 前面章节中,已经给大家介绍了文件流对象如何调用 open() 方法打开文件,并且在读写(又称 I/O )文件操作结束后,应调用 close() 方法关闭先前打开的文件。那么,如何实现对文件
内容
的读写呢?接下来就对此问题做详细的讲解。 在讲解具体读写文件的方法之前,读者首先要搞清楚的是,对文件的读/写操作又可以细分为 2 类,分别是以文本形式读写文件和以二进制形式读写文件。 我们知道,文件中存储的数据并没有类型上的分别,统统都是字符。所谓以文本形式读/写文件,就是直白地将文件中
Python实现
文本文件
拆分写入到多个
文本文件
引言 将
一个
txt
文本文件
中的
内容
行拆分固定的行数,自动分批写入到多个
文本文件
。 比如:
一个
源txt文件有5100行数据,每1000行插入到
一个
txt文件,最后获得6个txt文件(5个
文本文件
有1000行数据,第6个
文本文件
有100行数据)。 步骤 1、先建立
一个
目录用于存放分割后的txt文件(这里的目录名为:dataText) 2、修改拆分的数目(这里是每5000行数据存入
一个
txt文件) 3、运行python文件,查看生成的txt文件 代码 open_diff = open('data.txt',
C语言
70,024
社区成员
243,263
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章