#include <string>
#include <stdio.h>
//从整个字符串中按行读取内容
//rst 为要按行读取的文本内容,如整个html文件内容
int GetLine(std::string rst = "")
{
int buffLen = 4096;
char line[4096 + 1]; //预留一位保留截断符号0
char *pLine = line;
int i = 0;
while (rst[i] != 0)
{
int j = 0;
while (j < buffLen) //getline
{
char temp = rst[i];
i++;
if (temp)
{
*(pLine + j) = temp;
j++;
if (temp == 10) //\n 换行标识符号
break;
}
}
*(pLine + j) = 0;
printf(pLine);
}
}
//按行读取文件内容
//filePath 文件路径
void GetLine(std::string filePath = "")
{
FILE *fRead;
fRead = fopen(filePath.c_str(), "r");
if (fRead)
{
int buffLen = 4096;
char line[4096 + 1];
char *pLine = line;
while (!feof(fRead))
{
int i = 0;
while (!feof(fRead) && i < buffLen) //getline
{
char temp = fgetc(fRead);
if (temp)
{
*(pLine+i) = temp;
i++;
if (temp == 10) //\n 换行标识符号
break;
}
}
*(pLine + i) = 0;
printf(pLine);
}
fclose(fRead);
}
}
要点:1. if (temp == 10) //\n 换行标识符号
以此来判断是否换行了