c언어 strtoul 함수질문 드립니다,.

rkdka12의 이미지

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define Fclose(a) if (a != NULL) \
{\
    fclose(a); \
    a = NULL; \
}
#define COMMAND 16
 
 
int main(int argc, char *argv[]){
	if (argc != 5)
	{
		printf("error! program don't have four arguments");
		return 1;
	}
	FILE *tr;
	long adr;
	char pch[20],*buf,*temp;
	if ((tr = fopen(argv[1], "r")) == NULL)
	{
		perror("memory trace file");
		return -1;
	}
	else{
		/*parsing*/
		while (!feof(tr)){
			fgets(pch,COMMAND, tr);
			buf = strtok(pch, " ");
			buf = strtok(NULL, " ");
			//adr = atoi(buf);
			adr = strtoul(buf,NULL,10);
			printf("%s\n", buf);
		}
	}
	int n;
	printf("argc = %d\n", argc - 1);
	for (n = 1; n < argc; n++)
		printf("argv{%d} = %s\n", n, argv[n]);
	Fclose(tr);
	return 0;
}

혹시 몰라 전체 코드 올립니다.

제가 질문 드리고 싶은 부분은

while (!feof(tr)){
			fgets(pch,COMMAND, tr);
			buf = strtok(pch, " ");
			buf = strtok(NULL, " ");
			//adr = atoi(buf);
			adr = strtoul(buf,buf,10);
			printf("%s\n", buf);
		}

이부분인데요 이런 형태의 파일 읽어서

s 0x1fffff70 1
l 0x1fffff78 1
l 0x1fffff98 6
l 0x1fffffa0 2
l 0x1fffffa8 2
l 0x1fffffb0 2
l 0x1fffffb8 2
l 0x1fffffc0 2
l 0x1fffffc8 2
l 0x1fffffd0 2
l 0x1fffffd8 2
l 0x1fffffe0 2
l 0x1fffffe8 2
l 0x1ffffff0 2
l 0x1ffffff8 2
l 0x20000000 2
l 0x20000008 2
l 0x20000010 2

16진수 부분을 파싱을 했구요 그다음에 이 16진수를 buf에 담아가지고 각각 printf에 buf를 하면 16진수 부분만 뽑혀서 잘 출력이 되는데 이제 이 buf가지고 strtoul를 쓰려고 하니 런타임 에러가 뜨네요... 그 printf로 buf 출력할때 잘 나오다가

0x1fffffa8
0x1fffffa8
...
<null>

이라고 출력이 되는데 혹시 이게 잘못인 건지... 근데 또 strtoul을 쓰면 null부분 전에라도 잘 변환이 되야하는데 adr에 변환된 16진수 넣고 adr출력을 시키면

<null>
<null>
...

나오다가 런타임 에러가 뜹니다. 어떻게 해야할지 감도 안잡히네요 ㅠ

yhsuk의 이미지

strtoul의 사용법이 잘못되었습니다. 함수의 원형을 보시면 다음과 같습니다.

unsigned long int strtoul(const char *nptr, char **endptr, int base);

두번 째 인자는 이중 포인터이고, 다른 변수를 할당해서 사용해야 합니다.
16진수 변환시에는 base값에 0이나 16을 넣어주어야 합니다.

char *endptr = NULL;
unsigned long int n_ret;
n_ret = strtoul(buf, &endptr, 16);
 
...

strtoul은 overflow, underflow등 까지도 처리하고자 하면, 생각보다 사용하기 까다로운 함수입니다.
man 페이지를 좀 더 살펴보세요.

Signature :) - "여유를 갖고 행동하되 게을러지지 말자"

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.