C++ I';我一直在研究如何读取文本文件

C++ I';我一直在研究如何读取文本文件,c++,C++,/*在文本文件中,我有一个字符,后跟一个空格,然后是一个字符串。我试图将字符和字符串读入单独的数组。谢谢你的帮助*/ #include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; int main() { char arrivOrDepart; string licensePlt; ifstream inFile

/*在文本文件中,我有一个字符,后跟一个空格,然后是一个字符串。我试图将字符和字符串读入单独的数组。谢谢你的帮助*/

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
 char arrivOrDepart;
 string licensePlt;
 ifstream inFile;
 inFile.open("Text.txt");
 if (!inFile)
 {
  cout << "Can't open file" << endl;
  return 1;
 }
 for (int i = 0; i < 4; i++)
 {
  getline(cin, arrivOrDepart[i]);
  getline(cin, licensePlt[i]);
 }

 inFile.close();
 cin.get();
 return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
char Arrivor离开;
字符串许可证;
河流充填;
infle.open(“Text.txt”);
如果(!infle)
{
不能包含
#包括
#包括
这将从文件读入向量

std::ifstream input("d:\\testinput.txt");

std::vector<std::string> bytes(
     (std::istreambuf_iterator<std::string>(input)),
     (std::istreambuf_iterator<std::string>()));

input.close();
std::ifstream输入(“d:\\testinput.txt”);
向量字节(
(std::istreambuf_迭代器(输入)),
(std::istreambuf_迭代器());
input.close();

然后,只需将数据放入您想要的任何容器中。您应该总是喜欢向量而不是数组。顺便说一句,代码有一些问题:

  • getline
    是错误的工具。如果要基于空格分割流,请使用
  • arr
    licensePlt
    未定义为数组,而是用作数组
  • 从cin中读取,而不是从文件中读取
  • 我建议的修复(不包括使用向量而不是数组):

    #包括
    #包括
    #包括
    #包括
    使用命名空间std;//避免使用此
    int main()
    {
    const int MAXARRAY=4;//避免使用幻数
    char arrivordevice[MAXARRAY];//创建了一个数组,但更喜欢std::vector
    字符串licensePlt[MAXARRAY];//创建了一个数组
    河流充填;
    infle.open(“Text.txt”);
    如果(!infle)
    {
    cout temp>>licensePlt[i]&&//从文件流读取数据
    temp.length()==1)//仅为数组读取一个字符
    {
    arrivor=temp[0];
    i++;
    }
    infle.close();
    cin.get();
    返回0;
    }
    
    建议阅读:

    (候补)


    请注意用于设置解析定界符的第三个参数。

    这听起来很傻,但是将字符作为
    字符串来读取会更容易。
    。然后您可以
    而(cin>>arrivordevide[i]>>licensePlt[i]){i++:}
    也就是说,如果你想得到一个漂亮的结构,你可以将
    许可证plt
    聚合到
    结构中,这样你只需要处理该
    结构的一个数组。作为额外的好处,你可以为该
    结构编写一个
    操作符>
    重载,并简化为
    (cin>>mystructarray[i]){i++:}我需要两个数组来完成其他任务。不能只拥有一个字符串。我一直在用GETLIN获取错误,然后可以再向前走一步,使用<代码> STD::vector < /代码>代替一个数组。这将消除文件中最多4个输入。我真正的问题是如何将两个数组都读入单独的数组,而不包括空白SP。ace.我必须动态设置这些数组,但无法静态设置如果询问者需要
    向量
    字符串
    ,那就太好了。遗憾的是,他们没有。谢谢你的帮助,但我需要两个数组。一个字符和另一个字符串。嗯,他可能应该使用数组上的向量。阅读后将向量拆分为两个数组从文件中,我将尝试使用skeller。感谢您只需使用tweek语句即可(inti=0;I>arriveOrDepart[I]>>licensePlt[I];}@dk019,其中有错误。没有检查
    infle>>arriveOrDepart[I]>>licensePlt[I]
    以确保读取成功。您可能会从文件中读取垃圾或什么也不读取,除非程序稍后因错误数据而崩溃,否则您永远不会知道。
    std::ifstream input("d:\\testinput.txt");
    
    std::vector<std::string> bytes(
         (std::istreambuf_iterator<std::string>(input)),
         (std::istreambuf_iterator<std::string>()));
    
    input.close();
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    using namespace std; // avoid using this
    int main()
    {
        const int MAXARRAY = 4; // avoid using magic numbers
        char arrivOrDepart[MAXARRAY]; // made an array, but prefer std::vector
        string licensePlt[MAXARRAY]; //made an array
        ifstream inFile;
        inFile.open("Text.txt");
        if (!inFile)
        {
            cout << "Can't open file" << endl;
            return 1;
        }
        string temp;
        int i = 0;
        while (i < MAXARRAY && // not overrunning the arrays 
                inFile >> temp >> licensePlt[i] && // read data from file stream
               temp.length() == 1) // read only one character for arrivOrDepart 
        {
            arrivOrDepart = temp[0];
            i++;
        }
    
        inFile.close();
        cin.get();
        return 0;
    }