954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with writing each 100 lines into different files.

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.

jobra
Newbie Poster
19 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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 - filename | tail - >> 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..

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You