Problem: Write the program columns that takes three command-line arguments. The first is the file to open, the second, the file to write to, and the third is the number of characters to display on each line. Read the input file, character by character, echoing to the output file, inserting a line break after the line width is reached. If the correct number of parameters are not passed, print an error message and exit. You'll have to convert the third parameter to an int for your processing.

This is one of the problem my professor provided for us to study for an exam. I am completely don't know how to handle this problem. And I could not find any example with could help me.
Can someone please help me with this problem? I really need to know this for an exam on Monday. Please.
Thanks.

Test code(my professor provided, I have no idea how this could test the program.)

system("columns file11.txt file12.txt 25");
    cout << "After running:" << endl;
    system("cmd /c type file12.txt")

file11.txt:

The pale Usher--threadbare in coat, heart, body, and brain; I see him now.  He was ever dusting his old lexicons and grammars, with a queer handkerchief, mockingly embellished with all the gay flags of all the known nations of the world.  He loved to dust his old grammars; it somehow mildly reminded him of his mortality.
"While you take in hand to school others, and to teach them by what name a whale-fish is to be called in our tongue leaving out, through ignorance, the letter H, which almost alone maketh the signification of the word, you deliver that which is not true." --HACKLUYT

1. if argc != 3, throw error message. exit program.
2. create an ifstream object that will handle all file reading operations.
3. create on ofstream object that will handle all file writing operations.
4. open the file using your ifstream object, using argv[0] as the file to open.
5. perform some sort of error checking to see if the file was actually opened.
6. create a new file using your ofstream object, using argv[1] as the file to create.
7. loop through the file, reading character at a time; write the char to the output file.
8. if (char_count == atoi(argv[2])) write a '\n' newline to file
9. close the ifstream object
10. close the ofstream object
11. return 0;

I have followed your instruction, however, my code doesn't seem to copy each character over the output file. It doesn't go through the if statement, it return the error message.

using namespace std;

int string_to_int(string s)
{
    istringstream instr(s);
    int n;
    instr >> n;
    return n;
}
int main(int argc, char * argv[])
{
    if(argc < 3)
    {
        cerr << "Usage: " << argv[0] << " file-to-read" << endl;
        return EXIT_FAILURE;
    }
    string file_in = argv[0];
    string file_out = argv[1];
    string len = argv[2];
    
    int num_char = string_to_int(len);
    
    ifstream fin;
    fin.open(file_in.c_str());
    if(!fin)
    {
        return EXIT_FAILURE;
        fin.close();
    }
    ofstream fout;
    fout.open(file_out.c_str());
    if(!fout)
    {
        return EXIT_FAILURE;
        fout.close();
    }
    char ch;
    int count = 0;
    fin >> noskipws;
    while(fin)
    {
        fout << ch;
        count++;
        if(count == num_char)
        {
            fout << '\n';
            count = 0;
        }
    }
    return 0;
}
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.