I'm trying to write a function that will read and print the contents of a file. I gave the filename as a parameter for my function. I used FILE *testfile to create a file handle and then I use fread to read the file. block_t is a struct and nreserved are the reserved segments of the block. Each block has records. I don't think that it is necessary to tell you how block_t is created.
My problem is that even though the function runs and I can see in the console the results that I want to see the process terminates. This happens even if I comment out the if else parts. I get this message Process terminated with status -1073741510
Here is my code:
#include "dbtproj.h"
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
void showEntriesOfBlock(char *filename){
FILE *testfile;
block_t block;
int nreserved;
//open file and print contents
testfile = fopen(filename,"r");
if(testfile==NULL)
cout << "Error";
else{
while(!feof(testfile)){
fread(&block, 1, sizeof(block_t), testfile);
nreserved = block.nreserved;
//print block contents
for (int i=0; i<nreserved; ++i) {
printf("this is block id: %d, record id: %d, num: %d, str: %s\n",
block.blockid, block.entries[i].recid, block.entries[i].num,
block.entries[i].str);
}
}
}
fclose(testfile);
};
In my main file I create a file by using outfile = fopen("file.bin", "w"); then I write random data to the file. Then I close the file with fclose(outfile); and in the next line I call my function like this showEntriesOfBlock("file.bin");
Can anybody help? I think that I might have messed up my pointers of did something wrong with the file handlers.
This is how I give data to my blocks and records.
for (int b=0; b<nblocks; ++b) { // for each block
block.blockid = b;
for (int r=0; r<MAX_RECORDS_PER_BLOCK; ++r) { // for each record
// prepare a record
record.recid = recid++;
record.num = rand() % 1000;
strcpy(record.str,"hello"); // put the same string to all records
record.valid = true;
memcpy(&block.entries[r], &record, sizeof(record_t)); // copy record to block
}
block.nreserved = MAX_RECORDS_PER_BLOCK;
block.valid = true;
fwrite(&block, 1, sizeof(block_t), outfile); // write the block to the file
}
fclose(outfile);
And here are the definitions of my structs:
// This is the definition of a record of the input file. Contains three fields, recid, num and str
typedef struct {
unsigned int recid;
unsigned int num;
char str[STR_LENGTH];
bool valid; // if set, then this record is valid
} record_t;
// This is the definition of a block, which contains a number of fixed-sized records
typedef struct {
unsigned int blockid;
unsigned int nreserved; // how many reserved entries
record_t entries[MAX_RECORDS_PER_BLOCK]; // array of records
bool valid; // if set, then this block is valid
unsigned char misc;
unsigned int next_blockid;
unsigned int dummy;
} block_t;
Here's a working version using FILE* (which I wouldn't recommend if you're learning...)
NOTE: open your files in binary mode : fopen(filename, "wb") or fopen(filename, "rb")
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cassert>
#include <fstream>
const int STR_LENGTH = 10;
const int MAX_RECORDS_PER_BLOCK = 5;
//! For my test I assumed the following definitions.
//! (i.e. that block_t is a POD.)
// This is the definition of a record of the input file. Contains three fields, recid, num and str
typedef struct
{
unsigned int recid;
unsigned int num;
char str[STR_LENGTH];
bool valid; // if set, then this record is valid
} record_t;
// This is the definition of a block, which contains a number of fixed-sized records
typedef struct
{
unsigned int blockid;
unsigned int nreserved; // how many reserved entries
record_t entries[MAX_RECORDS_PER_BLOCK]; // array of records
bool valid; // if set, then this block is valid
unsigned char misc;
unsigned int next_blockid;
unsigned int dummy;
} block_t;
void showEntriesOfBlock(const char *filename)
{
FILE* testfile = fopen(filename, "rb");
assert(testfile);
if (!testfile)
{
perror("Error");
return;
}
block_t block;
while(fread(reinterpret_cast<char*>(&block), sizeof(block_t), 1, testfile))
{
if (ferror(testfile))
{
perror("Error while reading");
return;
}
//print block contents
for (int i = 0; i < block.nreserved; ++i)
{
printf("this is block id: %d, record id: %d, num: %d, str: %s\n",
block.blockid, block.entries[i].recid, block.entries[i].num,
block.entries[i].str);
}
}
fclose(testfile);
};
int main(int argc, const char *argv[])
{
std::string filename = "g:/test.dat";
FILE* outfile;
outfile = fopen(filename.c_str(), "wb");
int nblocks = 10;
int recid = 0;
for (int b = 0; b < nblocks; ++b)
{
block_t block;
block.blockid = b;
for (int r = 0; r < MAX_RECORDS_PER_BLOCK; ++r)
{
// for each record
// prepare a record
record_t record;
record.recid = recid++;
record.num = rand() % 1000;
strcpy(record.str, "hello"); // put the same string to all records
record.valid = true;
memcpy(&block.entries[r], &record, sizeof(record_t)); // copy record to block
}
block.nreserved = MAX_RECORDS_PER_BLOCK;
block.valid = true;
fwrite(&block, sizeof(block_t), 1, outfile); // write the block to the file
}
fclose(outfile);
showEntriesOfBlock(filename.c_str());
return 0;
}
Try this:
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cassert>
#include <fstream>
#include <type_traits>
void showEntriesOfBlock(char *filename)
{
std::ifstream testfile(filename, std::ios_base::binary);
assert(testfile);
if (!testfile)
{
std::cout << "Error";
return;
}
block_t block;
int nreserved;
while (testfile)
{
//! This assumes block is a POD.
static_assert(std::is_pod<block_t>::value, "block_t is not a POD.");
testfile.read(reinterpret_cast<char*>(&block), sizeof(block_t));
nreserved = block.nreserved;
//print block contents
for (int i = 0; i < nreserved; ++i)
{
printf("this is block id: %d, record id: %d, num: %d, str: %s\n",
block.blockid, block.entries[i].recid, block.entries[i].num,
block.entries[i].str);
}
}
testfile.close();
};
Related
I wrote a c++ code to multithreaded copying a file to another directory in linux. but doesn't work(just it made an empty file in directory).
I don't know what's the problem? I think my tread has no right access to write in the shared file. but don't know what should I do.
It should work when typed in terminal :
$./a.out <file name> <dir> <thread number (default 4)>
This is my code:
/*Multithreads file copier*/
#include <fcntl.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <sys/sendfile.h>
#include <unistd.h>
#include <cstdio>
char* file;
char* fileout_path;
int fin, fout;
// part of each thread
struct PART{
off_t* offset =0;
size_t size;
};
//multithreading
void *Copy (void * data)
{
struct PART *mypart;
mypart = (struct PART *) data;
//open file to read and write
fin = open(file, O_RDONLY,0);
fout = open(fileout_path, O_WRONLY|O_CREAT, 0644);
unsigned long a = static_cast<unsigned long>(mypart->size);
lseek(fout, a, SEEK_SET); //set offset by size of the part
//use sendfile instead read and write to easier code
sendfile(fin, fout, mypart->offset, mypart->size);
printf("threading....\n");//to know the thread ran
pthread_exit(0);
}
int main(int argc, char *argv[])
{
int threads_number;
if (argv[3]!= NULL)
{
threads_number = atoi(argv[3]);
}
else
{
threads_number = 4;//default thread number
}
//multithreading datatypes
pthread_t tid[threads_number];
pthread_attr_t attr;
pthread_attr_init(&attr);
struct stat f_stat;
struct PART part[threads_number];
//allocation size of each part
unsigned long part_size = f_stat.st_size / threads_number;
for(int i =0; i <number_threads; i++)
{
if ( i == threads_number -1)
{
part[threads_number].size = f_stat.st_size - (part_size * (threads_number -1));
}
else
{
part[i].size = part_size;
}
}
file = argv[1];
stat(file, &f_stat);
fileout_path = argv[2];
int fin1 = open(file, O_RDONLY,0);
int fout1 = open(fileout_path, O_WRONLY|O_CREAT, 0644);
for (int j = 0; j < threads_number; j++)
{
pthread_create(&tid[j], NULL, Copy, (void *)&part[j]);
pthread_join(tid[j],NULL);
}
printf("thread is done.\n");
close(fout);
close(fin);
return 0;
}
I am trying to copy a whole text file into char array using fstream but even upon increasing the size of the array it reads the text file to same limit .i am bount to save it in a char array and it will be good if it is not a dynamic one ??? any solution please ...
// smallGrams.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include<iostream>
using namespace std;
#include<string>
#include<fstream>
void readInput(const char* Path);
void removePunctucationMarks();
void removeSpacing();
void insertDots();
char * getText();
void generateUnigrams();
void generateBigrams();
void generateTrigrams();
double validateSentance(string str);
string sentenceCreation(int position);
int main()
{
char *path="alice.txt";
readInput(path);
return 0;
}
void readInput(const char* Path)
{
ifstream infile;
infile.open(Path);
if(!infile.fail())
cout<<"File opened successfully"<<endl;
else
cout<<"File failed to open"<<endl;
int arrSize=100000000;
char *arr=new char[arrSize];
int i=0;
while(!infile.eof()&&i<arrSize)
{
infile.get(arr[i]);
i++;
}
arr[i-1]='\0';
for(short i=0;i<arrSize&&arr[i]!='\0';i++)
{
cout<<arr[i];
}
}
This is a C style solution that works. It checks the file size and then allocate the necessary memory for the array and reads all the content of the file in one call. The fread() call returns the number of bytes you requested or an error has ocurred (check fread() reference)
# include <cstring>
# include <cstdlib>
# include <cstdio>
int main(int argc, char *argv[]) {
char *data;
int data_len;
FILE *fd;
fd = fopen ("file.txt", "r");
if (fd == NULL) {
// error
return -1;
}
fseek (fd , 0 , SEEK_END);
data_len = ftell (fd);
rewind (fd);
data = (char *) malloc ((data_len + 1) * sizeof (char));
memset (data, data_len + 1, NULL);
if (fread (data, sizeof (char), data_len, fd) != data_len) {
// error
return -1;
}
printf ("%s\n", data);
fclose (fd);
free (data);
return 0;
}
Here with a simple doubling method...
#include<iostream>
#include<string>
#include<fstream>
#include <cstdint>
#include <cstring>
using namespace std;
void readInput(const char* Path)
{
ifstream infile;
infile.open(Path);
if(!infile.fail())
cout<<"File opened successfully"<<endl;
else{
cout<<"File failed to open"<<endl;
return;
}
int capacity=1000;
char *arr=new char[capacity];
char *temp;
int i=0;
while(infile >> arr[i])
{
i++;
if ( i >= capacity ) {
temp = new char[capacity*2];
std::memcpy(temp , arr, capacity);
delete [] arr;
arr = temp;
capacity *=2;
}
}
}
int main()
{
char *path="alice.txt";
readInput(path);
return 0;
}
The error could when you read and display the array content using the for loop and not on reading the data from file.
Use int instead of short in for loop, as short can increment upto 32768, only.
I am trying to get this constructor to do these set of things:
This constructor tries to open the file whose name is passed
to it in filename. If file opens successfully, calls function
getFileSize to determine how many bytes should be allocated
for the message. Allocates space for message and reads the
content from the file into it. Closes the file at the end.
Member variable length should be set to the file size.
If file cannot be found, length should be set to zero.
I am having trouble currently, when I try to run my program I get an error as it is not even reading my file and having trouble not understanding the problem. Any help would be appreciated thanks.
Constructor:
Message::Message(std::string filename) {
fstream fin(filename);
if (fin.fail())
{
cout << "failed";
}
else {
length = getFileSize(fin);
message = new char[length];
fin.getline(message, length); {
fin >> message;
}
}
fin.close();
}
.h File:
#ifndef MESSAGE_H_
#define MESSAGE_H_
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
class Message
{
private:
char *message; // holds the message
int length; // holds the the message length
static const short ALPHABET_SIZE = 26;
char code[ALPHABET_SIZE]; // holds the cypher alphabet
// iztohndbeqrkglmacsvwfuypjx
// ex: an 'a' in the original message should be converted to 'i', 'b' should be converted to 'z' and so forth
// returns the input file size in bytes
std::streamsize getFileSize(std::fstream &file) const
{
std::streamsize fsize = 0;
file.seekg(0, std::ios::end);
fsize = file.tellg();
file.seekg(0, std::ios::beg); // moves file pointer back to the beginning
return fsize;
}
public:
Message(std::string filename);
// The destructor frees the space allocated to message
virtual ~Message();
// Decodes the message
void decode();
// Capitalizes first letter in each sentence
void fixCapitalization();
// Prints the content of message on the screen
void dump() const;
// Returns true if the message is empty
bool isEmpty() const;
};
Here are my files:
OBJECT.CPP:
#include "Message.h"
using namespace std;
Message::Message(std::string filename) {
fstream fin(filename);
if (fin.fail())
{
cout << "failed";
}
else {
length = getFileSize(fin);
message = new char[length];
fin.getline(message, length); {
fin >> message;
}
}
fin.close();
}
Message::~Message()
{
//dtor
}
void Message::decode() {
int offset;
strcpy(code, "iztohndbeqrkglmacsvwfuypjx");
for (int i = 0; i < strlen(message); i++) {
if (message[i] == ' ') continue;
if (message[i] == ',') continue;
if (message[i] == '.') continue;
offset = int(message[i] - 'a');
message[i] = code[offset];
}
}
void Message::fixCapitalization() {
for (int i = 0; i < strlen(message); i++) {
if (message[0] != ' ' || message[0] != ',') {
message[0] = toupper(message[0]);
}
if (message[i] == '.' || message[i] == '?' || message[i] == ',') {
message[i + 2] = toupper(message[i + 2]);
}
}
}
void Message::dump() const {
for (int i = 0; i < strlen(message); i++) {
cout << message[i];
}
}
bool Message::isEmpty() const {
if (length == 0) {
return true;
}
else {
return false;
}
}
.H file:
/*
* Message.h
*
* Created on: Dec 11, 2016
* Author: hellenpacheco
*/
#ifndef MESSAGE_H_
#define MESSAGE_H_
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
class Message
{
private:
char *message; // holds the message
int length; // holds the the message length
static const short ALPHABET_SIZE = 26;
char code[ALPHABET_SIZE]; // holds the cypher alphabet
// iztohndbeqrkglmacsvwfuypjx
// ex: an 'a' in the original message should be converted to 'i', 'b' should be converted to 'z' and so forth
// returns the input file size in bytes
std::streamsize getFileSize(std::fstream &file) const
{
std::streamsize fsize = 0;
file.seekg(0, std::ios::end);
fsize = file.tellg();
file.seekg(0, std::ios::beg); // moves file pointer back to the beginning
return fsize;
}
public:
/*
* This constructor tries to open the file whose name is passed
* to it in filename. If file opens successfully, calls function
* getFileSize to determine how many bytes should be allocated
* for the message. Allocates space for message and reads the
* content from the file into it. Closes the file at the end.
* Member variable length should be set to the file size.
* If file cannot be found, length should be set to zero.
*/
Message(std::string filename);
// The destructor frees the space allocated to message
virtual ~Message();
// Decodes the message
void decode();
// Capitalizes first letter in each sentence
void fixCapitalization();
// Prints the content of message on the screen
void dump() const;
// Returns true if the message is empty
bool isEmpty() const;
};
#endif /* MESSAGE_H_ */
MAIN.CPP:
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include "Message.h"
using namespace std;
int main()
{
// create a message object with the content of Encrypted.txt
Message m("Encrypted.txt");
if (m.isEmpty())
{
cout << "Could not read message";
return EXIT_FAILURE;
}
cout << "Original message: " << std::endl;
m.dump();
cout << std::endl << std::endl;
m.decode();
m.fixCapitalization();
cout << "Decoded message: " << std::endl;
m.dump();
cout << std::endl << std::endl;
return EXIT_SUCCESS;
}
The following file is the .txt file I am trying to open and "decode" and is all on 1 line:
ifqkwxcadf ar cei fpoi masif cd cei xkdqirr du pxxnwafm pf pnmdkaceo cd p oirrpmi, teaqe rqkpohnir cei gpcp af ac-oplafm ac sikw gauuaqvnc pfg caoi qdfrvoafm, au fdc xkpqcaqpnnw aoxdrrahni, cd gigvqi cei dkamafpn masif dfnw cei ifqdgig gpcp. afxvcr cd cei pnmdkaceo cwxaqpnnw afsdnsi pggacadfpn riqkic gpcp qpnnig liwr, teaqe xkisifcr cei oirrpmi ukdo hiafm giqdgig-isif au cei pnmdkaceo ar xvhnaqnw lfdtf.
Problems with
message = new char[length];
fin.getline (message, length); {
fin >> message;
}
getline will stop if a newline character is encountered.
The fin >> message; line will overwrite what was read in getline.
The { and } don't make sense at all. They are not problems per se but they lead me to think that you are not clear on what you are trying to do.
I would change those lines to
message = new char[length + 1]; // Add an extra character if
// message is supposed to be null
// terminated.
fine.read(message, length);
message[length] = '\0';
I have a simple program that reads data from a PNG into a 2D array. I would like to save that data to a .RAW file so that Raw Studio or Irfanview can view the raw image that my program outputs to my_out.raw. Currently if I just write the raw binary data to the my_out.raw file, neither application can actually read the file, that is view the image. What do I need to do to the program below so that I can see the image?
The code to read the PNG files is:
// MAIN.cpp
#include "pngfilereader.h"
#include <string>
#include <vector>
#include <fstream>
int main (int argc, char *argv[])
{
PNGFileReader pngfr;
if (!pngfr.decompress_png_to_raw(std::string("/home/matt6809/Downloads"
"/City.png"))) {
std::cout << "File decompression error: " << std::endl;
} else {
std::ofstream out;
out.open("./my_out.raw", std::ios_base::out);
std::vector<std::vector<unsigned char> > data;
pngfr.get_image_data(data);
typedef std::vector<std::vector<unsigned char> >::iterator row_it;
typedef std::vector<unsigned char>::iterator col_it;
for(row_it rit= data.begin(); rit != data.end(); ++rit) {
for(col_it cit = rit->begin(); cit != rit->end(); ++cit) {
out << (*cit);
}
}
out << std::endl;
}
return 0;
}
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
#include <iostream>
#include <vector>
#include <string>
class PNGFileReader
{
public:
PNGFileReader();
~PNGFileReader();
// Public exposed API:
bool compress_raw_to_png(uint8_t data, int size);
bool decompress_png_to_raw(const std::string &path);
// Getters
long unsigned int get_image_width();
long unsigned int get_image_height();
void get_image_data(std::vector<std::vector<unsigned char> > &data);
private:
// Helper functions:
bool read_png(const std::string &path);
bool create_png_structs(FILE *fp);
bool free_data();
bool alloc_data();
// Member variables:
png_structp m_pPNG;
png_infop m_pPNGInfo;
png_infop m_pPNGEndInfo;
png_bytepp m_Data;
long unsigned int m_ImageWidth;
long unsigned int m_ImageHeight;
// Enums
enum PNGBOOL {NOT_PNG, PNG};
enum PNGERRORS {ERROR, SUCCESS};
};
#include "pngfilereader.h"
#include <stdexcept>
PNGFileReader::PNGFileReader() :
m_pPNG(NULL),
m_pPNGInfo(NULL),
m_pPNGEndInfo(NULL),
m_Data(NULL),
m_ImageWidth(0),
m_ImageHeight(0)
{
}
PNGFileReader::~PNGFileReader()
{
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
if (m_Data[i]) {
delete m_Data[i];
m_Data[i] = NULL;
}
}
if (m_Data) {
delete m_Data;
m_Data = NULL;
}
}
// Public Exposed API
bool PNGFileReader::compress_raw_to_png(uint8_t m_Data, int size)
{
return PNGFileReader::SUCCESS;
}
bool PNGFileReader::decompress_png_to_raw(const std::string &path)
{
return read_png(path);
}
// Getters
long unsigned int PNGFileReader::get_image_width()
{
return m_ImageWidth;
}
long unsigned int PNGFileReader::get_image_height()
{
return m_ImageHeight;
}
void PNGFileReader::get_image_data(
std::vector<std::vector<unsigned char> > &data)
{
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
std::vector<unsigned char> v;
data.push_back(v);
for (unsigned long int j = 0; j < m_ImageWidth; ++j) {
std::vector<unsigned char> *vp = &data[i];
vp->push_back(m_Data[i][j]);
}
}
}
// Private Methods
bool PNGFileReader::read_png(const std::string &path)
{
/*
* Open up the file to read (path) in binary mode
* first so that if anything goes wrong with libpng
* we won't have much to undo
*/
const char *c_path = path.c_str();
FILE *fp = fopen(c_path, "rb");
if (!fp)
return PNGFileReader::ERROR;
/*
* Read the first BYTES_TO_READ bytes from file
* then determine if it is a png file or
* not. If png_sig_cmp == 0 all is okay
*/
enum {BYTES_TO_READ = 8};
unsigned char sig[BYTES_TO_READ];
if (!fread(sig, 1, BYTES_TO_READ, fp)) {
fclose(fp);
return PNGFileReader::ERROR;
}
bool is_png = !png_sig_cmp(sig, 0, BYTES_TO_READ);
if (!is_png) {
fclose(fp);
return PNGFileReader::ERROR;
}
if (!this->create_png_structs(fp)) {
fclose(fp);
return PNGFileReader::ERROR;
}
/*
* For error handling purposes. Set a long pointer
* back to this function to handle all error related
* to file IO
*/
if (setjmp(png_jmpbuf(m_pPNG)))
{
png_destroy_read_struct(&m_pPNG, &m_pPNGInfo, &m_pPNGEndInfo);
fclose(fp);
return PNGFileReader::ERROR;
}
/*
* Set up the input code for FILE openend in binary mode,
* and tell libpng we have already read BYTES_TO_READ btyes from
* signature
*/
png_init_io(m_pPNG, fp);
png_set_sig_bytes(m_pPNG, BYTES_TO_READ);
/*
* Using the lowlevel interface to lib png ...
*/
png_read_info(m_pPNG, m_pPNGInfo);
m_ImageHeight = png_get_image_height(m_pPNG, m_pPNGInfo);
m_ImageWidth = png_get_rowbytes(m_pPNG, m_pPNGInfo);
this->alloc_data();
png_read_image(m_pPNG, m_Data);
png_read_end(m_pPNG, NULL);
png_destroy_read_struct(&m_pPNG, &m_pPNGInfo, &m_pPNGEndInfo);
fclose(fp);
return PNGFileReader::SUCCESS;
}
bool PNGFileReader::create_png_structs(FILE *fp)
{
/*
* Create the pointer to main libpng struct, as well as
* two info structs to maintain information after, and
* prior to all operations on png m_Data. Only necessary
* to release resource after function succeeds.
*/
m_pPNG = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
NULL, NULL);
if (!m_pPNG)
{
fclose(fp);
return PNGFileReader::ERROR;
}
m_pPNGInfo = png_create_info_struct(m_pPNG);
if (!m_pPNGInfo)
{
png_destroy_read_struct(&m_pPNG, (png_infopp)NULL,(png_infopp)NULL);
fclose(fp);
return PNGFileReader::ERROR;
}
m_pPNGEndInfo = png_create_info_struct(m_pPNG);
if (!m_pPNGEndInfo)
{
png_destroy_read_struct(&m_pPNG, &m_pPNGInfo, (png_infopp)NULL);
fclose(fp);
return PNGFileReader::ERROR;
}
return PNGFileReader::SUCCESS;
}
bool PNGFileReader::free_data()
{
if (m_ImageHeight == 0 || m_ImageWidth == 0)
return PNGFileReader::ERROR;
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
if (m_Data[i]) {
delete m_Data[i];
m_Data[i] = NULL;
}
}
if (m_Data) {
delete m_Data;
m_Data = NULL;
}
return PNGFileReader::SUCCESS;
}
bool PNGFileReader::alloc_data()
{
if (m_ImageHeight == 0 || m_ImageWidth == 0)
return PNGFileReader::ERROR;
if (m_Data != NULL)
this->free_data();
m_Data = new png_bytep[m_ImageHeight]();
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
m_Data[i] = NULL;
}
try {
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
m_Data[i] = new png_byte[m_ImageWidth];
}
}
catch (std::bad_alloc e) {
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
if (m_Data[i]) {
delete m_Data[i];
m_Data[i] = NULL;
}
}
if (m_Data) {
delete m_Data;
m_Data = NULL;
}
throw e;
}
return PNGFileReader::SUCCESS;
}
A "raw" file that is intended to be used with a camera-image processing program like Raw Studio and Irfraview is not a raw-binary dump of the image-data with no header. Instead the "raw" moniker refers to the fact that the image has a minimal amount of image-processing applied in-camera. For instance, the image-data may still be a single-channel monochrome image from the camera's bayer-pattern CFA, or no white-balance, color-matrix, etc. has been applied, etc. Either way, the image-data is still formatted in a standard binary image file format complete with a header, data-packing method, etc. Examples include formats such as Adobe's DNG file format (which is based on TIFF), or proprietary formats from camera manufacturer's themselves such as Canon's CR2, Nikon's NEF, etc.
So if you want these raw-file processing programs to read your "raw" file image data, you'll have to read-up on the binary data specifications the raw-file formats they support, and then re-format the original PNG image-data correctly.
I've written a program that uses a vector and a map.
When i run it, i get this following error message:
lru: malloc.c:3552: munmap_chunk: Assertion `ret == 0' failed.
Abort
What is the meaning of this error message?
P.S.
When i run my program with valgrind - it passes, with no 'abort'.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <map>
#include "byutr.h"
using namespace std;
///////////////////////////////////////////
/* DEFINE ZONE */
///////////////////////////////////////////
#define NUM_OF_ARGS 4
#define NUM_OF_DIFF_PAGES 100000
///////////////////////////////////////////
/* GLOBAL VARIABLES */
///////////////////////////////////////////
p2AddrTr tr;//a pre-defined struct
vector<uint32_t> stack;
vector<int> depths;
map<uint32_t, int> pages;
map<uint32_t, int>::iterator it;
int main(int argc, char **argv)
{
stack.reserve(NUM_OF_DIFF_PAGES);
FILE *ifp;//TODO remove!
// unsigned long i;//TODO int OR unsigned long??
int i;
unsigned long pCnt =0;
if(argc != NUM_OF_ARGS)
{
fprintf(stderr,"usage: lru <pageSize> <startAt> <numAccesses>\n");
exit(1);
}
int pageSize = atoi(argv[1]);
int startAt = atoi(argv[2]);
int numAccesses = atoi(argv[3]);
int k;
//Skip some entries if needed
for(k=0;k< startAt;k++){
fread(&tr, sizeof(p2AddrTr), 1, stdin);
}
//size_t bytes = fread(&tr, sizeof(p2AddrTr),1, stdin);
//fread(&tr, sizeof(p2AddrTr),1, stdin); TODO here??
i = 0;
while((!feof(stdin)) && (i<numAccesses)){
fread(&tr, sizeof(p2AddrTr),1, stdin);
//prints the address of the memory access
printf("%08lx ", tr.addr);
cout<<endl;
int currAddr = (tr.addr)/pageSize;
if(pages.find(currAddr) == pages.end()){//New page
pCnt++;
//insert the new page to the map
pages.insert(pair<uint32_t, int>(currAddr,pCnt));
//insert the new page to the 'stack'
stack.push_back(currAddr);
}
else{//page already exists
size_t j;
//find the page in the stack
for(j=0;j<stack.size();j++){
if(stack[j] == currAddr){
cout << "passed stack[j]"<<endl;
depths.push_back(stack.size() - j);
break;
}
}
//move the page to the top of the stack
stack.erase(stack.begin() + (j-1));
stack.push_back(currAddr);
}
i++;
}
return (0);
}
I see at least one error:
stack.erase(stack.begin() + (j-1));
If j is 0, this tries to erase an element before the beginning of the list, resulting in a crash.