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?
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.
Posts
returns the integer value written in a null-terminated (ascii) buffer.
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.
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.