As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

Minor C problem

TwistedJesterTwistedJester Registered User regular
edited September 2007 in Help / Advice Forum
For the first project in my system software class, we need to create a program that lets you read and edit the id3 tag (v1.1) of a music file. I have everything working pretty much, except for editting the track field. In the assignment, the track takes up one byte in the file. My problem is reading in a track number someone submits, and then reading out the value from the file as an integer. For example, if I try to set the track number to 6, it reads out (using %d) as its ascii value 54. If I use %c to print it out, it works, but only for 0-9. Is there some sort of formatting I can use with the scan codes to get what I'm looking for?

TwistedJester on

Posts

  • Options
    GoetterdaemmerungGoetterdaemmerung Registered User regular
    edited September 2007
    int atoi(buffer);
    returns the integer value written in a null-terminated (ascii) buffer.

    Goetterdaemmerung on
  • Options
    TwistedJesterTwistedJester Registered User regular
    edited September 2007
    That gives me a segmentation fault.

    TwistedJester on
  • Options
    LewishamLewisham Registered User regular
    edited September 2007
    Then your string pointer is no longer valid, atoi is what you need.

    NAME
    atoi -- convert ASCII string to integer

    LIBRARY
    Standard C Library (libc, -lc)

    SYNOPSIS
    #include <stdlib.h>

    int
    atoi(const char *nptr);

    DESCRIPTION
    The atoi() function converts the initial portion of the string pointed to
    by nptr to integer representation.

    It is equivalent to:

    (int)strtol(nptr, (char **)NULL, 10);

    SEE ALSO
    atof(3), atol(3), strtod(3), strtol(3), strtoul(3)

    STANDARDS
    The atoi() function conforms to ANSI X3.159-1989 (``ANSI C89'').

    CAVEATS
    atoi does no overflow checking, handles unsigned numbers poorly, and han-
    dles strings containing trailing extra characters (like ``123abc'')
    poorly. Careful use of strtol(3) and strtoul(3) can alleviate these
    problems.

    Lewisham on
  • Options
    TwistedJesterTwistedJester Registered User regular
    edited September 2007
    Actually, the problem wasn't printing out the value, it was correctly reading it in from the command line. Thanks for the help though duders.

    TwistedJester on
  • Options
    JaninJanin Registered User regular
    edited September 2007
    char string[2] = "4";
    int number = string[0] - '0';
    printf ("%d", number); /* Prints 4 */
    

    Janin on
    [SIGPIC][/SIGPIC]
  • Options
    BamaBama Registered User regular
    edited September 2007
    That method still only works for a single digit.

    Bama on
  • Options
    JaninJanin Registered User regular
    edited September 2007
    Bama wrote: »
    That method still only works for a single digit.

    The field in the file is only one byte long, so that's not a problem.

    Janin on
    [SIGPIC][/SIGPIC]
  • Options
    ASimPersonASimPerson Cold... and hard.Registered User regular
    edited September 2007
    Janin wrote: »
    Bama wrote: »
    That method still only works for a single digit.

    The field in the file is only one byte long, so that's not a problem.

    Isn't it, though? The number could be anywhere from 0-255 if it's unsigned, IIRC.

    ASimPerson on
  • Options
    JaninJanin Registered User regular
    edited September 2007
    ASimPerson wrote: »
    Janin wrote: »
    Bama wrote: »
    That method still only works for a single digit.

    The field in the file is only one byte long, so that's not a problem.

    Isn't it, though? The number could be anywhere from 0-255 if it's unsigned, IIRC.

    If it was already a number, he wouldn't have to convert from ASCII encoding.

    Janin on
    [SIGPIC][/SIGPIC]
Sign In or Register to comment.