如何从文本文件中读取整数 [英] how to read integers from a text file

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

问题描述

大家好,

我是C语言的新手。我想从文本文件中读取整数,并且

想要在主程序中进行一些操作。更具体一点我需要将这些整数中的每一个与另一组整数相乘

存储在一个数组中。如果你能提供一些整数,那将是一个很好的帮助/>
代码。我尝试了函数fscanf,但是我能够读取

只是文本文件的第一个整数。请帮帮我。

解决方案

rohit ha scritto:


我尝试了fscanf函数,但是我能够阅读

只是文本文件的第一个整数。



将每个整数放在一行中,并使用这些

值弹出数组v []。可以如下:


/ * ... * /


#define DIM 100 / *最多100个整数* /


int v [DIM];


/ *''n''是整数(行)最大DIM值* /

/ *它在第一行指定* /

/ * fpin是指向文件的指针* /


for(j = 0; j< n; j ++)

{

if(fscanf(fpin,"%d \ n",& v [j])!= 1 )

{

fprintf(stderr," \ nData corrupted");

返回1;

}

}


/ * ... * /


nembo kid< nembo @ kidwrote:


rohit ha scritto:


我尝试了函数fscanf但是我能够读取

只是文本文件的第一个整数。


将每个整数放在一行中,用这些

$ b $弹出一个数组v [] b
对于fscanf()工作,他们不必分开。

fscanf()跳过所有空白字符,即空格,制表符

和行结束字符(或字符组合)等。


值。可以如下:


/ * ... * /


#define DIM 100 / *最多100个整数* /


int v [DIM];


/ *''n''是整数(行)最大DIM值* /

/ *它在第一行指定* /

/ * fpin是指向FILE * /
的指针


for(j = 0; j< n; j ++)

{

if(fscanf(fpin,"%d \ n",& v [j])!= 1)

{

fprintf(stderr," \ nData corrupted");

返回1;

}

}



或者,如果您不知道文件中有多少整数

(但也假设您知道上限''DIM''):


#include< stdio.h>

#include< ; stdlib.h>


(...)

int count = 0;


(...)


while(count < DIM&& fscanf(fpin,"%d",v + count)== 1)

count ++;


if(ferror) (fpin))

{

fprintf(stderr,Read failure\\\
);

退出(EXIT_FAILURE);

}


但如果你能表现出你所做的事情会好得多br />
试试自己。在这种情况下,你会得到一个解释

为什么它不起作用以及如何改进它。而且,在

长期运行中,除了盲目地使用

其他人代码之外,还会帮助你更多...

问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de
\ __________________________ http://toerring.de


文章< 3d ********************************** @ x19g2000prg。 googlegroups.com>,

rohit< ro ********** @ gmail.comwrote:


>大家好,
我是C语言的新手。我想从文本文件中读取整数,并希望在主程序中进行一些操作。更具体一点我需要繁殖每个整数与另一组整数存储在一个数组中。如果你能为它提供一些代码,那将是一个很大的帮助。我尝试了函数fscanf,但是我能够阅读
只有文本文件的第一个整数。请帮帮我。



< OT>我想你会发现在AWK中这样做要容易得多,而不是用b / b打扰它。


在我看来,你正在做的事情非常适合AWK。


Hi All,
I am new to C language.I want to read integers from a text file and
want to do some operation in the main program.To be more specific I
need to multiply each of these integers with another set of integers
stored in an array.It would be a great help if you could provide some
code for it.I tried the function fscanf but by that I am able to read
only the first integer of the text file.Please help me.

解决方案

rohit ha scritto:

I tried the function fscanf but by that I am able to read
only the first integer of the text file.

Place each integer in a line and popolate an array v[] with these
values. Could be as follows:

/* ... */

#define DIM 100 /* max 100 integers */

int v[DIM];

/* ''n'' is the number of integers (lines) max DIM values */
/* It is specified in the first line */
/* fpin is the pointer to FILE */

for (j=0;j<n;j++)
{
if(fscanf(fpin, "%d\n", &v[j])!=1)
{
fprintf(stderr, "\nData corrupted");
return 1;
}
}

/* ... */


nembo kid <nembo@kidwrote:

rohit ha scritto:

I tried the function fscanf but by that I am able to read
only the first integer of the text file.

Place each integer in a line and popolate an array v[] with these

For fscanf() to work they don''t have to be on separate lines.
fscanf() skips all white-space characters, i.e. spaces, tabs
and line end characters (or character combinations) etc.

values. Could be as follows:

/* ... */

#define DIM 100 /* max 100 integers */

int v[DIM];

/* ''n'' is the number of integers (lines) max DIM values */
/* It is specified in the first line */
/* fpin is the pointer to FILE */

for (j=0;j<n;j++)
{
if(fscanf(fpin, "%d\n", &v[j])!=1)
{
fprintf(stderr, "\nData corrupted");
return 1;
}
}

Or, if you don''t know how many integers there are in the file
(but also assuming that you know an upper limit ''DIM''):

#include <stdio.h>
#include <stdlib.h>

(...)

int count = 0;

(...)

while ( count < DIM && fscanf( fpin, "%d", v + count ) == 1 )
count++;

if ( ferror( fpin ) )
{
fprintf( stderr, "Read failure\n" );
exit( EXIT_FAILURE );
}

But it would be much better if you would show what you did
try yourself. In that case you would get an explanation of
why it didn''t work and how to improve it. And that, in the
long run, will help you much more than just blindly using
other peoples code...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


In article <3d**********************************@x19g2000prg. googlegroups.com>,
rohit <ro**********@gmail.comwrote:

>Hi All,
I am new to C language.I want to read integers from a text file and
want to do some operation in the main program.To be more specific I
need to multiply each of these integers with another set of integers
stored in an array.It would be a great help if you could provide some
code for it.I tried the function fscanf but by that I am able to read
only the first integer of the text file.Please help me.

<OT>I think you''ll find this a lot easier to do it in AWK, than
bothering with C.

It sounds to me like what you are doing is a perfect fit for AWK.


这篇关于如何从文本文件中读取整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆