简体   繁体   English

在 C++ 中逐字符读取文件

[英]Reading file char by char in C++

I have this txt file我有这个txt文件

ENG222;COMPUTER PROGRAMMING II;2;6;C;D;DR. JIM
ENG111;COMPUTER PROGRAMMING I;1;4;C;D;DR. JOHNSON
ENG313;MATH I;3;3;E;D;DR. ALISSON
ENG104;CHEM;1;5;C;D;DR. SAM

how can i read and write this file to ArrayList from ';'我如何从“;”读取和写入这个文件到 ArrayList to ';'至 ';' in C++在 C++

I suppose that you mean java's ArrayList which has an equivalence in C++ of std::vector .我想您的意思是 java 的ArrayListstd::vector的 C++ 等效。 Also I assume that you want to store each string between the semicolons inside the ArrayList .此外,我假设您希望将每个字符串存储在ArrayList内的分号之间。 I also assume that the file is called input.txt .我还假设该文件名为input.txt

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

int main()
{
    std::vector<std::string> strings;

    std::ifstream input("input.txt");

    std::string tempString;
    while (std::getline(input, tempString, ';'))            
        strings.push_back(tempString);

    for (auto& str : strings)
        std::cout << str << '\n';

}

Output: Output:

ENG222
COMPUTER PROGRAMMING II
2
6
C
D
DR. JIM
ENG111
COMPUTER PROGRAMMING I
1
4
C
D
DR. JOHNSON
ENG313
MATH I
3
3
E
D
DR. ALISSON
ENG104
CHEM
1
5
C
D
DR. SAM

Edit:编辑:

I've understood from the comments that you want to add each column to an ArrayList .我从评论中了解到您希望将每一列添加到ArrayList I'll assume that the number of columns is just 7 .我假设列数仅为7 If you want the number of columns to vary, you can input the value somehow.如果您希望列数不同,您可以以某种方式输入该值。 maybe by reading the value from the file ( input >> columns ) or by reading it from the standard input ( std::cin >> columns ).可能是通过从文件中读取值( input >> columns )或从标准输入( std::cin >> columns )中读取值。 In this code, if some row contains only 4 values for example, I consider the rest of the 3 values to be empty.在此代码中,例如,如果某行仅包含 4 个值,我认为这 3 个值中的 rest 为空。

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>

int main()
{
    std::ifstream input("input.txt");

    int columns = 7;

    /*

    // in case the columns count is not constant.

    // to read from the file. in this case, the number should be at the begining of the file.
    input >> columns;

    // to read from the standard input (from the terminal).
    std::cin >> columns;

    // to white spaces.
    input.ignore();

    */

    std::vector<std::vector<std::string>> values(columns);


    std::string tempLineString;
    std::string tempWordString;

    while (!input.eof())
    {
        std::getline(input, tempLineString);
        std::istringstream iss(tempLineString);

        int i = 0;
        while (i < columns && std::getline(iss, tempWordString, ';'))
        {
            values[i].push_back(tempWordString);
            i++;
        }

        while (i < columns)
            values[i++].push_back("");
    }

    for (int i = 0; i < columns; i++)
    {
        std::cout << "Column " << i + 1 << " values: \n";
        for (auto& str : values[i])
            std::cout << str << '\n';
        std::cout << '\n';
    }

}

Output: Output:

Column 1 values:
ENG222
ENG111
ENG313
ENG104

Column 2 values:
COMPUTER PROGRAMMING II
COMPUTER PROGRAMMING I
MATH I
CHEM

Column 3 values:
2
1
3
1

Column 4 values:
6
4
3
5

Column 5 values:
C
C
E
C

Column 6 values:
D
D
D
D

Column 7 values:
DR. JIM
DR. JOHNSON
DR. ALISSON
DR. SAM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM