我试图将一些字符从文件读取到字符串中。例如@2%4$$3
。我的程序将从文本文件中取出243
,现在我需要它在一个字符串中,所以我可以将它转换为二百四十一并给予INT var
。
任何帮助都将非常感谢! :) 谢谢, 布赖恩
答案 0 :(得分:0)
试试这个程序
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char file_buff[1024]="!@25#3$#4";
char num_buff[1024];
int len,i,k;
// open file and read the entire contents specied as per the size
FILE *f = fopen("file.txt", "r");
fgets(file_buff, 1024, f);
printf("String read: %s\n", file_buff);
fclose(f);
len=strlen(file_buff);
// parse the buffer for digits and store it in a buffer
for(i=0,k=0;i<len;i++)
{
if((file_buff[i] >= 0x30) && (file_buff[i] <= 0x39))
{
num_buff[k]=file_buff[i];
k++;
}
}
num_buff[k]='\0';
printf("%s",num_buff);
return 0;
}