Re: [問題] atoi的方法..
#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
09/29 00:18, 2F
→
09/29 00:19, , 3F
09/29 00:19, 3F
討論串 (同標題文章)