C++ C++;流式大文本文件
下面是我为类的一个简单部分编写的代码:C++ C++;流式大文本文件,c++,arrays,stream,fstream,ofstream,C++,Arrays,Stream,Fstream,Ofstream,下面是我为类的一个简单部分编写的代码: void ArrayToTextFile::textfiller(string *givenpointer){ cout<< "Recieved array pointer address" << givenpointer << endl; const char * constantcharversion = path.c_str(); ofstream filler(constantcharve
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();
}
无论程序何时运行,我都无法进入return语句。事实上,我必须单击控制台窗口上的退出按钮。当我看到创建的文本文件时,它是巨大的:
我有什么问题?如何修复此问题?您需要传入数组中的元素数
sizeof
只提供字节大小,这很少有用。为了获得数组的大小,您可以使用(sizeof array)/(sizeof array[0])
,但在函数中,您只有一个指针,而不是数组,这将给您不同的结果。您希望sizeof(*givenpoint)
返回什么?提示-这不是数组中的字符串数。请使用givenpointer->size()而不是sizeof(*givenpointer)。哦,我看到给定指针将返回一个元素的大小。那么我怎样才能找到数组的大小呢?@Cygwinnian:你不能。使用std::vector
或将元素数作为附加参数提供。@Cygwinnian,如果不传入它或使用更多的C++-ey集合类型,则不能这样做。
#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;
}