简体   繁体   English

C ++ ofstream巨大的文本文件

[英]C++ ofstream Huge Text File

So, here's my code for a simple part of a class: 因此,这是我的一个类的简单部分的代码:

void ArrayToTextFile::textfiller(string *givenpointer){
    cout<< "Recieved array pointer address" << givenpointer << endl;
    const char * constantcharversion = path.c_str();
    ofstream filler(constantcharversion);

    int i = 0;
    //string delims = (string)delim;
    for(i = 0; i < sizeof(*givenpointer); i++) {
        filler << *givenpointer << delim;
        givenpointer = givenpointer + 1;
    }
    filler.close();
}

The pointer points the first element in an array of strings. 指针指向字符串数组中的第一个元素。

delim is a ';' delim是';'

This is my main class: 这是我的主要课程:

#include <iostream>
#include "ArrayToTextFile.h"
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{
    system("color 1A");

    cout << "Hello world!" << endl;
    string kinch[3] = {"a", "second el", "third"};
    ArrayToTextFile newone("C:/haha.txt", ';');
    string *pointy = &kinch[0];

    newone.textfiller(pointy);
    return 0;
}

Whenever the program runs, I can never make it to the return statement. 每当程序运行时,我永远都无法使它进入return语句。 In fact I have to click the exit button on the console window. 实际上,我必须单击控制台窗口上的退出按钮。 When I look at the text file created it's huge: 当我查看创建的文本文件时,它是巨大的:

看这里

What is my problem? 我怎么了 How can I fix this? 我怎样才能解决这个问题?

You need to pass in the number of elements in the array. 您需要传入数组中元素的数量。 sizeof will only give you a size in bytes, which is rarely useful. sizeof只给您一个字节大小,这很少有用。 In order to get the size of an array, you can use (sizeof array)/(sizeof array[0]) , but inside the function you only have a pointer, not an array, and this will give you different results. 为了获得数组的大小,可以使用(sizeof array)/(sizeof array[0]) ,但是在函数内部您只有一个指针,没有数组,这将为您提供不同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM