将没有空格的文件行读入单独的变量

时间:2016-12-15 20:50:08

标签: c++

第一次在这个网站上提问,所以这里。我已经在很长一段时间里绞尽脑汁,但似乎仍无法找到答案。

我们说我的文件内容如下:

    123456789John       Doe        0001111.11
    925219042Mary       Jane       0000302.54
    891492829Gertrude   Marisou    0123467.76

我如何将say,123456789和John分成各自的字符串,以便输入包含四个变量的向量? (Std::stringStd::stringStd::stringDouble

以下是我目前的代码,如果你们都想看看它并告诉我哪里出错了。

#pragma once
#if !defined(__Account7_h__)
#define __Accoun7_h__

#include <string>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
//Personal file for trimming the extra whitespace
#include "Trim.h"



class Account7 {
private:
    std::string account_code;
    std::string first_name;
    std::string last_name;
    double balance;



public:
    //Getters, Setters, Initialization List and whatnot.


    //On a separate file
#if !defined(__Vmanager7_h__)
#define __Vmanager7_h__

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include "Account7.h"
#include "Trim.h"

    using namespace generic;
    class Vmanager7 {
    public:

        int a = 1;
        std::ifstream infile;
        std::ofstream outputFile;

        std::vector<Account7> _Account;
        Account7 temp;

        std::string Empl;
        std::string scapeg;
        std::string acc_c;
        std::string fname;
        std::string lname;
        double bal;




        int Managed() {
            int count;

            infile.exceptions(std::ifstream::failbit | std::ifstream::badbit);

            try {
                infile.open("account.dat", std::ifstream::in);
            }
            catch (std::ios_base::failure &fail) {
                std::cout << "File is not opening" << std::endl;
                return 0;
            }
            infile.exceptions(std::ios::goodbit);
            while (getline(infile, Empl)) {
                count = 1;
                std::istringstream ss(Empl);
                while (getline(ss, scapeg)) {
                    if (count == 1)
                        acc_c = scapeg;
                    else if (count == 2)
                        fname = scapeg;
                    else if (count == 3)
                        lname = scapeg;
                    else
                        bal = atof(scapeg.c_str());
                    count++;

                }

                temp.setac(acc_c);
                temp.setfn(fname);
                temp.setln(lname);
                temp.setba(bal);



                _Account.push_back(temp);

            }
            infile.close();

            outputFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);

            try {
                outputFile.open("Aoutput.dat");
            }
            catch (std::ios_base::failure &fail) {
                std::cout << "File opening fail" << std::endl;
                return 0;
            }
            outputFile.exceptions(std::ios::goodbit);

            for (int i = 0; i < _Account.size(); i++) {


                std::cout << _Account[i].getac() << " " << _Account[i].getfn() << " " << _Account[i].getln() << " " << _Account[i].getba();
                bal = _Account[i].getba();
                bal -= int(bal);
                if (bal == 0)
                    std::cout << ".00";
                std::cout << '\n';


            }


            outputFile.close();

        }
    };
};

我得到的输出是这样的:

    123456789John       Doe        0001111.11   -9.25596e+61
    925219042Mary       Jane       0000302.54   -9.25596e+61
    191492829Gertrude   Marisou    0123467.76   -9.25596e+61

我希望输出看起来就像输入一样。任何帮助都会非常感激。

0 个答案:

没有答案