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();
}









share|improve this question






















  • 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















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();
}









share|improve this question






















  • 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













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();
}









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine