【问题标题】:C++ ofstream cannot write to fileC++ ofstream 无法写入文件
【发布时间】:2010-04-06 02:21:46
【问题描述】:

嘿,我正在尝试将一些数字写入文件,但是当我打开文件时它是空的。你能帮帮我吗?谢谢。

/** main function **/
int main(){

    /** variables **/
    RandGen* random_generator = new RandGen;
    int random_numbers; 
    string file_name;   

    /** ask user for quantity of random number to produce **/
    cout << "How many random number would you like to create?" << endl;
    cin >> random_numbers;

    /** ask user for the name of the file to store the numbers **/
    cout << "Enter name of file to store random number" << endl;
    cin >> file_name;

    /** now create array to store the number **/
    int random_array [random_numbers];

    /** file the array with random integers **/
    for(int i=0; i<random_numbers; i++){
        random_array[i] = random_generator -> randInt(-20, 20);
        cout << random_array[i] << endl;
    }

    /** open file and write contents of random array **/
    const char* file = file_name.c_str();
    ofstream File(file);

    /** write contents to the file **/
    for(int i=0; i<random_numbers; i++){
        File << random_array[i] << endl;
    }

    /** close the file **/
    File.close();   

    return 0;
    /** END OF PROGRAM **/
}

【问题讨论】:

  • 一看就知道这段代码不会编译——“random_array”数组是用一个非常量变量声明的。你应该发布你的实际代码,我怀疑你已经过度简化了
  • @Terry:这是一个非标准扩展。
  • @Potatocorn:真的吗?天哪,在哪个平台上?
  • 无论如何,random_generator 是动态分配的吗?例如,为什么总是首选自动分配,您正在泄漏它。另外,等到需要变量后再声明它们;否则您的代码很难阅读。此外,您将文件变量命名为 File,它不适合您的其他变量,这很奇怪。它可能只是ofstream file(file_name.c_str());。此外,无需手动关闭文件,它会自动完成。最后,您的错误在这里:int random_array [random_numbers];。需要是std::vector
  • 我发现即使在程序退出前立即显式关闭文件并释放内存总是一个好主意,因为否则,如果该代码被移动,很容易忘记清理。

标签: c++ file ifstream ofstream file-writing


【解决方案1】:

您不能在堆栈上声明一个只有在运行时才知道大小的整数数组。但是,您可以在堆上声明这样的数组:

int *random_array = new int[random_numbers];

不要忘记在 main() 的末尾添加 delete [] random_array;(以及 delete random_generator;)以释放您使用 new 分配的内存。当您的程序退出时,此内存会自动释放,但最好还是释放它(如果您的程序增长,以后很容易忘记添加它)。

除此之外,您的代码看起来还不错。

【讨论】:

  • 我可以建议一个 std::vector 代替吗? :)
  • -1,对不起。使用std::vector,没有理由手动操作。
  • @GMan:我同意有很多更好的编写代码的方法(std::vector 是个好主意)......但这是一个足够简单的例子,手动内存管理不太可能成为一个问题。我试图与原始代码保持一致(这看起来不像是由一个准备好钻研 STL 的人编写的)。
  • @GMan 不确定我是否同意,手工操作没有错,尤其是对于像这样的简单示例
  • @Cameron:我们不同意。我认为人们应该首先使用标准库,然后了解它是如何工作的。在人们需要了解vector 的工作原理之前,没有必要教人们使​​用手动内存管理。在他们了解内存管理及其所有注意事项之前,这表明它只会帮助他们编写混乱、不安全和糟糕的代码。不过出于好意,我会删除 -1。
【解决方案2】:

无需循环两次或保留数组或向量。

const char* file = file_name.c_str();
ofstream File(file);

for(int i=0; i<random_numbers; i++){
    int this_random_int = random_generator -> randInt(-20, 20);
    cout << this_random_int << endl;
    File << this_random_int << endl;
}

File.close();

【讨论】:

  • 有时,如果它的行为真的无法解释,那么保留并研究它而不是压制它是有用的。
【解决方案3】:

如果我只是填写你的 RandGen 类来调用rand,该程序在 Mac OS X 10.6 上运行良好。

How many random number would you like to create?
10
Enter name of file to store random number
nums
55
25
44
56
56
53
20
29
54
57
Shadow:code dkrauss$ cat nums
55
25
44
56
56
53
20
29
54
57

此外,我认为它没有理由不在 GCC 上工作。你在什么版本和平台上运行?能否提供完整的源码?

【讨论】:

  • int random_array [random_numbers]; 非常量大小的非动态数组?
  • 嗯,公平,但我没有看到 OP 想要编写非标准 C++ 的迹象。
  • @GMan:好吧,我无法重现该错误,但我认为这里真正的不当行为更有趣,我们应该一次进行一项更改。
猜你喜欢
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多