C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-14-2009, 12:59 AM   #1
msh
Novice
 
Join Date: Jul 2009
Posts: 32
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.
msh is offline   Reply With Quote
Old 08-14-2009, 02:05 AM   #2
Ex scientia vera
 
Join Date: Sep 2007
Posts: 426
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."
IceDane is offline   Reply With Quote
Old 08-14-2009, 02:21 AM   #3
msh
Novice
 
Join Date: Jul 2009
Posts: 32
I love my arrays.

But yea, you're right. I should rewrite it using strings.
msh is offline   Reply With Quote
Reply

Tags
file, i/o

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:27 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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