阅读文字和创建数组C ++ [英] Reading in characters and creating array c++

查看:132
本文介绍了阅读文字和创建数组C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人会如何去从一个文件中的行字符阅读。第一方案中从文件的整数读取。该数字表示多少个字符读取在下一step.Next读取的字符,并将其存储在数组中。那么,如何打造字符的变量,这样我可以从迈克尔字符正确读取到一个数组显示它们。

How would one go about reading in a line of characters from a file. First the program reads in an integer from the file. That number indicates how many characters to read in in the next step.Next reads the characters in and store them in an array. So how do i create the 'char' variable so that i can correctly read in the characters from Michael to displaying them in an array.

file.txt: 
8 
Michael

即时通讯使用INPUTFILE >>整数,从那里我需要一个整数使用,使这个阵列麦克风的char [整数] ;,然后我可以在读取字符到数组

im using inputFile >> integer, from there i need that integer to use to make this array char mike[integer];, then i can read in the chars to the array

推荐答案

要回答你的问题:

#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;
}

不过,我会去(如果你的迈克尔出现在自己的行):

#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream f("file.txt");
    int n;
    f >> n;
    string str;
    getline(f, str);
}

这篇关于阅读文字和创建数组C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆