This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I've created a file handler function for an assignment which accesses a binary file, takes the int data from the file and passes it to an array.
The code is below. The problem I have is with the line "numElements = inFile.tellg() / sizeof(int);". When I compile the program, I'm getting a warning:
warning C4267: '=': conversion from 'size_t' to 'int', possible loss of data
Is there anyone who can point me the way to a solution? the code still compiles and the function runs correctly, but I would like to resolve this issue.
void fileHandler(ifstream &inFile, string &filename, int &numElements, int *&A)
{
inFile.open(filename, ios::binary);
if(inFile.is_open()){
inFile.seekg(0, ios::end);
numElements = inFile.tellg() / sizeof(int);
inFile.seekg(0, ios::beg);
A = new int[numElements];
inFile.read((char *)A, numElements * sizeof(int));
inFile.close();
} else{
cerr << "Error opening file. Exiting program." << endl;
exit(0);
}
}
Subreddit
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...