-4

part of my code which is working with Visual C++ is below. data in bin file looks like ff a0 b0 c0 a0 b0 c0 d0

#include<fstream>
#include<vector>
main()
{
    std::ifstream file;
    file.open("file.bin", std::ios::binary);
    vector<uint32_t> data;
    uint32_t temp;
    // now read the data in uint32_t format
    while(file.read(reinterpret_cast<char *>(&temp), sizeof(temp)))
    {
        data.push_back(temp);
    }
    file.close();
}
G T
  • 11
  • 2
  • 7
    And what exactly is the problem? – PhilMasteG Jun 20 '18 at 14:04
  • 3
    You should check for errors when you call functions like `file.open()` – Galik Jun 20 '18 at 14:05
  • able to read the bin file correctly in windows, but if i try same code in GCC it give end of file without reading any data. – G T Jun 20 '18 at 14:06
  • @Galik i'm not getting any error messages/compile errors, it's working fine in windows though. – G T Jun 20 '18 at 14:07
  • `vector` -> `std::vector`, `main()` -> `int main()`. – Paul R Jun 20 '18 at 14:07
  • 4
    My crystal ball tells me it's a working directory problem... – Paul R Jun 20 '18 at 14:08
  • 1
    *"data in bin file looks like ff a0 b0 c0 a0 b0 c0 d0"* - in what? In a text editor? When you dump out the binary values into hexadecimal notation? How are you viewing that data? – Galik Jun 20 '18 at 14:08
  • 1
    You are not checking for errors, that's why you are not finding any. When you do `file.open()` that can fail. You should check for that failure. – Galik Jun 20 '18 at 14:09
  • @Paul R As mentioned in the description, it's just a part of my code, i'm trying to understand what is the missing factor which makes my code fail only with GCC compiler. – G T Jun 20 '18 at 14:09
  • 1
    You need to provide a proper [mcve], not just a random approximation of your code that doesn't even compile. You also need to tell us how you're building the executable, and how you are running it (in particular the working directory and where the data file is located relative to the working directory). – Paul R Jun 20 '18 at 14:10
  • After opening the file try adding `if(!file.is_open()) std::cout << std::strerror(errno) << '\n';` see what that says. – Galik Jun 20 '18 at 14:11
  • 1
    Related: https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – UKMonkey Jun 20 '18 at 14:11

1 Answers1

1

Probably your code fails because the file file.bin does not exist in the working directory which depends on your development platform.

Try this (this code compiles unlike your code in the question):

#include <fstream>
#include <iostream>
#include <vector>

int main()
{
  std::ifstream file;
  file.open("file.bin", std::ios::binary);

  if (file.fail())  // check if file could actually be opened
  {
    // you end up here if the file does not exist
    std::cout << "File could not be opened. Check if it exists.";
    return 1;
  }

  std::vector<uint32_t> data;
  uint32_t temp;
  // now read the data in uint32_t format
  while (file.read(reinterpret_cast<char *>(&temp), sizeof(temp)))
  {
    data.push_back(temp);
  }

  file.close();

  for (auto value : data)
    std::cout << value << "\n";      
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115