这段代码应该生成一个名为“Data.in”的文件,但不会生成任何内容

时间:2014-09-10 02:17:24

标签: c++ visual-c++

这段代码应该生成一个名为data.in的文件,其中包含我应该读取的随机二进制值并生成标志。但是,使用多个IDE,不会产生任何结果。我做错了什么?

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>

using namespace std;

int createTestData(int flags[], int dataSize) {
    ofstream binary_file("data.in", ios::out | ios::binary);

    if (!binary_file) {
        cout << "The file data.in could not open for a binary write.\n" << endl;
        return (1);
    } else if (!binary_file.write(reinterpret_cast<char*>(flags),
                                  sizeof(int) * 3)) {
        cout << "Write failure 1 in createTestData().\n" << endl;
        return (1);
    } else if (!binary_file.write(reinterpret_cast<char*>(&dataSize),
                                  sizeof(int))) {
        cout << "Write failure 2 in createTestData().\n" << endl;
        return (1);
    }

    int val;

    for (int i = 0; i < 3; i++) {
        if (flags[i] == 1) {
            for (int j = 0; j < dataSize; j++) {
                val = rand();

                if (!binary_file.write(reinterpret_cast<char*>(&val),
                                       sizeof(int))) {
                    cout << "Write failure 3 in createTestData().\n" << endl;
                    return (1);
                }
            }
        }
    }

    binary_file.close();
    return (0);
}

//-----------------------------------------------------------------------------

int main(int argc, char* argv[]) {
    int dataFlags[3];
    int dataSize = 0;

    if (argc != 5) {
        cout << "\nUsage: " << argv[0]
             << " sstFlag sFlag sshFlag dataArraySize\n" << endl;
        return (1);
    } else {
        dataFlags[0] = atoi(argv[1]);
        dataFlags[1] = atoi(argv[2]);
        dataFlags[2] = atoi(argv[3]);

        dataSize = atoi(argv[4]);

        createTestData(dataFlags, dataSize);
    }

    return (0);
}

1 个答案:

答案 0 :(得分:0)

在Visual Studio 2013中,data.in是在

中生成的

C:\Users\<user>\Documents\visual studio 2013\Projects\<project-name>\<project-name>

这是您的主.cpp文件所在的文件夹。