Thread: C++ / Qt5.8 - Read file into char vector

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    56

    C++ / Qt5.8 - Read file into char vector

    Hello,

    I'm reading a USB stick into an unsigned char vector.
    Not a problem, Method 1 is obviously faster than Method 2.
    As you can see I'm reading into a buffer and then putting the data into the vector.
    In Method 1, would it be possible to read straight into the vector? If so, how?

    Regards
    Code:
        std::vector <unsigned char> buff;
        buff.reserve(size);
    Code:
    Method 1
        //check on file size
        unsigned char data[512];
        infile.read((char *)&data, size);
        for (int i = 0; i < size; i++)
        {
            buff.push_back(data[i]);
            printf("%i - %c, ", i, data);
        }
    Code:
    Method 2
        unsigned char data[1];
        for (int i = 0; i < size; i++)
        {
            if (infile.eof()) // check for EOF
            {
                printf("EOF reached\n");
                cin.get();
                exit (999);
            }
    
            infile.read((char *)&data, sizeof(data));
            buff.push_back(data);
            printf("%i - %c, ", i, data);
        }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Code:
    // C++11
    buff.resize(size);
    infile.read((char *)buff.data(), size);
    And you're not checking for eof properly in Method 2. Think about it.

    And you are still using printf in C++.
    Last edited by algorism; 03-03-2017 at 04:44 PM.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Why did you title this Qt. Do you want someone to comment on how to use Qt APIs to read the file? Write your attempt first using Qt APIs and someone can suggest improvements.

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello,

    Thanks for the code.
    Quote Originally Posted by algorism View Post
    Code:
    // C++11
    buff.resize(size);
    infile.read((char *)buff.data(), size);
    And you're not checking for eof properly in Method 2. Think about it.

    And you are still using printf in C++.
    Is this better?
    Code:
        if ( (infile.read((char *)buff.data(), size)).eof() )
        {
            std::cout << "EOF reached!" << endl;
            std::cin.get();
            return (105);
        }
    The printf's are only for testing, I'm used to them.

    c99tutorial
    My mistake.

    Regards

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    For eof testing in your "Method 2", I was thinking something like this:
    Code:
        char data;
        for (int i = 0; i < size; i++)
        {
            if (!infile.read(&data, 1))  // or if(!infile.get(&data))
            {
                cout << "EOF reached\n";
                cin.get();
                exit(99);
            }
            buff.push_back((unsigned char)data);
            cout << i << " - " << data << ", ";
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read text file char by char by using vector class
    By Ph0x in forum C++ Programming
    Replies: 8
    Last Post: 10-29-2014, 01:11 PM
  2. Read data from file and put into vector
    By optimus203 in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2012, 03:41 AM
  3. Read numbers from file to std::vector
    By Petike in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2010, 04:33 AM
  4. How to read a file stream entirely into a vector?
    By jiapei100 in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2008, 03:22 PM
  5. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 06:18 PM

Tags for this Thread