The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

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

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

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

    TwistedJester on
  • 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
  • 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
  • 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]
  • BamaBama Registered User regular
    edited September 2007
    That method still only works for a single digit.

    Bama on
  • 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]
  • 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
  • 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.