create a new CSV file and limit it size to 100 MB

hello i want to open and then write into a file until it size gets to a specific size , let say for example 100 MB. and then after the first file reach to 100 MB size i want to open the next file and to the same until all of the data was taken...
so first off all how can i write something like:"while(my File size < 100 MB" ?

and second how can i open another file while i don't know how many file i need to open? should i use Dynamic memory allocation? and how can i give all of the files the same name but with different number like myfile_1,myfile_2...

for now this is the code that i written
ofstream myfile;
myfile.open("myfile_1.csv");

/while(myfile_1.size<100 MB) ***this is the part that i need help
///write data into myfile. ****dont need help here.

after the while loop i want to open new file and name him like the first one but with +1 on its number(myfile_1,myfile_2.myfile_3...)?

can someone help me please?
You need to keep count how much data you have written to your file and when it's 100 MB you close it and open a new file;
Where does the data come from?
Do you know beforehand how much data you have ?
there is no other way?
i can't check the file size?

the data come from second pc that connected to the first pc thru UART adapter.
i am using system call (read,write)and a buffer.
i dont know how much data i have but i know that is at most 64 GB.

and how can i add more files with Dynamic memory allocation?
i can't check the file size?
The file object can tell you the size, it's the current put position.

the data come from second pc that connected to the first pc thru UART adapter.
i am using system call (read,write)and a buffer.
i dont know how much data i have but i know that is at most 64 GB.
Does the data come into your application? If so, you have complete control. If you don't, it's not clear what your requirement is.

These links may help.
https://linux.die.net/man/8/logrotate
https://www.networkworld.com/article/3218728/linux/how-log-rotation-works-with-logrotate.html
Not sure if this helps, but just wanted to add that you can get the filesize by using stat.
https://stackoverflow.com/a/6039648
If you want to support files >4 GB, you will need to use stat64.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

int main() {
	for(int K=0; K<42; ++K){
		std::string s = "hello_world_" + std::to_string(K) + ".txt"; //no leading 0, sort issues
		std::stringstream ss;
		ss << "hello_world_" << std::setw(4) << std::setfill('0') << K << ".txt";
		std::cout << s << '\t' << ss.str() << '\n';

		std::ofstream out(ss.str());
		out << K << '\n';
	}
}
1
2
3
4
5
6
7
8
9
10
11
void write_more(ofstream& f, unsigned& cnt_files, const string& text)
{
//	if (f.tellp() + text.size() > 100000000UL) {
	if (f.tellp() > 100000000UL) {
		f.close();
		ostringstream filename;
		filename << "file_" << ++cnt_files << ".csv";
		f.open(filename.str());
	}
	f << text;
}
Last edited on
Topic archived. No new replies allowed.