Thread: Is there a faster way to open and read files, this seems too slow.

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    3

    Post Is there a faster way to open and read files, this seems too slow.

    I'm pretty new to C++, I wrote this small program to learn about file handeling, its a little program to display files to the screen like windows type or *nix's more program, it works ok but just seems very slow, is there a faster way to do this?
    Code:
    #include <windows.h>
    #include <fstream.h>
    #include <iostream.h>
    #include <io.h> 
    
    int main(int argc, char * argv[])
    {
        char line[1000];
        if(argc!=2)
        {
            cout << "Please specify a filename"; 
            return 0;   
        }             
        if(access(argv[1], 00)) 
        {		
            cout << "The file " << argv[1] << " does not exist";
            return 0;
        }
    	LARGE_INTEGER us;
        long int t,a;
        QueryPerformanceCounter(&us);
        t = us.u.LowPart;
        ifstream theFile;
        theFile.open(argv[1]);
        while(!theFile.eof())
        {
            theFile.getline(line,1000); 
            cout << line << endl;
        }
        theFile.close();
        QueryPerformanceCounter(&us);
        a = us.u.LowPart -t;
        cout << a << " micro seconds";
        return 0;
    }
    sorry for the long paste.
    I included a benchmark to test how long it takes, I managed to half the time reading a line at a time instead of a char at a time.

    Will be greatful for any ideas

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    Here's how I did it for a client/server program.
    Code:
      // Start file access.
      ifstream infile (largeBuffer, ifstream::binary);
      // Get file size.
      infile.seekg(0,ifstream::end);
      int size=infile.tellg();
      // Go back to begining (Very important :) )
      infile.seekg(0);
      // allocate memory for file content
      fileBuffer = new char [size];
      // read content of infile
      infile.read (fileBuffer, size);
    I would reccomend also to make fileBuffer size + 1, then give the last place a '\n' to terminate it, for what I was doing, I didn't need it.

    Oh, also:
    largeBuffer held my file name. And looking at it, not sure if you need the binary access, though it might be faster, no idea about that. This should be a lot faster, as its all at once.

    Not sure, maybe you could even write it straight out to screen. ifstream IS a stream, maybe you can jsut

    cout << infile << endl;
    Last edited by Sethiorth; 12-27-2003 at 10:34 AM.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Bad idea. What if the user opens a multi-gigabyte file?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    Then he would have a problem. I am sorry, was jsut trying to give him another option for getting information from a file. And yes, I know my code had the same problem, but the current version of the section is a lot more complicated, this earlier snippet explains the idea, I figured he could handle the implimentation.

    Seth

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    Adding the ifstream::binary when opening the file did seem to make a big difference in speed, ran in about 75% of the time than it did without it. This was testing without the cout as its just to jumpy with it and the times change all the time.

    I should probably do a little more reading as i don't even know what the ifstream tellg() function does. I just made up the program with the command line stuff to make it easy to test the speeds, I should have said in my first post, that I would be loading the file into RAM anyway, was just using that program to learn about i/o streams.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    Glad that could help. I think I found it from here, what I use for c/c++ reference:

    http://www.cppreference.com/

    Under C++ I/O. Essentially its a stream pointer that was pointed to the end of the file, so as to return the file's size.

    Seth

  7. #7
    Not using fstream for file manipulation should speed it up a bit. cstdio r0x0rz.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Using the streambuffers directly should remove nearly all speed disadvantages of C++ I/O vs. C I/O. Nearly.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    Thank you all for your ideas, as I said I'm pretty new to C++ (and C) so I guess i'll google for cstdio stuff and see what I can find
    It took me a while to figure out QueryPerformanceCounter() to I could time things better, was just using GetTickCount() before, but much better in microseconds.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. open and read bmp image files
    By cnewbie85 in forum C Programming
    Replies: 2
    Last Post: 05-19-2009, 01:36 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Read In From Many Files In One Function
    By djwicks in forum C Programming
    Replies: 12
    Last Post: 03-24-2005, 07:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. open and read data from files
    By ethorn10 in forum C Programming
    Replies: 5
    Last Post: 02-04-2003, 04:00 AM