C++ ofstream 无法写入文件

发布于 2024-08-28 07:20:51 字数 1249 浏览 10 评论 0原文

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

/** 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 **/
}

Hey I am trying to write some numbers to a file, but when I open the file it is empty. Can you help me out here? Thanks.

/** 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 **/
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贱贱哒 2024-09-04 07:20:51

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

int *random_array = new int[random_numbers];

不要忘记在 main() 末尾添加 delete [] random_array; (以及 delete random_generator; )释放使用new分配的内存。当您的程序退出时,该内存会自动释放,但无论如何释放它都是一个好主意(如果您的程序不断增长,很容易忘记稍后添加它)。

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

You can't declare an array of integers with a size known only at run-time on the stack. You can declare such an array on the heap however:

int *random_array = new int[random_numbers];

Don't forget to add delete [] random_array; at the end of main() (and delete random_generator; too) to deallocate the memory that you allocated using new. This memory is automatically freed when your program exits, but it's a good idea to release it anyway (if your program ever grows, it's easy to forget to add it in later).

Apart from that, your code looks fine.

[浮城] 2024-09-04 07:20:51

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

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();

There's no need to loop twice or keep an array or vector.

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();
╰◇生如夏花灿烂 2024-09-04 07:20:51

如果我只是填写您的 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 上工作。您运行在什么版本和平台上?能提供完整源码吗?

If I just fill in your RandGen class to call rand, the program works fine on 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

Moreover, I see no reason for it not to work on GCC. What version and platform are you running on? Can you provide complete source?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文