c读取txt文件数据(C语言文件操作,要读取一个txt文件内容,应该怎么做)

本文目录
C语言文件操作,要读取一个txt文件内容,应该怎么做
//data.txt文件内容如下\x0d\x0a\x0d\x0a1个猪\x0d\x0a2个猪\x0d\x0a3个猪\x0d\x0a4个猪\x0d\x0a5个猪\x0d\x0a6个猪\x0d\x0a7个猪\x0d\x0a8个猪\x0d\x0a\x0d\x0a//运行结果一\x0d\x0athe 8 line :8 个 猪\x0d\x0a\x0d\x0aPress any key to continue \x0d\x0a//运行结果二\x0d\x0aout of range!\x0d\x0aPress any key to continue \x0d\x0a\x0d\x0a//代码如下\x0d\x0a#include
VC++怎么读写TXT文件
有以下几种常用方法读写TXT文件:
一、
CStdioFile
二、
FILE* f = fopen("file name", "mode");
char buff;
fread(buff, size, 1, f);
fclose(f);
三、
//用MFC读文件
CFile file("yourfile.txt",CFile::modeRead);
char *pBuf;
int iLen=file.GetLength();
pBuf =new char;
file.Read(pBuf,iLen);
pBuf=0;
file.Close();
MessageBox(pBuf);
四、
//用C SDK 读文件
HANDLE hFile;
hFile=CreateFile("2.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
char ch;
DWORD dwReads;
ReadFile(hFile,ch,100,&dwReads,NULL);
CloseHandle(hFile);
ch=0;
MessageBox(ch);*
五、
用C读文件
FILE *pFile=fopen("1.txt","rb");
char *pBuf;
fseek(pFile,0,SEEK_END);//移动文件指针到文件末尾
int len=ftell(pFile);//获取当前文件指针在文件中的偏移量,Gets the current position of a file pointer.offset
pBuf=new char;
rewind(pFile);//将指针移动到文件头,Repositions the file pointer to the beginning of a file
//也可以用fseek(pFile,0,SEEK_SET);
fread(pBuf,1,len,pFile);
pBuf=0;
fclose(pFile);
MessageBox(pBuf);
C++编程,从TXT文档中读取数字
#include 《iostream》
#include 《string》
#include 《vector》
#include《fstream》
#include 《sstream》
using namespace std;
// 判断一个字符串是否为一个数字,如果是返回它
bool JudgeNum(string str,int& iTmp)
{
bool bNum = true;
string::size_type szSize = str.size();
for (int i=0;i《szSize;++i)
{
char ch = str.at(i);
if ((ch 《 ’0’) || (ch 》 ’9’))
{
bNum = false;
break;
}
}
if (bNum)
{
istringstream iss(str);
iss 》》 iTmp;
}
return bNum;
}
int main()
{
ifstream infile("F:\\save.txt");
int a,b,c,d;
vector《int》 iVec;
string strTmp;
int iTmp = 0;
if (!infile)
{
return -1;
}
while(getline(infile,strTmp,’ ’)) // 以空格为分隔符,读取每一个词
{
if (JudgeNum(strTmp,iTmp))
{
iVec.push_back(iTmp);
}
}
vector《int》::size_type stCnt = iVec.size();
if (stCnt 》= 4)
{
a = iVec.at(0);
b = iVec.at(1);
c = iVec.at(2);
d = iVec.at(3);
cout 《《 a 《《 " " 《《 b 《《 " " 《《 c 《《 " " 《《 d;
}
getchar();
return 1;
}
这个可以获取到文本中任意的数字,但文本里的数字与其他字符之间必须有空格分隔,仅供参考!
其他情况,LZ可自行参考修改。这里只提供个思路.
C语言如何读取TXT文件并存入数组中
一、编程思路。
1 以文本方式打开文件。
2 循环用fscanf格式化输入数据到数组。
3 判断fscanf的返回值,如果显示到达文件结尾,退出输入。
4 关闭文件。
5 使用数据。
二、代码实现。
设定文件名为in.txt, 存有一系列整型数据,以空格或换行分隔。
代码可以写作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include 《stdio.h》
int main()
{
int v;//开一个足够大的数组。
int i = 0, j;
FILE *fp;//文件指针
fp = fopen("in.txt", "r");//以文本方式打开文件。
if(fp == NULL) //打开文件出错。
return -1;
while(fscanf(fp, "%d", &v) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
i++;
fclose(fp);//关闭文件
for(j = 0; j 《 i; j ++)//循环输出数组元素。
{
printf("%d ", v);
}
return 0;
}
当文件内容为:
1 35 6 8 9 9
10 123 34
76 54 98
程序输出:
1 35 6 8 9 9 10 123 34 76 54 98

更多文章:
全球新冠肺炎疫情背景下航运发展(盐田港复苏日志:半年历劫从“低谷”到“爆仓” 疫情之后巨轮如何越洋航行)
2026年9月7日 17:10
matlab求解带字母参数方程组(我想matlab求一个关于x,y的方程组 ab c d f e h m n 都是参数)
2026年9月7日 16:30
oracle中的循环语句(下面哪个不是oracle程序设计中的循环语句 a for)
2026年9月7日 15:30
电脑里2个系统怎么删除一个(电脑开机显示有两个系统,如何删除一个)
2026年9月7日 12:20
scrollthrough意思(“scroll”是什么意思)
2026年9月7日 08:00
怎么激活keygen(注册机如何激活cad2008一个简单激活cad2008的方法)
2026年9月7日 06:30
vba编写excel插件(excel vba中能否动态创建控件)
2026年9月7日 04:40






