I must be missing something simple here, but I am trying to write and read a binary file in C++.
ofstream file3("C:\\data2.txt", ios::out | ios::binary);
for (int i = 0; i < 100; i++) {
file3.write((char*)(&i), sizeof(int));
}
ifstream file4("C:\\data2.txt", ios::in | ios::binary);
int temp;
while (!file4.eof()) {
file4.read((char*)(&temp), sizeof(int));
cout << temp << endl;
}
The file looks like it is getting created properly when viewed with a hex editor. However, as I go to read the file it reads 1 random junk value and quits vs. listing out all the numbers.
Update
I made a slight update based on the comments and all seems good now. On Windows the close made a difference and I did fix the loop condition.
ofstream file3("C:\\data2.txt", ios::out | ios::binary);
for (int i = 0; i < 100; i++) {
file3.write((char*)(&i), sizeof(int));
}
file3.close();
ifstream file4("C:\\data2.txt", ios::in | ios::binary);
//cout << file4.eof();
int temp;
while (!file4.read((char*)(&temp), sizeof(int)).eof()) {
cout << temp << endl;
}
You might not have permission to write to c:\\file, so you should check if you can. As for using .eof() see this topic. Finally, you might want to close the file before you open it again for reading. Here is your example tweaked:
#include<iostream>
#include<fstream>
int main()
{
std::ofstream file3("data2.txt", std::ios::binary);
if (file3)
{
for (int i = 0; i < 100; i++)
{
file3.write((char*)(&i), sizeof(int));
}
file3.close();
}
std::ifstream file4("data2.txt", std::ios::binary);
int temp;
while (file4)
{
file4.read((char*)(&temp), sizeof(int));
std::cout << temp << std::endl;
}
}
Demo: http://coliru.stacked-crooked.com/view?id=8f519fcd05879855
Related
I am attempting to read numbers from a text file into a program, but for some reason, the program isn't reading the file. Here is my code:
#include <iostream>
#include <stream>
using namespace std;
int main()
{
ifstream infile;
infile.open ("adventDay1.txt");
if (!infile) { //Check if file is opening
cerr << "Error!"<< endl;
return 0;
}
int dataSize = 0;
infile >> dataSize;
int* arr;
arr = new int[dataSize]; //dynamically allocated array
int measureCount = 0; //Keep track of input from file
for (int i = 0; i < dataSize; i++) {
// infile >> dataSize;
arr[i] = dataSize;
measureCount += 1;
}
cout << measureCount << endl;
delete[] arr; //Delete dynamically allocated memory
return 0;
}
Each time I run it, it just displays the "Error!" message I added. There are 2,000 numbers in the text file, so that should be the expected output based on what I have here. I can't pinpoint the mistake.
Include fstream and ensure that you are opening the file in read mode. Perhaps also define it as ifstream infile("adventDay1.txt")
I've been doing my project and the last thing I need to do is to save to and start reading a structure array from a file on startup of the program, but I can't figure out why the code isn't loading the information of the file. Earlier I asked a similar question and got referred to serialization, which didn't work and I decided to turn my strings into chars and change the functions to work with chars, since I thought that I might be serializing wrong, considering the fact that I first learned how to do it today, unfortunately the program still doesn't read, even though it writes in the file. My style is pretty bad, so I apologize in advance. Here's the code sample only of the struct fread and fwrite with the main:
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
struct property {
int num;
char nBrok[50];
char type[10];
char adress [70];
char outlook[20];
double price;
double size;
int nRooms;
int floor;
int status;
};
fstream fp;
void fileWrite(property bDanni[], int n) {
fp.open("dbase.dat", ios::binary | ios::out);
if (!fp) {
cout << "\n Error in file \n"; exit(1);
}
fp.write((char*)bDanni, sizeof(property) *n);
fp.close();
}
int fileRead(property bDanni[]) {
long pos; int n = 0, i; property b;
fp.open("dbase.dat", ios::binary | ios::in);
if (!fp) {
cout << "\n file does not exist\n"; return n;
}
fp.seekg(0l, ios::end);
pos = fp.tellg();
fp.close();
n = pos / (sizeof(property));
fp.open("dbase.dat", ios::binary | ios::in);
if (!fp) {
cout << "\n Error in file \n"; exit(1);
}
for (i = 0; i < n; i++) {
fp.read((char*)&b, sizeof(property));
bDanni[i] = b;
}
fp.close();
return n;
}
int main() {
property bDanni[100];
char answer;
int total = 0;
cout << "Do you wat to read from the save file?(y/n): ";
cin >> answer;
if (answer == 'y') {
int total = fileRead(bDanni);
}
}
The code posted works, the issue was the way the main loaded the function.
I am trying to run below code but it is neither showing any file on the path nor reading anything from it. Whatever I am writing into the file through "cin >>" it is not being written. Can anybody please let me know mistake in my code below:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char string[80];
cout << "Enter Input" << endl;
cin >> string;
int len = strlen(string);
fstream file;
file.open("TEXT", ios::in | ios::out);
for (int i = 0; i < len; i++)
file.put(string[i]);
file.close();
file.open("TEXT", ios::in | ios::out);
file.seekg(0);
cout << "Output" << endl;
while (file) {
char ch;
file.get(ch);
cout << ch;
}
file.close();
return 0;
}
You should add the fstream::app flag in your open call and this will do the trick !
Don't mess with file modes you don't need (or understand). Open the file for writing only first, and then open it for reading only next. Use different streams for reading and writing.
ofstream file_out;
file_out.open("TEXT");
for (int i = 0; i < len; i++)
file_out.put(string[i]);
file_out.close();
ifstream file_in;
file_in.open("TEXT");
cout << "Output" << endl;
while (file_in) {
char ch;
file_in.get(ch);
cout << ch;
}
Unless you actually understand the rules concerning ios::in and ios::out it's safer to just use ifstream when you want input and ofstream when you want output.
More reading if you do want to understand the rules.
just want to ask if anyone knows why I cant convert an entire picture into decimal.
Problem: after about 180 couts it turns 0 for the rest of the pixels.
Code:
#include <fstream>
#include <iostream>
int main() {
char unsigned charTemp = 0;
int unsigned intTemp = 0;
std::fstream file;
file.open("PIC.pbm", std::ios::in);
//Ignore header
for (int i = 0; i < 13; i++) {
file.ignore();
}
//read and print
//img res is 40x40 = 1600
for (int i = 0; i < 1600; i++) {
file >> charTemp;
intTemp = charTemp;
std::cout << intTemp << " ";
charTemp = 0;
intTemp = 0;
}
std::cout << "\n\n\n";
system("pause");
return 0;
}
Pic: any 40x40 pbm
A better method with picture and image files is to read them as binary files:
std::ifstream file("PIC.pbm", ios::binary);
std::vector<unsigned char> bitmap(1600);
// Skip over the header
file.seekg(13, ios::beg); // Skip over 13 bytes.
// Read in the data at once
file.read((char *) &bitmap[0], 1600);
// Now process the bitmap from memory
for (int i = 0; i < 1600; ++i)
{
cout << static_cast<unsigned int>(bitmap[i]) << " ";
if ((i % 40) == 39)
{
cout << "\n";
}
}
cout << "\n";
The idea here is to read in the bitmap in one transaction into memory. Streams like to flow (don't interrupt the flow). Memory is faster to access than files, so the bitmap values are processed from memory.
The cast is used so that the formatted insertion doesn't treat the byte as a character, but a number.
First, open your PIC.pbm file in another hex editor because it's quite possible that those bytes really are zeroes. If not, then you've got problems reading the file.
The fstream constructor does not default to reading in binary mode, so it reads files as "text" - and I've learned the hard way that you can't trust the standard-library with knowing anything about text anymore (what with mishandling Unicode, line-endings, etc - I feel it's best to always use binary and a dedicated Unicode library).
You should check the fstream::good() function after each read operation to see if it failed, and if so, then check iostate:
using namespace std;
// ...
fstream file;
file.open( "PIC.pbm", ios::in | ios::binary );
file.ignore( 13 );
for (int i = 0; i < 1600; i++) {
file >> charTemp;
if( !file.good() ) {
cout << endl;
cout << "Error reading file: iostate == " << file.iostate << endl;
break;
}
else {
intTemp = charTemp;
std::cout << intTemp << " ";
charTemp = 0;
intTemp = 0;
}
}
I cannot find the problem in my code. readFile function works well, but writeFile function does not make any changes in the file:
#include <iostream>
#include <fstream>
using namespace std;
const int BUF_SIZE = 1024;
void readFile(fstream &file, char buffer[BUF_SIZE]);
void writeFile(fstream &file);
void readFile(fstream &file, char buffer[BUF_SIZE])
{
int position;
cout << "Please enter a position to read from the file some info" << endl;
cin >> position;
file.seekg(position, ios::beg);
file.read((char*) buffer, BUF_SIZE); // <<<<<
for(int i = 0; i < file.gcount(); i++){
cout << buffer[i];
}
}
void writeFile(fstream &file)
{
char temp[100] = "HHHH";
//cout << "Please enter some info to add to the file" << endl;
file.write((char*) &temp, 100);
for(int i = 0; i < file.gcount(); i++){
cout << temp[i];
}
}
int main(int argc, char const *argv[])
{
char buffer[BUF_SIZE];
if (argc != 2){
cout << "Program usage: prog_name file_name";
return 1;
}
fstream file(argv[1], ios::in | ios::out | ios::binary | ios::app);
if (!file){
cout << "File can not open or doesn't exist";
return 1;
}
//Try to read & write some info from/to file in particular position
readFile(file, buffer);
writeFile(file);
file.close();
return 0;
}
When I create a new ostream it works well, but I want to understand why fstream in/out mode works in my code only for reading.
I see several problems:
The reason behind the writing problem is probably because you reach the end of the file (is the file smaller than BUF_SIZE bytes?). This sets the EOF bit, which makes any write operations to fail. You have to clear that bit before (use the std::fstream::clear method):
void readFile(fstream &file, char buffer[BUF_SIZE])
{
int position;
cout << "Please enter a position to read from the file some info" << endl;
cin >> position;
file.seekg(position, ios::beg);
file.read(buffer, BUF_SIZE);
for(int i = 0; i < file.gcount(); i++){
cout << buffer[i];
}
file.clear(); // clears EOF
}
The line file.write((char*) &temp, 100); is wrong since you are actually passing a point to the temp variable, which is also a pointer, but it is camouflaged by the cast. These ones are OK: file.write(temp, 100); or file.write(&temp[0], 100);
When printing the written characters, you are using std::fstream::gcount, which literally means get count (amount of characters read in the last get operation). You are writing (put) not reading (get). Indeed, you are actually indicating how many bytes you are willing to write, so use it:
file.write(temp, 100);
for(int i = 0; i < 100; i++){
cout << temp[i];
}
Finally, you are always writing 100 characters, probably including some garbage from the buffer. As I see that you want to let the user choose what to write (the commented line), you can instead:
const size_t size = strlen(temp);
file.write(temp, size);
for(size_t i = 0; i < size; i++){
cout << temp[i];
}
In addition, some suggestions:
Use a std::string to read the user input, in this way you avoid a possible buffer overflow (if the user enters more than 100 characters).
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // read the link bel
string temp;
getline(cin, temp); // need #include <string>
file.write(temp.c_str(), temp.size());
You will probably want to read this answer to learn more about the first line (basically it avoids the getline to be skipped after using cin >> position).
Avoid the for loop to print the user input. For both the buffer and the std::string options you can just cout << temp << endl;.