How to directly input integer values from string (read from file) using sscanf?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to write a piece of code that will read the "header" of a PPM file.
For example:
P3
400 200
255
In this case the width is 400 and the height is 200 and the max colour value is 255. I'm trying to assign these strings values as integers but I think there is a better way to do this with less lines and more "safer." How can I avoid having to use the atoi() function? (Note I've already included the "check if file is open-able part in my ACTUAL code this is merely a reduced snippet)
char buffer[200];
char height[200];
char width[200];
char maxColour[200];
FILE *file = fopen("mcmaster.ppm", "r");
fgets(buffer, sizeof(buffer), file); // File format line
fgets(buffer, sizeof(buffer), file); // Width x height line
sscanf(buffer, "%s %s", width, height);
fgets(buffer, sizeof(buffer), file); // Max colour line
sscanf(buffer, "%s", maxColour);
int actHeight = atoi(height);
int actWidth = atoi(width);
int actMaxColour = atoi(maxColour);
c io scanf
add a comment |
I'm trying to write a piece of code that will read the "header" of a PPM file.
For example:
P3
400 200
255
In this case the width is 400 and the height is 200 and the max colour value is 255. I'm trying to assign these strings values as integers but I think there is a better way to do this with less lines and more "safer." How can I avoid having to use the atoi() function? (Note I've already included the "check if file is open-able part in my ACTUAL code this is merely a reduced snippet)
char buffer[200];
char height[200];
char width[200];
char maxColour[200];
FILE *file = fopen("mcmaster.ppm", "r");
fgets(buffer, sizeof(buffer), file); // File format line
fgets(buffer, sizeof(buffer), file); // Width x height line
sscanf(buffer, "%s %s", width, height);
fgets(buffer, sizeof(buffer), file); // Max colour line
sscanf(buffer, "%s", maxColour);
int actHeight = atoi(height);
int actWidth = atoi(width);
int actMaxColour = atoi(maxColour);
c io scanf
1
For your width x height why are you not just directly using asscanf
format string to scan integers:sscanf(buffer, "%d %d", &actWidth, &actHeight);
(assuming of course they're previously declared asint
)? Same for readingactMaxColour
.
– lurker
Nov 22 '18 at 3:44
@lurker: Good question, especially as the conversion function isatoi()
which has undefined error handling properties. If the code usedstrtol()
correctly for the conversion, then scan to strings and then strings to integers would avoid undefined behaviour insscanf()
(orscanf()
) when the value is too large for the integer type. I doubt if the thinking here was that subtle, though — it was more likely a simple oversight.
– Jonathan Leffler
Nov 22 '18 at 3:53
"better way to do this with less lines and more "safer.". It depends: 1) What do you want to happen when the input is not a validint
? What do you want to happen when the input is out of range?
– chux
Nov 22 '18 at 5:25
add a comment |
I'm trying to write a piece of code that will read the "header" of a PPM file.
For example:
P3
400 200
255
In this case the width is 400 and the height is 200 and the max colour value is 255. I'm trying to assign these strings values as integers but I think there is a better way to do this with less lines and more "safer." How can I avoid having to use the atoi() function? (Note I've already included the "check if file is open-able part in my ACTUAL code this is merely a reduced snippet)
char buffer[200];
char height[200];
char width[200];
char maxColour[200];
FILE *file = fopen("mcmaster.ppm", "r");
fgets(buffer, sizeof(buffer), file); // File format line
fgets(buffer, sizeof(buffer), file); // Width x height line
sscanf(buffer, "%s %s", width, height);
fgets(buffer, sizeof(buffer), file); // Max colour line
sscanf(buffer, "%s", maxColour);
int actHeight = atoi(height);
int actWidth = atoi(width);
int actMaxColour = atoi(maxColour);
c io scanf
I'm trying to write a piece of code that will read the "header" of a PPM file.
For example:
P3
400 200
255
In this case the width is 400 and the height is 200 and the max colour value is 255. I'm trying to assign these strings values as integers but I think there is a better way to do this with less lines and more "safer." How can I avoid having to use the atoi() function? (Note I've already included the "check if file is open-able part in my ACTUAL code this is merely a reduced snippet)
char buffer[200];
char height[200];
char width[200];
char maxColour[200];
FILE *file = fopen("mcmaster.ppm", "r");
fgets(buffer, sizeof(buffer), file); // File format line
fgets(buffer, sizeof(buffer), file); // Width x height line
sscanf(buffer, "%s %s", width, height);
fgets(buffer, sizeof(buffer), file); // Max colour line
sscanf(buffer, "%s", maxColour);
int actHeight = atoi(height);
int actWidth = atoi(width);
int actMaxColour = atoi(maxColour);
c io scanf
c io scanf
asked Nov 22 '18 at 3:17
Akila KavisingheAkila Kavisinghe
133
133
1
For your width x height why are you not just directly using asscanf
format string to scan integers:sscanf(buffer, "%d %d", &actWidth, &actHeight);
(assuming of course they're previously declared asint
)? Same for readingactMaxColour
.
– lurker
Nov 22 '18 at 3:44
@lurker: Good question, especially as the conversion function isatoi()
which has undefined error handling properties. If the code usedstrtol()
correctly for the conversion, then scan to strings and then strings to integers would avoid undefined behaviour insscanf()
(orscanf()
) when the value is too large for the integer type. I doubt if the thinking here was that subtle, though — it was more likely a simple oversight.
– Jonathan Leffler
Nov 22 '18 at 3:53
"better way to do this with less lines and more "safer.". It depends: 1) What do you want to happen when the input is not a validint
? What do you want to happen when the input is out of range?
– chux
Nov 22 '18 at 5:25
add a comment |
1
For your width x height why are you not just directly using asscanf
format string to scan integers:sscanf(buffer, "%d %d", &actWidth, &actHeight);
(assuming of course they're previously declared asint
)? Same for readingactMaxColour
.
– lurker
Nov 22 '18 at 3:44
@lurker: Good question, especially as the conversion function isatoi()
which has undefined error handling properties. If the code usedstrtol()
correctly for the conversion, then scan to strings and then strings to integers would avoid undefined behaviour insscanf()
(orscanf()
) when the value is too large for the integer type. I doubt if the thinking here was that subtle, though — it was more likely a simple oversight.
– Jonathan Leffler
Nov 22 '18 at 3:53
"better way to do this with less lines and more "safer.". It depends: 1) What do you want to happen when the input is not a validint
? What do you want to happen when the input is out of range?
– chux
Nov 22 '18 at 5:25
1
1
For your width x height why are you not just directly using a
sscanf
format string to scan integers: sscanf(buffer, "%d %d", &actWidth, &actHeight);
(assuming of course they're previously declared as int
)? Same for reading actMaxColour
.– lurker
Nov 22 '18 at 3:44
For your width x height why are you not just directly using a
sscanf
format string to scan integers: sscanf(buffer, "%d %d", &actWidth, &actHeight);
(assuming of course they're previously declared as int
)? Same for reading actMaxColour
.– lurker
Nov 22 '18 at 3:44
@lurker: Good question, especially as the conversion function is
atoi()
which has undefined error handling properties. If the code used strtol()
correctly for the conversion, then scan to strings and then strings to integers would avoid undefined behaviour in sscanf()
(or scanf()
) when the value is too large for the integer type. I doubt if the thinking here was that subtle, though — it was more likely a simple oversight.– Jonathan Leffler
Nov 22 '18 at 3:53
@lurker: Good question, especially as the conversion function is
atoi()
which has undefined error handling properties. If the code used strtol()
correctly for the conversion, then scan to strings and then strings to integers would avoid undefined behaviour in sscanf()
(or scanf()
) when the value is too large for the integer type. I doubt if the thinking here was that subtle, though — it was more likely a simple oversight.– Jonathan Leffler
Nov 22 '18 at 3:53
"better way to do this with less lines and more "safer.". It depends: 1) What do you want to happen when the input is not a valid
int
? What do you want to happen when the input is out of range?– chux
Nov 22 '18 at 5:25
"better way to do this with less lines and more "safer.". It depends: 1) What do you want to happen when the input is not a valid
int
? What do you want to happen when the input is out of range?– chux
Nov 22 '18 at 5:25
add a comment |
1 Answer
1
active
oldest
votes
I suggest you to use fscanf instead of sscanf. First, define a "Error Function" to verify problems of reading the file, as
void fscanf_Error(char *file)
{
fprintf(stderr,"Error reading file: %sn. Exiting(1).n ",file);
exit(1);
}
Then
char dummy[12];
if(!fscanf(file, "%sn", dummy))
fscanf_Error(file);
if(!fscanf(file," %d %dn", width, height))
fscanf_Error(file);
if(!fscanf(file, "%dn", maxColour))
fscanf_Error(file);
You rarely want spaces and never want newlines in the fscanf format string. Also, fscanf only returns 0 if no conversions happen and EOF is not reached. So while 0 should be an error, some non-zero return values should also be errors.
– Chris Dodd
Nov 22 '18 at 7:17
I only put this new line because, depending on the version of the C compiler you're using, warnings appear complaining that the returning of fscanf is not used.
– Anderson Oliveira
Nov 23 '18 at 8:15
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%2f53423379%2fhow-to-directly-input-integer-values-from-string-read-from-file-using-sscanf%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
I suggest you to use fscanf instead of sscanf. First, define a "Error Function" to verify problems of reading the file, as
void fscanf_Error(char *file)
{
fprintf(stderr,"Error reading file: %sn. Exiting(1).n ",file);
exit(1);
}
Then
char dummy[12];
if(!fscanf(file, "%sn", dummy))
fscanf_Error(file);
if(!fscanf(file," %d %dn", width, height))
fscanf_Error(file);
if(!fscanf(file, "%dn", maxColour))
fscanf_Error(file);
You rarely want spaces and never want newlines in the fscanf format string. Also, fscanf only returns 0 if no conversions happen and EOF is not reached. So while 0 should be an error, some non-zero return values should also be errors.
– Chris Dodd
Nov 22 '18 at 7:17
I only put this new line because, depending on the version of the C compiler you're using, warnings appear complaining that the returning of fscanf is not used.
– Anderson Oliveira
Nov 23 '18 at 8:15
add a comment |
I suggest you to use fscanf instead of sscanf. First, define a "Error Function" to verify problems of reading the file, as
void fscanf_Error(char *file)
{
fprintf(stderr,"Error reading file: %sn. Exiting(1).n ",file);
exit(1);
}
Then
char dummy[12];
if(!fscanf(file, "%sn", dummy))
fscanf_Error(file);
if(!fscanf(file," %d %dn", width, height))
fscanf_Error(file);
if(!fscanf(file, "%dn", maxColour))
fscanf_Error(file);
You rarely want spaces and never want newlines in the fscanf format string. Also, fscanf only returns 0 if no conversions happen and EOF is not reached. So while 0 should be an error, some non-zero return values should also be errors.
– Chris Dodd
Nov 22 '18 at 7:17
I only put this new line because, depending on the version of the C compiler you're using, warnings appear complaining that the returning of fscanf is not used.
– Anderson Oliveira
Nov 23 '18 at 8:15
add a comment |
I suggest you to use fscanf instead of sscanf. First, define a "Error Function" to verify problems of reading the file, as
void fscanf_Error(char *file)
{
fprintf(stderr,"Error reading file: %sn. Exiting(1).n ",file);
exit(1);
}
Then
char dummy[12];
if(!fscanf(file, "%sn", dummy))
fscanf_Error(file);
if(!fscanf(file," %d %dn", width, height))
fscanf_Error(file);
if(!fscanf(file, "%dn", maxColour))
fscanf_Error(file);
I suggest you to use fscanf instead of sscanf. First, define a "Error Function" to verify problems of reading the file, as
void fscanf_Error(char *file)
{
fprintf(stderr,"Error reading file: %sn. Exiting(1).n ",file);
exit(1);
}
Then
char dummy[12];
if(!fscanf(file, "%sn", dummy))
fscanf_Error(file);
if(!fscanf(file," %d %dn", width, height))
fscanf_Error(file);
if(!fscanf(file, "%dn", maxColour))
fscanf_Error(file);
answered Nov 22 '18 at 4:06
Anderson OliveiraAnderson Oliveira
1076
1076
You rarely want spaces and never want newlines in the fscanf format string. Also, fscanf only returns 0 if no conversions happen and EOF is not reached. So while 0 should be an error, some non-zero return values should also be errors.
– Chris Dodd
Nov 22 '18 at 7:17
I only put this new line because, depending on the version of the C compiler you're using, warnings appear complaining that the returning of fscanf is not used.
– Anderson Oliveira
Nov 23 '18 at 8:15
add a comment |
You rarely want spaces and never want newlines in the fscanf format string. Also, fscanf only returns 0 if no conversions happen and EOF is not reached. So while 0 should be an error, some non-zero return values should also be errors.
– Chris Dodd
Nov 22 '18 at 7:17
I only put this new line because, depending on the version of the C compiler you're using, warnings appear complaining that the returning of fscanf is not used.
– Anderson Oliveira
Nov 23 '18 at 8:15
You rarely want spaces and never want newlines in the fscanf format string. Also, fscanf only returns 0 if no conversions happen and EOF is not reached. So while 0 should be an error, some non-zero return values should also be errors.
– Chris Dodd
Nov 22 '18 at 7:17
You rarely want spaces and never want newlines in the fscanf format string. Also, fscanf only returns 0 if no conversions happen and EOF is not reached. So while 0 should be an error, some non-zero return values should also be errors.
– Chris Dodd
Nov 22 '18 at 7:17
I only put this new line because, depending on the version of the C compiler you're using, warnings appear complaining that the returning of fscanf is not used.
– Anderson Oliveira
Nov 23 '18 at 8:15
I only put this new line because, depending on the version of the C compiler you're using, warnings appear complaining that the returning of fscanf is not used.
– Anderson Oliveira
Nov 23 '18 at 8:15
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%2f53423379%2fhow-to-directly-input-integer-values-from-string-read-from-file-using-sscanf%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
1
For your width x height why are you not just directly using a
sscanf
format string to scan integers:sscanf(buffer, "%d %d", &actWidth, &actHeight);
(assuming of course they're previously declared asint
)? Same for readingactMaxColour
.– lurker
Nov 22 '18 at 3:44
@lurker: Good question, especially as the conversion function is
atoi()
which has undefined error handling properties. If the code usedstrtol()
correctly for the conversion, then scan to strings and then strings to integers would avoid undefined behaviour insscanf()
(orscanf()
) when the value is too large for the integer type. I doubt if the thinking here was that subtle, though — it was more likely a simple oversight.– Jonathan Leffler
Nov 22 '18 at 3:53
"better way to do this with less lines and more "safer.". It depends: 1) What do you want to happen when the input is not a valid
int
? What do you want to happen when the input is out of range?– chux
Nov 22 '18 at 5:25