C++ 函数 `getline()` 不能为 char[] 正常工作
C++ function `getline()` doesn't work correctly for char[]
我需要从一行中提取前 150 个字符并将它们保存在 char[] 数组中(不允许使用字符串)。我下面的代码不起作用,我就是找不到原因:
#include <ifstream>
#include <iostream>
using namespace std;
int main()
{
ifstream myFile;
myFile.open("message.txt");
if(!myFile.is_open()) cout<<"error"; //added this after edit
const int SIZE = 151;
char buffer[SIZE]={};
while(myFile.getline(buffer, 151)){
buffer[150]='[=10=]';
cout<<buffer<<endl;
}
myFile.close();
}
这是“message.txt”的片段:
abcdefg
hijklmn
opqrstuv
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
hello
它只是什么都不打印。文件“message.txt”存在并且里面有几行字符。我哪里错了?
编辑:如果一行中的字符少于 150,则应全部读取。如果超过150,则忽略其余部分。
如果您尝试阅读第一行的前 150 个字符,则不需要 while
循环。而且您不需要手动终止 buffer
,istream::getline()
会为您完成,例如:
#include <ifstream>
#include <iostream>
using namespace std;
int main()
{
ifstream myFile("message.txt");
if (!myFile.is_open())
{
cout << "error";
return 0;
}
const int SIZE = 151;
char buffer[SIZE] = {};
myFile.getline(buffer, SIZE);
cout << buffer << endl;
myFile.close();
return 0;
}
如果您只想读取特定行的前 150 个字符,那么您需要一个循环来跳过所有行而不管它们的长度直到你想要的那一行,然后你就可以读到那一行的150个字符,例如:
#include <ifstream>
#include <iostream>
#include <limits>
using namespace std;
int main()
{
ifstream myFile("message.txt");
if (!myFile.is_open())
{
cout << "error";
return 0;
}
size_t lineIndex = ...;
while (lineIndex > 0)
{
if (!myFile.ignore(numeric_limits<streamsize>::max(), '\n'))
{
cout << "error";
return 0;
}
if (myFile.eof())
{
cout << "eof";
return 0;
}
--lineIndex;
}
const int SIZE = 151;
char buffer[SIZE] = {};
myFile.getline(buffer, SIZE);
cout << buffer << endl;
myFile.close();
return 0;
}
如果您想阅读每行的前 150 个字符,那么在成功阅读后您需要跳过换行符之前的所有剩余字符,然后才能阅读下一行,例如:
#include <ifstream>
#include <iostream>
#include <limits>
using namespace std;
int main()
{
ifstream myFile("message.txt");
if (!myFile.is_open())
{
cout << "error";
return 0;
}
const int SIZE = 151;
char buffer[SIZE] = {};
do
{
myFile.getline(buffer, SIZE);
if (myFile.bad())
{
cout << "error";
return 0;
}
if (myFile.fail())
{
// SIZE-1 characters were extracted before a line break
// was reached, need to reset the error to keep going...
myFile.clear();
myFile.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << buffer << endl;
}
while (!myFile.eof());
myFile.close();
return 0;
}
我需要从一行中提取前 150 个字符并将它们保存在 char[] 数组中(不允许使用字符串)。我下面的代码不起作用,我就是找不到原因:
#include <ifstream>
#include <iostream>
using namespace std;
int main()
{
ifstream myFile;
myFile.open("message.txt");
if(!myFile.is_open()) cout<<"error"; //added this after edit
const int SIZE = 151;
char buffer[SIZE]={};
while(myFile.getline(buffer, 151)){
buffer[150]='[=10=]';
cout<<buffer<<endl;
}
myFile.close();
}
这是“message.txt”的片段:
abcdefg
hijklmn
opqrstuv
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
hello
它只是什么都不打印。文件“message.txt”存在并且里面有几行字符。我哪里错了? 编辑:如果一行中的字符少于 150,则应全部读取。如果超过150,则忽略其余部分。
如果您尝试阅读第一行的前 150 个字符,则不需要 while
循环。而且您不需要手动终止 buffer
,istream::getline()
会为您完成,例如:
#include <ifstream>
#include <iostream>
using namespace std;
int main()
{
ifstream myFile("message.txt");
if (!myFile.is_open())
{
cout << "error";
return 0;
}
const int SIZE = 151;
char buffer[SIZE] = {};
myFile.getline(buffer, SIZE);
cout << buffer << endl;
myFile.close();
return 0;
}
如果您只想读取特定行的前 150 个字符,那么您需要一个循环来跳过所有行而不管它们的长度直到你想要的那一行,然后你就可以读到那一行的150个字符,例如:
#include <ifstream>
#include <iostream>
#include <limits>
using namespace std;
int main()
{
ifstream myFile("message.txt");
if (!myFile.is_open())
{
cout << "error";
return 0;
}
size_t lineIndex = ...;
while (lineIndex > 0)
{
if (!myFile.ignore(numeric_limits<streamsize>::max(), '\n'))
{
cout << "error";
return 0;
}
if (myFile.eof())
{
cout << "eof";
return 0;
}
--lineIndex;
}
const int SIZE = 151;
char buffer[SIZE] = {};
myFile.getline(buffer, SIZE);
cout << buffer << endl;
myFile.close();
return 0;
}
如果您想阅读每行的前 150 个字符,那么在成功阅读后您需要跳过换行符之前的所有剩余字符,然后才能阅读下一行,例如:
#include <ifstream>
#include <iostream>
#include <limits>
using namespace std;
int main()
{
ifstream myFile("message.txt");
if (!myFile.is_open())
{
cout << "error";
return 0;
}
const int SIZE = 151;
char buffer[SIZE] = {};
do
{
myFile.getline(buffer, SIZE);
if (myFile.bad())
{
cout << "error";
return 0;
}
if (myFile.fail())
{
// SIZE-1 characters were extracted before a line break
// was reached, need to reset the error to keep going...
myFile.clear();
myFile.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << buffer << endl;
}
while (!myFile.eof());
myFile.close();
return 0;
}