How to directly input integer values from string (read from file) using sscanf?












0















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









share|improve this question


















  • 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













  • @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
















0















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









share|improve this question


















  • 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













  • @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














0












0








0








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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













  • "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














  • 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













  • @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








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












1 Answer
1






active

oldest

votes


















-2














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





share|improve this answer
























  • 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











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


}
});














draft saved

draft discarded


















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









-2














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





share|improve this answer
























  • 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
















-2














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





share|improve this answer
























  • 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














-2












-2








-2







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 4:06









Anderson OliveiraAnderson Oliveira

876




876













  • 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













  • 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




















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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini