Re: [問題] atoi的方法..

看板C_and_CPP作者 (返無)時間13年前 (2012/09/28 15:59), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串2/2 (看更多)
#include<stdio.h> #include<stdlib.h> int main(void) { FILE *fptr; char ch; fptr=fopen("C01","r"); char string[10]=""; while(fgets(string, 9, fptr)!=NULL) { printf("str: %s\n", string); int num=atoi(string); printf("%d\n",num); } fclose(fptr); return 0; } 現在比較建議使用 long int strtol(const char *nptr, char **endptr, int base); ※ 引述《perhot9 (小樹)》之銘言: : 已經有爬過文 但還是沒辦法字串轉數字... : #include<stdio.h> : #include<stdlib.h> : int main(void){ : FILE *fptr; : char ch; : fptr=fopen("C01","r"); : while((ch=fgetc(fptr))!=EOF){ : int num=atoi(ch); : printf("%d",num); : } : fclose(fptr); : return 0; : } : 我的C01是類似於 : 10 : 20 : 30 : 這種資料 : 爬文的文章有說 atoi是char* 不是char : 但我還是不懂該怎麼改... : 可以稍微指點迷津嗎!? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.125.239.46

09/28 16:24, , 1F
這是標準方法。
09/28 16:24, 1F

09/29 00:18, , 2F
strtol之類的,要用到對,也不簡單,要寫wrapper來簡化
09/29 00:18, 2F

09/29 00:19, , 3F
09/29 00:19, 3F
文章代碼(AID): #1GPLZnZT (C_and_CPP)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
文章代碼(AID): #1GPLZnZT (C_and_CPP)