I am making an fstream clone thats supposed to be more simple to use. I have multiple "write" functions for writing to the file, however, these versions added a new line at the end, so to try and find a way to eliminate that blank last line, i made a write_test function to find a way to write without adding a final blank line.
Pretty simple.
In theory, it should work. However, I cannot iterate through the string array because i cant get the size of it.
I tried sizeof(lines)/sizeof(std::string)
as well as sizeof(lines)/sizeof(lines[0])
and both returned the same result, 0.
My goal is to get the actual size of the string array so the function can iterate through it.
Here is my main.cpp file,
#include "bstdlib/fstream-new.hpp"
int main(){
// make object
bstd::fstream::file fs("./output/file.txt");
// string array
std::string str1, str2, str3;
str1="hello";
str2="world";
str3="!!!!!";
std::string strs[]={str1, str2, str3};
// write it, this is where the cactus on my seat lies
fs.write_test(strs);
}
Here is the write_test function
void write_test (std::string lines...) {
// make fstream object
std::fstream fs(this->fp.c_str(), std::fstream::out);
// get size of array
int range = sizeof(lines) / sizeof(lines[0]);
std::cout << range; // print
// make 1 grand string of each line
std::string grand_string;
for (int i=0; i<range-2; i++) {
grand_string += (lines[i]+"\n");
}
grand_string += lines[range-1];
// write, i used .c_str() because i felt as though that was what type it takes
fs << grand_string.c_str();
fs.close();
this->scan(); // just a function to read the file and update this->lines, irrelevant to the problem
}
It prints the size of the string array, 0. Of course, it writes nothing to the file, leaving it blank.