std :: stringstream operator>>无法将字符串转换为浮点数

时间:2016-08-20 09:28:50

标签: c++ stringstream

我无法理解为什么第二个>>失败。我做错了什么或遗漏了一些代码?

std::ifstream file;
std::stringstream ss;
std::string str;
float f1, f2;

file.open("file.txt");
getline(file, str);
ss.str(str);
ss >> f1;

getline(file, str);//when packed inside if(), evalueates to true
ss.str(str);
ss >> f2; //when packed inside if(), evalueates to false - but why it fails?


std::cout<<"str = "<<str<<"\n";
std::cout<<"ss.str() = "<<ss.str()<<"\n";
std::cout<<"f1 = "<<f1<<"\nf2 = "<<f2<<"\n";

文件:

0.120000
0.120000

输出:

str = 0.120000
ss.str() = 0.120000
f1 = 0.12
f2 = 2.06831e+032

我在多个文件上尝试过这个代码,显然只有第一次插入到float中,文件末尾有一个空行

修改

Dan指出,我尝试直接从文件中提取浮点数:

file.open("file.txt");
file >> f1;
file >> f2;

理想地运作;也很简单的代码

1 个答案:

答案 0 :(得分:6)

在第二次尝试阅读之前,您必须添加以下语句:

import UIKit

class FlipCardAnimator: NSObject, UIViewControllerAnimatedTransitioning {
var originFrame = CGRect.zero
var cardView = UIView()

func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
    return 0.6
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    guard let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey),
        let containerView = transitionContext.containerView(),
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) else {
            return
    }

    let initialFrame = originFrame
    let finalFrame = transitionContext.finalFrameForViewController(toVC)

    let snapshot = toVC.view.snapshotViewAfterScreenUpdates(true)

    snapshot.frame = initialFrame
    snapshot.layer.cornerRadius = 25
    snapshot.layer.masksToBounds = true

    containerView.addSubview(toVC.view)
    containerView.addSubview(snapshot)
    toVC.view.hidden = true

    perspectiveTransformForContainerView(containerView)

    snapshot.layer.transform = yRotation(M_PI_2)

    let duration = transitionDuration(transitionContext)

    UIView.animateKeyframesWithDuration(
        duration,
        delay: 0,
        options: .CalculationModeCubic,
        animations: {

            UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 1/3, animations: {
                fromVC.view.layer.transform = self.yRotation(-M_PI_2)
            })

            UIView.addKeyframeWithRelativeStartTime(1/3, relativeDuration: 1/3, animations: {
                snapshot.layer.transform = self.yRotation(0.0)
            })

            UIView.addKeyframeWithRelativeStartTime(2/3, relativeDuration: 1/3, animations: {
                snapshot.frame = finalFrame
            })
        },
        completion: { _ in
            toVC.view.hidden = false
            snapshot.removeFromSuperview()
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
    })
}


func yRotation(angle: Double) -> CATransform3D {
    return CATransform3DMakeRotation(CGFloat(angle), 0.0, 1.0, 0.0)
}

func perspectiveTransformForContainerView(containerView: UIView) {
    var transform = CATransform3DIdentity
    transform.m34 = -0.002
    containerView.layer.sublayerTransform = transform
}

为什么?

因为当您阅读第一行时,字符串流包含ss.clear(); "0.120000"将导致ss>>f1到达文件末尾。这样就设置了eof标志。

使用str()重置stringstreams的内容时,不会重置状态标志,因此尝试读取将失败。在重置stringstreams的内容后添加ss.clear()将纠正这种情况。

Online demo