Inverting bits of PBM image while vs for loopHow do you set, clear, and toggle a single bit?ctags does not parse stdio.h properlyWhy are elementwise additions much faster in separate loops than in a combined loop?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsWhy does this pbm image behaves weirdlyopening a jpeg or png image as pixel data in c or c++can't change all pixel in image Mat opencvCreating a chess board with variable square sizePreprocessing images for OCR with Tesseract: Distinguishing between black on white and white on black text
Mathematically modelling RC circuit with a linear input
Why does this compile? Returning nullptr as std::string
How did Aragorn, Legolas, and Gimli know what Uruk-hai were?
I just entered the USA without passport control at Atlanta airport
Short story: Prisoner on alien planet escapes by making up a story about ghost companions and wins the war
(Familier) (Populaire) (Argot) - what's the difference?
Umlaut character order when sorting
Print one file per line using echo
Why do you need to heat the pan before heating the olive oil?
Teferi's Time Twist and Gideon's Sacrifice
What is the highest voltage from the power supply a Raspberry Pi 3 B can handle without getting damaged?
Why isn't my calculation that we should be able to see the sun well beyond the observable universe valid?
How could a tree survive a volcanic blast?
Understanding the classification of quantum states based on partial transposition: representations of the bipartite density matrix
80s or 90s Fantasy novel, part of series. Castle talks to wizard, 2 headed dragon fights itself
Can you use one creature for both convoke and delve for Hogaak?
Non-misogynistic way to say “asshole”?
In the US, can a former president run again?
Is it possible to transpose samples (in cents) from minor to major?
How could empty set be unique if it could be vacuously false
What are the pros and cons for the two possible "gear directions" when parking the car on a hill?
Traversing Latin America & Caribbean: A Cryptic Journey
Where should a runway for a spaceplane be located?
How do internally carried IR missiles acquire a lock?
Inverting bits of PBM image while vs for loop
How do you set, clear, and toggle a single bit?ctags does not parse stdio.h properlyWhy are elementwise additions much faster in separate loops than in a combined loop?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsWhy does this pbm image behaves weirdlyopening a jpeg or png image as pixel data in c or c++can't change all pixel in image Mat opencvCreating a chess board with variable square sizePreprocessing images for OCR with Tesseract: Distinguishing between black on white and white on black text
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to flip the color of pixels of a simple pbm image which has only pure black and pure white. I am generating the image myself and then reading it and then flipping the bits and saving both the generated image and the color inverted image.
Here is my code (write_pbm.cpp) -
#include "headers/write_pbm.h"
int width_pbm, height_pbm;
void input_sample_dim()
printf("Enter the width and height of image to create = ");
scanf("%d %d", &width_pbm, &height_pbm);
void create_sample_image()
FILE *fp;
fp = fopen("sample.pbm", "wb");
fprintf(fp, "P1n");
fprintf(fp, "# myfile.pbmn");
fprintf(fp, "%d %dn", width_pbm, height_pbm);
for (int i = 1; i <= height_pbm; i++)
for (int j = 1; j <= width_pbm; j++)
(width_pbm - j + 1 == i))
fprintf(fp, "0");
else
fprintf(fp, "1");
if (j == width_pbm)
fprintf(fp, "n");
else
fprintf(fp, " ");
fclose(fp);
void invert ()
printf("tinverting the imagenturning black pixels white and white pixels blackn");
FILE *fp = fopen("sample.pbm", "rb");
while(fgetc(fp) != 'n');
while(fgetc(fp) != 'n');
while(fgetc(fp) != 'n');
FILE *fp_inv;
fp_inv = fopen("inverted.pbm", "wb");
fprintf(fp_inv, "P1n");
fprintf(fp_inv, "# inverted.pbmn");
fprintf(fp_inv, "%d %dn", width_pbm, height_pbm);
for (int i = 1; i <= height_pbm; i++)
for (int j = 1; j <= width_pbm; j++)
char ch = fgetc(fp);
if (ch == '1')
fputc('0', fp_inv);
else if (ch == '0')
fputc('1', fp_inv);
else
fputc(ch, fp_inv);
fclose(fp);
fclose(fp_inv);
Below is the header I am including (write_pbm.h)
#ifndef Write_PBM_H
#define Write_PBM_H
#include <cstdio>
#include <iostream>
void create_sample_image(void);
void input_sample_dim(void);
void invert (void);
extern int width_pbm, height_pbm;
#endif
Below is my main -
#include "write/PBM/headers/write_pbm.h"
int main()
input_sample_dim();
printf("writing sample imagen");
create_sample_image();
printf("sample image with dimenstions %d by %d createdn", width_pbm, height_pbm);
invert();
So I am making a V cross kind of pattern and then inverting the colors and saving both of the created image and the inverted image.
Lets suppose we provide input 10 10
then the file sample.pbm looks like
P1
# myfile.pbm
10 10
0 1 1 1 1 1 1 1 1 0
1 0 1 1 1 1 1 1 0 1
1 1 0 1 1 1 1 0 1 1
1 1 1 0 1 1 0 1 1 1
1 1 1 1 0 0 1 1 1 1
1 1 1 1 0 0 1 1 1 1
1 1 1 0 1 1 0 1 1 1
1 1 0 1 1 1 1 0 1 1
1 0 1 1 1 1 1 1 0 1
0 1 1 1 1 1 1 1 1 0
and inverted.pbm looks like this
P1
# inverted.pbm
10 10
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
as you can see that only half the rows are getting printed in the inverted image.
If I replace the nested loops of invert() of write_pbm.cpp with
char ch;
while(!feof(fp))
char ch = fgetc(fp);
if (ch == '1')
fputc('0', fp_inv);
else if (ch == '0')
fputc('1', fp_inv);
else
fputc(ch, fp_inv);
then it gives the right output in the inverted.pbm file which is
P1
# inverted.pbm
10 10
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1
FF
I am doing the same thing through both the nested loops and the while loop then why is it giving the wrong output in case of nested for loops?
Thanks for reading this, Please provide your valuable response.
c++ image-processing file-format pbm netpbm
add a comment |
I am trying to flip the color of pixels of a simple pbm image which has only pure black and pure white. I am generating the image myself and then reading it and then flipping the bits and saving both the generated image and the color inverted image.
Here is my code (write_pbm.cpp) -
#include "headers/write_pbm.h"
int width_pbm, height_pbm;
void input_sample_dim()
printf("Enter the width and height of image to create = ");
scanf("%d %d", &width_pbm, &height_pbm);
void create_sample_image()
FILE *fp;
fp = fopen("sample.pbm", "wb");
fprintf(fp, "P1n");
fprintf(fp, "# myfile.pbmn");
fprintf(fp, "%d %dn", width_pbm, height_pbm);
for (int i = 1; i <= height_pbm; i++)
for (int j = 1; j <= width_pbm; j++)
(width_pbm - j + 1 == i))
fprintf(fp, "0");
else
fprintf(fp, "1");
if (j == width_pbm)
fprintf(fp, "n");
else
fprintf(fp, " ");
fclose(fp);
void invert ()
printf("tinverting the imagenturning black pixels white and white pixels blackn");
FILE *fp = fopen("sample.pbm", "rb");
while(fgetc(fp) != 'n');
while(fgetc(fp) != 'n');
while(fgetc(fp) != 'n');
FILE *fp_inv;
fp_inv = fopen("inverted.pbm", "wb");
fprintf(fp_inv, "P1n");
fprintf(fp_inv, "# inverted.pbmn");
fprintf(fp_inv, "%d %dn", width_pbm, height_pbm);
for (int i = 1; i <= height_pbm; i++)
for (int j = 1; j <= width_pbm; j++)
char ch = fgetc(fp);
if (ch == '1')
fputc('0', fp_inv);
else if (ch == '0')
fputc('1', fp_inv);
else
fputc(ch, fp_inv);
fclose(fp);
fclose(fp_inv);
Below is the header I am including (write_pbm.h)
#ifndef Write_PBM_H
#define Write_PBM_H
#include <cstdio>
#include <iostream>
void create_sample_image(void);
void input_sample_dim(void);
void invert (void);
extern int width_pbm, height_pbm;
#endif
Below is my main -
#include "write/PBM/headers/write_pbm.h"
int main()
input_sample_dim();
printf("writing sample imagen");
create_sample_image();
printf("sample image with dimenstions %d by %d createdn", width_pbm, height_pbm);
invert();
So I am making a V cross kind of pattern and then inverting the colors and saving both of the created image and the inverted image.
Lets suppose we provide input 10 10
then the file sample.pbm looks like
P1
# myfile.pbm
10 10
0 1 1 1 1 1 1 1 1 0
1 0 1 1 1 1 1 1 0 1
1 1 0 1 1 1 1 0 1 1
1 1 1 0 1 1 0 1 1 1
1 1 1 1 0 0 1 1 1 1
1 1 1 1 0 0 1 1 1 1
1 1 1 0 1 1 0 1 1 1
1 1 0 1 1 1 1 0 1 1
1 0 1 1 1 1 1 1 0 1
0 1 1 1 1 1 1 1 1 0
and inverted.pbm looks like this
P1
# inverted.pbm
10 10
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
as you can see that only half the rows are getting printed in the inverted image.
If I replace the nested loops of invert() of write_pbm.cpp with
char ch;
while(!feof(fp))
char ch = fgetc(fp);
if (ch == '1')
fputc('0', fp_inv);
else if (ch == '0')
fputc('1', fp_inv);
else
fputc(ch, fp_inv);
then it gives the right output in the inverted.pbm file which is
P1
# inverted.pbm
10 10
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1
FF
I am doing the same thing through both the nested loops and the while loop then why is it giving the wrong output in case of nested for loops?
Thanks for reading this, Please provide your valuable response.
c++ image-processing file-format pbm netpbm
Why is this tagged C++?
– Jabberwocky
Mar 25 at 7:03
add a comment |
I am trying to flip the color of pixels of a simple pbm image which has only pure black and pure white. I am generating the image myself and then reading it and then flipping the bits and saving both the generated image and the color inverted image.
Here is my code (write_pbm.cpp) -
#include "headers/write_pbm.h"
int width_pbm, height_pbm;
void input_sample_dim()
printf("Enter the width and height of image to create = ");
scanf("%d %d", &width_pbm, &height_pbm);
void create_sample_image()
FILE *fp;
fp = fopen("sample.pbm", "wb");
fprintf(fp, "P1n");
fprintf(fp, "# myfile.pbmn");
fprintf(fp, "%d %dn", width_pbm, height_pbm);
for (int i = 1; i <= height_pbm; i++)
for (int j = 1; j <= width_pbm; j++)
(width_pbm - j + 1 == i))
fprintf(fp, "0");
else
fprintf(fp, "1");
if (j == width_pbm)
fprintf(fp, "n");
else
fprintf(fp, " ");
fclose(fp);
void invert ()
printf("tinverting the imagenturning black pixels white and white pixels blackn");
FILE *fp = fopen("sample.pbm", "rb");
while(fgetc(fp) != 'n');
while(fgetc(fp) != 'n');
while(fgetc(fp) != 'n');
FILE *fp_inv;
fp_inv = fopen("inverted.pbm", "wb");
fprintf(fp_inv, "P1n");
fprintf(fp_inv, "# inverted.pbmn");
fprintf(fp_inv, "%d %dn", width_pbm, height_pbm);
for (int i = 1; i <= height_pbm; i++)
for (int j = 1; j <= width_pbm; j++)
char ch = fgetc(fp);
if (ch == '1')
fputc('0', fp_inv);
else if (ch == '0')
fputc('1', fp_inv);
else
fputc(ch, fp_inv);
fclose(fp);
fclose(fp_inv);
Below is the header I am including (write_pbm.h)
#ifndef Write_PBM_H
#define Write_PBM_H
#include <cstdio>
#include <iostream>
void create_sample_image(void);
void input_sample_dim(void);
void invert (void);
extern int width_pbm, height_pbm;
#endif
Below is my main -
#include "write/PBM/headers/write_pbm.h"
int main()
input_sample_dim();
printf("writing sample imagen");
create_sample_image();
printf("sample image with dimenstions %d by %d createdn", width_pbm, height_pbm);
invert();
So I am making a V cross kind of pattern and then inverting the colors and saving both of the created image and the inverted image.
Lets suppose we provide input 10 10
then the file sample.pbm looks like
P1
# myfile.pbm
10 10
0 1 1 1 1 1 1 1 1 0
1 0 1 1 1 1 1 1 0 1
1 1 0 1 1 1 1 0 1 1
1 1 1 0 1 1 0 1 1 1
1 1 1 1 0 0 1 1 1 1
1 1 1 1 0 0 1 1 1 1
1 1 1 0 1 1 0 1 1 1
1 1 0 1 1 1 1 0 1 1
1 0 1 1 1 1 1 1 0 1
0 1 1 1 1 1 1 1 1 0
and inverted.pbm looks like this
P1
# inverted.pbm
10 10
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
as you can see that only half the rows are getting printed in the inverted image.
If I replace the nested loops of invert() of write_pbm.cpp with
char ch;
while(!feof(fp))
char ch = fgetc(fp);
if (ch == '1')
fputc('0', fp_inv);
else if (ch == '0')
fputc('1', fp_inv);
else
fputc(ch, fp_inv);
then it gives the right output in the inverted.pbm file which is
P1
# inverted.pbm
10 10
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1
FF
I am doing the same thing through both the nested loops and the while loop then why is it giving the wrong output in case of nested for loops?
Thanks for reading this, Please provide your valuable response.
c++ image-processing file-format pbm netpbm
I am trying to flip the color of pixels of a simple pbm image which has only pure black and pure white. I am generating the image myself and then reading it and then flipping the bits and saving both the generated image and the color inverted image.
Here is my code (write_pbm.cpp) -
#include "headers/write_pbm.h"
int width_pbm, height_pbm;
void input_sample_dim()
printf("Enter the width and height of image to create = ");
scanf("%d %d", &width_pbm, &height_pbm);
void create_sample_image()
FILE *fp;
fp = fopen("sample.pbm", "wb");
fprintf(fp, "P1n");
fprintf(fp, "# myfile.pbmn");
fprintf(fp, "%d %dn", width_pbm, height_pbm);
for (int i = 1; i <= height_pbm; i++)
for (int j = 1; j <= width_pbm; j++)
(width_pbm - j + 1 == i))
fprintf(fp, "0");
else
fprintf(fp, "1");
if (j == width_pbm)
fprintf(fp, "n");
else
fprintf(fp, " ");
fclose(fp);
void invert ()
printf("tinverting the imagenturning black pixels white and white pixels blackn");
FILE *fp = fopen("sample.pbm", "rb");
while(fgetc(fp) != 'n');
while(fgetc(fp) != 'n');
while(fgetc(fp) != 'n');
FILE *fp_inv;
fp_inv = fopen("inverted.pbm", "wb");
fprintf(fp_inv, "P1n");
fprintf(fp_inv, "# inverted.pbmn");
fprintf(fp_inv, "%d %dn", width_pbm, height_pbm);
for (int i = 1; i <= height_pbm; i++)
for (int j = 1; j <= width_pbm; j++)
char ch = fgetc(fp);
if (ch == '1')
fputc('0', fp_inv);
else if (ch == '0')
fputc('1', fp_inv);
else
fputc(ch, fp_inv);
fclose(fp);
fclose(fp_inv);
Below is the header I am including (write_pbm.h)
#ifndef Write_PBM_H
#define Write_PBM_H
#include <cstdio>
#include <iostream>
void create_sample_image(void);
void input_sample_dim(void);
void invert (void);
extern int width_pbm, height_pbm;
#endif
Below is my main -
#include "write/PBM/headers/write_pbm.h"
int main()
input_sample_dim();
printf("writing sample imagen");
create_sample_image();
printf("sample image with dimenstions %d by %d createdn", width_pbm, height_pbm);
invert();
So I am making a V cross kind of pattern and then inverting the colors and saving both of the created image and the inverted image.
Lets suppose we provide input 10 10
then the file sample.pbm looks like
P1
# myfile.pbm
10 10
0 1 1 1 1 1 1 1 1 0
1 0 1 1 1 1 1 1 0 1
1 1 0 1 1 1 1 0 1 1
1 1 1 0 1 1 0 1 1 1
1 1 1 1 0 0 1 1 1 1
1 1 1 1 0 0 1 1 1 1
1 1 1 0 1 1 0 1 1 1
1 1 0 1 1 1 1 0 1 1
1 0 1 1 1 1 1 1 0 1
0 1 1 1 1 1 1 1 1 0
and inverted.pbm looks like this
P1
# inverted.pbm
10 10
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
as you can see that only half the rows are getting printed in the inverted image.
If I replace the nested loops of invert() of write_pbm.cpp with
char ch;
while(!feof(fp))
char ch = fgetc(fp);
if (ch == '1')
fputc('0', fp_inv);
else if (ch == '0')
fputc('1', fp_inv);
else
fputc(ch, fp_inv);
then it gives the right output in the inverted.pbm file which is
P1
# inverted.pbm
10 10
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1
FF
I am doing the same thing through both the nested loops and the while loop then why is it giving the wrong output in case of nested for loops?
Thanks for reading this, Please provide your valuable response.
c++ image-processing file-format pbm netpbm
c++ image-processing file-format pbm netpbm
edited Mar 25 at 7:13
Christophe
43.3k43983
43.3k43983
asked Mar 25 at 6:53
Rakesh SharmaRakesh Sharma
83
83
Why is this tagged C++?
– Jabberwocky
Mar 25 at 7:03
add a comment |
Why is this tagged C++?
– Jabberwocky
Mar 25 at 7:03
Why is this tagged C++?
– Jabberwocky
Mar 25 at 7:03
Why is this tagged C++?
– Jabberwocky
Mar 25 at 7:03
add a comment |
1 Answer
1
active
oldest
votes
Looking at your input file, it appears that every meaningful char (i.e. '0' or '1') is followed by a space.
In the while loop that is working, you read as many chars as needed, until the end of file, just inverting the correct chars and copying unexpected chars. So everything is processed.
In the nested for loops, you are reading the exact number of chars corresponding to the dimensions of the picture so 10*10. Taking into account the layout of the input file, you are therefore reading only 100 chars, so 50 '1' or '0' and 50 spaces. So you process only half of the input.
Unrelated: your code uses C++ header, but all the rest is just plain C. In C++, you should consider to use fstream to read your files, and not the C legacy FILE* API.
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f55332574%2finverting-bits-of-pbm-image-while-vs-for-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Looking at your input file, it appears that every meaningful char (i.e. '0' or '1') is followed by a space.
In the while loop that is working, you read as many chars as needed, until the end of file, just inverting the correct chars and copying unexpected chars. So everything is processed.
In the nested for loops, you are reading the exact number of chars corresponding to the dimensions of the picture so 10*10. Taking into account the layout of the input file, you are therefore reading only 100 chars, so 50 '1' or '0' and 50 spaces. So you process only half of the input.
Unrelated: your code uses C++ header, but all the rest is just plain C. In C++, you should consider to use fstream to read your files, and not the C legacy FILE* API.
add a comment |
Looking at your input file, it appears that every meaningful char (i.e. '0' or '1') is followed by a space.
In the while loop that is working, you read as many chars as needed, until the end of file, just inverting the correct chars and copying unexpected chars. So everything is processed.
In the nested for loops, you are reading the exact number of chars corresponding to the dimensions of the picture so 10*10. Taking into account the layout of the input file, you are therefore reading only 100 chars, so 50 '1' or '0' and 50 spaces. So you process only half of the input.
Unrelated: your code uses C++ header, but all the rest is just plain C. In C++, you should consider to use fstream to read your files, and not the C legacy FILE* API.
add a comment |
Looking at your input file, it appears that every meaningful char (i.e. '0' or '1') is followed by a space.
In the while loop that is working, you read as many chars as needed, until the end of file, just inverting the correct chars and copying unexpected chars. So everything is processed.
In the nested for loops, you are reading the exact number of chars corresponding to the dimensions of the picture so 10*10. Taking into account the layout of the input file, you are therefore reading only 100 chars, so 50 '1' or '0' and 50 spaces. So you process only half of the input.
Unrelated: your code uses C++ header, but all the rest is just plain C. In C++, you should consider to use fstream to read your files, and not the C legacy FILE* API.
Looking at your input file, it appears that every meaningful char (i.e. '0' or '1') is followed by a space.
In the while loop that is working, you read as many chars as needed, until the end of file, just inverting the correct chars and copying unexpected chars. So everything is processed.
In the nested for loops, you are reading the exact number of chars corresponding to the dimensions of the picture so 10*10. Taking into account the layout of the input file, you are therefore reading only 100 chars, so 50 '1' or '0' and 50 spaces. So you process only half of the input.
Unrelated: your code uses C++ header, but all the rest is just plain C. In C++, you should consider to use fstream to read your files, and not the C legacy FILE* API.
edited Mar 25 at 7:13
answered Mar 25 at 7:05
ChristopheChristophe
43.3k43983
43.3k43983
add a comment |
add a comment |
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.
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%2f55332574%2finverting-bits-of-pbm-image-while-vs-for-loop%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
Why is this tagged C++?
– Jabberwocky
Mar 25 at 7:03