File I/O loop.

This is a discussion on File I/O loop. within the C++ Programming forums, part of the General Programming Boards category; I've written a little tool for myself to split a long text file into several shorter ones. (Probably re-invented to ...

  1. #1
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568

    File I/O loop.

    I've written a little tool for myself to split a long text file into several shorter ones. (Probably re-invented to wheel too. Heh.)

    The bit I'm concerned about follows. The question is: is it possible to avoid write/read to buffer in a trivial way in an effort to improve the execution speed?

    Code:
        int counter = 0;
        char buffer[1025];
        while (inFile.good()) {
            char outname[256];
            if (df) {
                sprintf(outname, ".\\%s\\%s%i.txt", basename, basename, counter);
            } else {
                sprintf(outname, "%s%i.txt", basename, counter);
            }
            
            std::ofstream outFile(outname);
            if (!outFile.is_open()) {
                std::cout << "Output file " << outname
                          << " could not be written.\nInterrupting.\n"
                             "Clean up and restart the program.\n";
                return 1;
            }
            
            for (int i = 1; i <= lines && inFile.good(); i++) {
                inFile.getline(buffer, 1025);
                outFile << buffer << '\n';
            }
            outFile.close();
            std::cout << "File " << outname << " written successfully.\n";
            counter++;
        }
    P.S.
    Feel free to suggest improvements where they can be made.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by msh View Post
    I've written a little tool for myself to split a long text file into several shorter ones. (Probably re-invented to wheel too. Heh.)

    The bit I'm concerned about follows. The question is: is it possible to avoid write/read to buffer in a trivial way in an effort to improve the execution speed?

    Code:
        int counter = 0;
        char buffer[1025];
        while (inFile.good()) {
            char outname[256];
            if (df) {
                sprintf(outname, ".\\%s\\%s%i.txt", basename, basename, counter);
            } else {
                sprintf(outname, "%s%i.txt", basename, counter);
            }
            
            std::ofstream outFile(outname);
            if (!outFile.is_open()) {
                std::cout << "Output file " << outname
                          << " could not be written.\nInterrupting.\n"
                             "Clean up and restart the program.\n";
                return 1;
            }
            
            for (int i = 1; i <= lines && inFile.good(); i++) {
                inFile.getline(buffer, 1025);
                outFile << buffer << '\n';
            }
            outFile.close();
            std::cout << "File " << outname << " written successfully.\n";
            counter++;
        }
    P.S.
    Feel free to suggest improvements where they can be made.
    Use string streams and strings, not character arrays and sprintf. You're writing C++, not C.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I love my arrays.

    But yea, you're right. I should rewrite it using strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 01:14 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21