Write PGM function overwritting existing file.
up vote
0
down vote
favorite
Im having problems understanding how to write a PGM file using data.
This function i have overwrites the existing txt document instead of creating a PGM file.
Im unsure whether my issue is lies within my Read_text function or somewhere else. Can anyone help me understand this issue and fix it please?
double* read_text(char *fileName, int sizeR, int sizeC)
{
double* data = new double[sizeR*sizeC];
int i = 0;
std::ifstream myfile(fileName);
if (myfile.is_open())
{
while (myfile.good())
{
if (i>sizeR*sizeC - 1) break;
myfile >> *(data + i);
std::cout << *(data+i) << ' '; //This line display the converted data on the screen, you may comment it out.
i++;
}
myfile.close();
}
else std::cout << "Unable to open file";
std::cout << i;
return data;
}
void write_pgm(char *filename, double *data, int sizeR, int sizeC, int Q)
{
int i, j;
unsigned char *image;
std::ofstream myfile;
image = (unsigned char *) new unsigned char[sizeR*sizeC];
// convert the integer values to unsigned char
for (i = 0; i<sizeR*sizeC; i++)
image[i] = (unsigned char)data[i];
myfile.open(filename, std::ios::out | std::ios::binary | std::ios::trunc);
if (!myfile) {
std::cout << "Can't open file: " << filename << std::endl;
exit(1);
}
myfile << "P5" << std::endl;
myfile << sizeC << " " << sizeR << std::endl;
myfile << Q << std::endl;
myfile.write(reinterpret_cast<char *>(image), (sizeR*sizeC)*sizeof(unsigned char));
if (myfile.fail()) {
std::cout << "Can't write image " << filename << std::endl;
exit(0);
}
myfile.close();
std::cout << "Finsihed " << std::endl;
delete image;
}
void main() {
double* data = read_text("Wally_grey.txt", rows(), columns());
std::cout << data << std::endl;
write_pgm("Wally_grey.txt",data, rows(), columns(), 255);
getchar();
}
c++ pgm
add a comment |
up vote
0
down vote
favorite
Im having problems understanding how to write a PGM file using data.
This function i have overwrites the existing txt document instead of creating a PGM file.
Im unsure whether my issue is lies within my Read_text function or somewhere else. Can anyone help me understand this issue and fix it please?
double* read_text(char *fileName, int sizeR, int sizeC)
{
double* data = new double[sizeR*sizeC];
int i = 0;
std::ifstream myfile(fileName);
if (myfile.is_open())
{
while (myfile.good())
{
if (i>sizeR*sizeC - 1) break;
myfile >> *(data + i);
std::cout << *(data+i) << ' '; //This line display the converted data on the screen, you may comment it out.
i++;
}
myfile.close();
}
else std::cout << "Unable to open file";
std::cout << i;
return data;
}
void write_pgm(char *filename, double *data, int sizeR, int sizeC, int Q)
{
int i, j;
unsigned char *image;
std::ofstream myfile;
image = (unsigned char *) new unsigned char[sizeR*sizeC];
// convert the integer values to unsigned char
for (i = 0; i<sizeR*sizeC; i++)
image[i] = (unsigned char)data[i];
myfile.open(filename, std::ios::out | std::ios::binary | std::ios::trunc);
if (!myfile) {
std::cout << "Can't open file: " << filename << std::endl;
exit(1);
}
myfile << "P5" << std::endl;
myfile << sizeC << " " << sizeR << std::endl;
myfile << Q << std::endl;
myfile.write(reinterpret_cast<char *>(image), (sizeR*sizeC)*sizeof(unsigned char));
if (myfile.fail()) {
std::cout << "Can't write image " << filename << std::endl;
exit(0);
}
myfile.close();
std::cout << "Finsihed " << std::endl;
delete image;
}
void main() {
double* data = read_text("Wally_grey.txt", rows(), columns());
std::cout << data << std::endl;
write_pgm("Wally_grey.txt",data, rows(), columns(), 255);
getchar();
}
c++ pgm
Of course it overwrites the input file. That's what you told it to do when you used the same name for the input and output file. What did you expect?
– Jesper Juhl
Nov 11 at 12:37
I'm honestly so new to C++. Where do I use the same name for the output?
– Craig
Nov 11 at 12:40
read_text("Wally_grey.txt" ...
- your input file.write_pgm("Wally_grey.txt" ...
- your output file. Notice any similarities?
– Jesper Juhl
Nov 11 at 12:42
I'm an idiot ...
– Craig
Nov 11 at 12:43
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Im having problems understanding how to write a PGM file using data.
This function i have overwrites the existing txt document instead of creating a PGM file.
Im unsure whether my issue is lies within my Read_text function or somewhere else. Can anyone help me understand this issue and fix it please?
double* read_text(char *fileName, int sizeR, int sizeC)
{
double* data = new double[sizeR*sizeC];
int i = 0;
std::ifstream myfile(fileName);
if (myfile.is_open())
{
while (myfile.good())
{
if (i>sizeR*sizeC - 1) break;
myfile >> *(data + i);
std::cout << *(data+i) << ' '; //This line display the converted data on the screen, you may comment it out.
i++;
}
myfile.close();
}
else std::cout << "Unable to open file";
std::cout << i;
return data;
}
void write_pgm(char *filename, double *data, int sizeR, int sizeC, int Q)
{
int i, j;
unsigned char *image;
std::ofstream myfile;
image = (unsigned char *) new unsigned char[sizeR*sizeC];
// convert the integer values to unsigned char
for (i = 0; i<sizeR*sizeC; i++)
image[i] = (unsigned char)data[i];
myfile.open(filename, std::ios::out | std::ios::binary | std::ios::trunc);
if (!myfile) {
std::cout << "Can't open file: " << filename << std::endl;
exit(1);
}
myfile << "P5" << std::endl;
myfile << sizeC << " " << sizeR << std::endl;
myfile << Q << std::endl;
myfile.write(reinterpret_cast<char *>(image), (sizeR*sizeC)*sizeof(unsigned char));
if (myfile.fail()) {
std::cout << "Can't write image " << filename << std::endl;
exit(0);
}
myfile.close();
std::cout << "Finsihed " << std::endl;
delete image;
}
void main() {
double* data = read_text("Wally_grey.txt", rows(), columns());
std::cout << data << std::endl;
write_pgm("Wally_grey.txt",data, rows(), columns(), 255);
getchar();
}
c++ pgm
Im having problems understanding how to write a PGM file using data.
This function i have overwrites the existing txt document instead of creating a PGM file.
Im unsure whether my issue is lies within my Read_text function or somewhere else. Can anyone help me understand this issue and fix it please?
double* read_text(char *fileName, int sizeR, int sizeC)
{
double* data = new double[sizeR*sizeC];
int i = 0;
std::ifstream myfile(fileName);
if (myfile.is_open())
{
while (myfile.good())
{
if (i>sizeR*sizeC - 1) break;
myfile >> *(data + i);
std::cout << *(data+i) << ' '; //This line display the converted data on the screen, you may comment it out.
i++;
}
myfile.close();
}
else std::cout << "Unable to open file";
std::cout << i;
return data;
}
void write_pgm(char *filename, double *data, int sizeR, int sizeC, int Q)
{
int i, j;
unsigned char *image;
std::ofstream myfile;
image = (unsigned char *) new unsigned char[sizeR*sizeC];
// convert the integer values to unsigned char
for (i = 0; i<sizeR*sizeC; i++)
image[i] = (unsigned char)data[i];
myfile.open(filename, std::ios::out | std::ios::binary | std::ios::trunc);
if (!myfile) {
std::cout << "Can't open file: " << filename << std::endl;
exit(1);
}
myfile << "P5" << std::endl;
myfile << sizeC << " " << sizeR << std::endl;
myfile << Q << std::endl;
myfile.write(reinterpret_cast<char *>(image), (sizeR*sizeC)*sizeof(unsigned char));
if (myfile.fail()) {
std::cout << "Can't write image " << filename << std::endl;
exit(0);
}
myfile.close();
std::cout << "Finsihed " << std::endl;
delete image;
}
void main() {
double* data = read_text("Wally_grey.txt", rows(), columns());
std::cout << data << std::endl;
write_pgm("Wally_grey.txt",data, rows(), columns(), 255);
getchar();
}
c++ pgm
c++ pgm
asked Nov 11 at 12:32
Craig
115
115
Of course it overwrites the input file. That's what you told it to do when you used the same name for the input and output file. What did you expect?
– Jesper Juhl
Nov 11 at 12:37
I'm honestly so new to C++. Where do I use the same name for the output?
– Craig
Nov 11 at 12:40
read_text("Wally_grey.txt" ...
- your input file.write_pgm("Wally_grey.txt" ...
- your output file. Notice any similarities?
– Jesper Juhl
Nov 11 at 12:42
I'm an idiot ...
– Craig
Nov 11 at 12:43
add a comment |
Of course it overwrites the input file. That's what you told it to do when you used the same name for the input and output file. What did you expect?
– Jesper Juhl
Nov 11 at 12:37
I'm honestly so new to C++. Where do I use the same name for the output?
– Craig
Nov 11 at 12:40
read_text("Wally_grey.txt" ...
- your input file.write_pgm("Wally_grey.txt" ...
- your output file. Notice any similarities?
– Jesper Juhl
Nov 11 at 12:42
I'm an idiot ...
– Craig
Nov 11 at 12:43
Of course it overwrites the input file. That's what you told it to do when you used the same name for the input and output file. What did you expect?
– Jesper Juhl
Nov 11 at 12:37
Of course it overwrites the input file. That's what you told it to do when you used the same name for the input and output file. What did you expect?
– Jesper Juhl
Nov 11 at 12:37
I'm honestly so new to C++. Where do I use the same name for the output?
– Craig
Nov 11 at 12:40
I'm honestly so new to C++. Where do I use the same name for the output?
– Craig
Nov 11 at 12:40
read_text("Wally_grey.txt" ...
- your input file. write_pgm("Wally_grey.txt" ...
- your output file. Notice any similarities?– Jesper Juhl
Nov 11 at 12:42
read_text("Wally_grey.txt" ...
- your input file. write_pgm("Wally_grey.txt" ...
- your output file. Notice any similarities?– Jesper Juhl
Nov 11 at 12:42
I'm an idiot ...
– Craig
Nov 11 at 12:43
I'm an idiot ...
– Craig
Nov 11 at 12:43
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248795%2fwrite-pgm-function-overwritting-existing-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Of course it overwrites the input file. That's what you told it to do when you used the same name for the input and output file. What did you expect?
– Jesper Juhl
Nov 11 at 12:37
I'm honestly so new to C++. Where do I use the same name for the output?
– Craig
Nov 11 at 12:40
read_text("Wally_grey.txt" ...
- your input file.write_pgm("Wally_grey.txt" ...
- your output file. Notice any similarities?– Jesper Juhl
Nov 11 at 12:42
I'm an idiot ...
– Craig
Nov 11 at 12:43