C++ 读取文件';将不带空格的行放入单独的变量中
第一次在这个网站上提问,现在开始。我绞尽脑汁已经有一段时间了,但似乎仍然找不到答案 假设我有一个文件,内容如下:C++ 读取文件';将不带空格的行放入单独的变量中,c++,C++,第一次在这个网站上提问,现在开始。我绞尽脑汁已经有一段时间了,但似乎仍然找不到答案 假设我有一个文件,内容如下: 123456789John Doe 0001111.11 925219042Mary Jane 0000302.54 891492829Gertrude Marisou 0123467.76 我该如何将say,123456789和John分离成各自的字符串,以便输入到包含四个变量的向量中?(Std:
123456789John Doe 0001111.11
925219042Mary Jane 0000302.54
891492829Gertrude Marisou 0123467.76
我该如何将say,123456789和John分离成各自的字符串,以便输入到包含四个变量的向量中?(Std::string
,Std::string
,Std::string
,Double
)
这是我目前的代码,如果你们都想看一眼,告诉我哪里出了问题
#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();
}
};
};
我希望输出看起来和输入一样。非常感谢您的帮助。
scanf(“%9s%11s%8s%d%e”、&number、&姓氏、&name、&dec、&sci)
std::getline()
总是希望读取以换行符结尾的字符串。甚至在从std::stringstream
读取时。由于std::stringstream不包含换行符,因此对std:getline()的第一次调用将再次吞掉整行。使用
>>`运算符,而不是内部的std::getline()
循环。@SamVarshavchik我想我有点理解你的意思,但是为了我的启发,你能进一步解释一下吗?如果不打算使用getline,具体来说,您将如何编写循环?如果我只使用>>操作符,它会真正理解我想要分离的信息吗?只要字段之间至少有一个空格,并且字段本身不包含空格。你并不真的需要一个循环,但你肯定仍然可以有一个。>
操作符的调用方式与getline()
完全相同。它不是while(getline(ss,scapeg))
,而是while(ss>>scapeg)
。实际上并不比这复杂。在这种情况下,使用std::string
的substr()
方法精确提取字符串的每个部分。scanf(“%9s%11s%8s%d%e”、&number、&name、&dec、&sci)
std::getline()
总是希望读取以换行符结尾的字符串。甚至在从std::stringstream
读取时。由于std::stringstream不包含换行符,因此对std:getline()的第一次调用将再次吞掉整行。使用
>>`运算符,而不是内部的std::getline()
循环。@SamVarshavchik我想我有点理解你的意思,但是为了我的启发,你能进一步解释一下吗?如果不打算使用getline,具体来说,您将如何编写循环?如果我只使用>>操作符,它会真正理解我想要分离的信息吗?只要字段之间至少有一个空格,并且字段本身不包含空格。你并不真的需要一个循环,但你肯定仍然可以有一个。>
操作符的调用方式与getline()
完全相同。它不是while(getline(ss,scapeg))
,而是while(ss>>scapeg)
。实际上并不比这复杂。在这种情况下,使用std::string
的substr()
方法精确提取字符串的每个部分。
123456789John Doe 0001111.11 -9.25596e+61
925219042Mary Jane 0000302.54 -9.25596e+61
191492829Gertrude Marisou 0123467.76 -9.25596e+61