Hi all,

I want to write every given number of lines into the different files, which I will be passing as an command line argument. I have written a function for just dividing the file into two, but how would I divide the files for every given number. If anyone can help me that will be great.

{
i++;
cin >> number; (ex:100)
cin >> output_file; (ex:"/local/tmp/sample_1.csv")

is it possible with a for loop? how does it work?
if ( i <= number )
{
out.open(output_file.c_str(),ios::app);
}
if ( i==number+1 )
{
out.close();
output_file = output_file.erase(output_file.length() - 6);
output_file = output_file + "_2.csv";
}
if ( i >= number+1 )
{
out.open(output_file.c_str(),ios::app);
}
if ( !out.is_open() )
cerr << "Error: Could not open " << output_file << " for writing." << std::endl;
}

Thanks a lot in advance.

Looking at one of your comments "(ex:"/local/tmp/sample_1.csv")" it seems like you're on Unix.
So if it's possible I suggest you use awk rather than C++, it'll be much easier.


head -<N> filename | tail -<M> >> splitfile

This command will put lines starting from N and till M from file filename into splitfile.

If you gotta have it in C/C++ get back..

use the mod operator -- it returns the remainder after division. If the remainder is 0 then the number if evenly divisible by the value

When (loop_counter % 100) == 0 then close the current file and start another one.

Even better if you're on Unix/Linux/Cygwin

$ split --help
Usage: split [OPTION] [INPUT [PREFIX]]
Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default
size is 1000 lines, and default PREFIX is `x'.  With no INPUT, or when INPUT
is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -a, --suffix-length=N   use suffixes of length N (default 2)
  -b, --bytes=SIZE        put SIZE bytes per output file
  -C, --line-bytes=SIZE   put at most SIZE bytes of lines per output file
  -d, --numeric-suffixes  use numeric suffixes instead of alphabetic
  -l, --lines=NUMBER      put NUMBER lines per output file
      --verbose           print a diagnostic to standard error just
                            before each output file is opened
      --help     display this help and exit
      --version  output version information and exit

SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.