65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
int count(FILE *fp)
{
int sum_ = 0;
char ch = fgetc(fp);
while (ch != EOF)
{
ch = fgetc(fp);
sum_++;
}
cout << "该文件中的字节总数为:" << sum_ << endl;
return sum_;
}
int main()
{
FILE *fp = fopen("update.txt", "r");
if(fp == NULL)
{
cout << "OPEN ERROR" << endl;
return 1;
}
//char buf[100] = "";
char *pBuf = NULL;
char *pLookFor;
int location = 1;
int sum = count(fp);
//pBuf = new char(sum + 1);
pBuf = new char[sum + 1];
rewind(fp);
fread(pBuf, sum, 1, fp);
cout << pBuf << endl;
for (pLookFor = pBuf; pLookFor < pBuf + sum; pLookFor++)
{
if (*pLookFor == 10)
{
cout << "回车换行符的位置为:" << location << endl;
}
location++;
}
return 0;
}