本文介绍了查找整数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我是C的新手所以请原谅我的无能。


如果给我一个文件" jfewuuj3uefi8jkw128jdmnsdf\ ;;''d1904"


我想将每个0-9整数的出现捕获到数组中。


所以在阵列中我会有{3,8,1,2,8,1,9,0,4}


我该怎么做呢? SCANF? fgets?


谢谢:)

Hello,

I''m very new to C so please forgive my ineptitude.

If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;''d1904"

I want to capture each occurence of an integer 0-9 into an array.

So in the array I would have {3,8,1,2,8,1,9,0,4}

How would I go about doing this? scanf? fgets?

Thank you :)

推荐答案



看看fgetc和isdigit。


-

Thomas M. Sommers - - AB2SB

Take a look at fgetc and isdigit.

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB




看看fgetc和isdigit。


-

Thomas M. Sommers - - AB2SB


Take a look at fgetc and isdigit.

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB



看看fgetc和isdigit。


Take a look at fgetc and isdigit.



这是我在代码中的内容:

arr [i] = fgetc("%d");


我从不兼容的指针中获取fgetc的传递参数1

type"错误。


Here''s what I have in my code:
arr[i] = fgetc("%d ");

I''m gettting a "passing argument 1 of ''fgetc'' from incompatible pointer
type" error.



阅读fgetc的手册页。它需要一个FILE *作为参数,

并返回一个int。你想要这样的东西:


int ch;

while((ch = fgetc(file_pointer))!= EOF){

if(isdigit(ch)){

/ *你的数组中的东西ch * /

}

}


假设file_pointer是一个FILE *并且已经成功打开了用于阅读的
,并且相应的头文件已经是

#included。


-

Thomas M. Sommers - - AB2SB

Read the man page for fgetc. It takes a FILE * as an argument,
and returns an int. You want something like this:

int ch;
while ( (ch = fgetc(file_pointer)) != EOF ) {
if ( isdigit(ch) ) {
/* stuff ch in your array */
}
}

Assuming file_pointer is a FILE * and has been successfully
opened for reading, and the appropriate header files have been
#included.

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB


这篇关于查找整数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 19:09