读写float文件的数组

时间:2016-04-07 01:42:41

标签: c++ arrays io

我在c ++中有一个float数组,我想将它保存到二进制文件中(以节省空间)并且以后能够再次读取它。为此,我编写了以下代码来编写数组:

        ReceiverList receiverList = new ReceiverList();
        receiverList.receiver = new List<Receiver>();
        RequestEnvelope requestEnvelope = new RequestEnvelope("en_US");

        Dictionary<string, string> configurationMap = new Dictionary<string, string>();

        configurationMap.Add("account1.apiUsername", "----");
        configurationMap.Add("account1.apiPassword", "----");
        configurationMap.Add("account1.apiSignature", "----");
        configurationMap.Add("account1.applicationId", "APP-80W284485P519543T");

        configurationMap.Add("mode", "sandbox");

        Receiver receiver1 = new Receiver();
        receiver1.amount = ((decimal?)9.00);
        receiver1.email = "rbxtrade-seller@gmail.com";

        receiverList.receiver.Add(receiver1);

        Receiver receiver2 = new Receiver();
        receiver2.amount = ((decimal?)2.00);
        receiver2.email = "rbxtrade-buyer@gmail.com";

        receiverList.receiver.Add(receiver2);

        PayRequest request = new PayRequest(requestEnvelope, "PAY", "https://devtools-paypal.com/guide/ap_parallel_payment/dotnet?cancel=true", "USD", receiverList, "https://devtools-paypal.com/guide/ap_parallel_payment/dotnet?success=true");

        AdaptivePaymentsService service = new AdaptivePaymentsService(configurationMap);
        PayResponse response = service.Pay(request);

        if (!response.responseEnvelope.ack.ToString().Trim().ToUpper().Equals(AckCode.FAILURE.ToString()) && !response.responseEnvelope.ack.ToString().Trim().ToUpper().Equals(AckCode.FAILUREWITHWARNING.ToString()))
        {
            Process.Start("https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=" + response.payKey); //opens the webpage
        }
        else
        {
            Console.WriteLine("E:" + String.Join("\n", response.error.Select((x) => x.message)));
        }

然后我立即重新打开文件以检查数据是否已正确保存:

float *zbuffer = new float[viewport[3]*viewport[2]*4];
//.....
//.. populate array
//.....
ofstream zOut(string(outFile).append("_geom.txt", ios::out | ios::binary));
zOut.write(reinterpret_cast<char*>(zBuffer), sizeof(float)*viewport[3] * viewport[2] * 4);
zOut.close();

然而,当我检查两个数组是否相等时,我会得到截然不同的值:

ifstream zIn(string(outFile).append("_geom.txt"), ios::in | ios::binary);
float *chBuffer = new float[viewport[3] * viewport[2] * 4];
zIn.read(reinterpret_cast<char*>(chBuffer), sizeof(float)*viewport[3] * viewport[2] * 4);
zIn.close();

我是否错误地阅读或写入数据?我读过的数据是否有任何问题?

1 个答案:

答案 0 :(得分:1)

看看这两行:

ofstream zOut(string(outFile).append("_geom.txt", ios::out | ios::binary));
ifstream zIn(string(outFile).append("_geom.txt"), ios::in | ios::binary);

在我看来,第一行有一个拼写错误。也许是预期的

ofstream zOut(string(outFile).append("_geom.txt"), ios::out | ios::binary);