如何从文件中读取一行字符。首先,程序从文件中读取一个整数。该数字表示下一步要读入多少个字符。下一步读取字符并将它们存储在数组中。那么我如何创建“char”变量,以便我可以正确读取 Michael 的字符并将它们显示在数组中。
file.txt:
8
Michael
我使用 inputFile >> 整数,从那里我需要使用该整数来使该数组 char mike[integer];,然后我可以将字符读入数组
回答你的问题:
#include <fstream>
using namespace std;
int main() {
ifstream f("file.txt");
int n;
f >> n;
char chs = new char[n];
for (int i = 0; i < n; ++i) f >> chs[i];
// do something about chs
delete [] chs;
}
但是,我会选择(如果你的Michael
出现在自己的行上):
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream f("file.txt");
int n;
f >> n;
string str;
getline(f, str);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)