读取字符并创建数组c++
Reading in characters and creating array c++
如何从文件中读取一行字符。首先,程序从文件中读取一个整数。该数字表示下一步要读入的字符数。接下来读取中的字符并将它们存储在一个数组中。那么,我该如何创建"char"变量,以便正确读取Michael的字符,并将其显示在数组中呢。
file.txt:
8
Michael
im使用inputFile>>integer,从那里我需要这个整数来制作这个数组char-mike[integrat];,然后我可以读取数组的字符
回答您的问题:
#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);
}
#include <fstream.h>
#include <string.h>
int main()
{
ifstream f("file.txt",ios::in);
int n;
f >> n;
char string[n];
f.getline(string,n);
cout<<string;
}
这将输出file.txt
中的以下字符串。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '