简单的输出输入.... [英] simple output input ....

查看:54
本文介绍了简单的输出输入....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



好​​的...我在阅读包含信息的文件时遇到了问题,并且

尝试将其发送到函数中...

包含信息的文件格式为:

燃气10000家用燃气表

电力3000家用电表

飞行8000海外假期

1000航班长途周末

汽车1250自有车(10000英里)

汽车500度假租车


我想把第一列存放在一个叫做的数组中 -

char燃料[]

第二列 - 双倍金额

第三列 - 字符描述[]


第一个和第二个将由一个名为double co2的函数使用(char

fuel [],double amount)

和第三个将存储在int main ....


第二个问题...你能从函数double返回2个值吗? />
co2(char燃料[],双倍金额)??


谢谢

克里斯

Hi
Ok...I got a problem in reading a file containning information, and
trying to send it to a function...
the file containing information has format:
Gas 10000 Home Gas Meter
Electricity 3000 Home Electricity Meter
Flight 8000 Holiday Abroad
Flight 1000 Long Weekend
Car 1250 Own Car (10000 miles)
Car 500 Holiday Hire Car

I would like to put the first column to be stored in an array called -
char fuel [ ]
second column - double amount
third column - char description [ ]

the first and second will be used by a fuction called double co2(char
fuel [ ], double amount)
and the third will be stored in the int main ....

Second Question...Can you return 2 values from a function double
co2(char fuel [ ], double amount)??

Thanks
Chris

推荐答案



< ch **** @ gmail.comwrote in消息

news:11 ********************** @ u30g2000hsc.googlegr oups.com ...

<ch****@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...



好​​的...我在阅读包含信息的文件时遇到了问题,并且

试图将其发送到函数...

包含信息的文件格式为:

燃气10000家用煤气表

电力3000家用电表

飞行8000海外假日

1000航班长途周末

汽车1250自有车(10000英里)

汽车500度假租车

我想把第一列存放在一个叫做的数组中 -

char fuel []

第二列 - 双倍金额

第三列 - 字符描述[]


第一个和第二个将被称为double co2的函数使用(char

燃料[],做uble amount)

和第三个将存储在int main ....


第二个问题...你能从一个函数返回2个值吗? double

co2(char fuel [],double amount)??
Hi
Ok...I got a problem in reading a file containning information, and
trying to send it to a function...
the file containing information has format:
Gas 10000 Home Gas Meter
Electricity 3000 Home Electricity Meter
Flight 8000 Holiday Abroad
Flight 1000 Long Weekend
Car 1250 Own Car (10000 miles)
Car 500 Holiday Hire Car

I would like to put the first column to be stored in an array called -
char fuel [ ]
second column - double amount
third column - char description [ ]

the first and second will be used by a fuction called double co2(char
fuel [ ], double amount)
and the third will be stored in the int main ....

Second Question...Can you return 2 values from a function double
co2(char fuel [ ], double amount)??



调用fgets()读取输入,一行读取时间。

一旦你将线路放入临时缓冲区,就需要将其分解。

这实际上非常繁琐,但在概念上并不困难。 br />
你可以通过读取来获得第一个单词,直到你通过调用strtol()命中一个空格字符,

数字,然后是你传递给strtol的结束指针

为您提供剩余的行。但是你也应该处理任何

意外输入。

另一种方法是使用fscanf()。然而,这很容易使用不安全。

-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

Call fgets() to read the input, one line at a time.
Once you''ve got the line into a temporary buffer, you need to break it up.
This is actually quite fiddly, but not conceptually difficult.
You can get the first word by reading until you hit a space character, the
number by calling strtol(), and then the end pointer that you pass to strtol
gives you the remainder of the line. However you should also deal with any
unexpected inputs.
Another way is to use fscanf(). However this is very easy to use unsafely.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


ch****@gmail.com 写道:



好​​的...我在阅读包含信息的文件时遇到了问题,并且

试图将其发送给函数.. 。

包含信息的文件格式为:

燃气10000家用煤气表

电3000家用电表

国际航班8000海外度假

1000航班周末长途旅行

汽车1250自有车(10000英里)

汽车500度假租车


我想把第一列存放在一个叫做的数组中 -

char fuel []

第二列 - 双倍数量

第三列 - 字符描述[]


第一个和第二个将由一个名为double co2的函数使用(char

fuel [ ],双倍金额)

和第三个将存储在int main ....


第二个问题...你能从中返回2个值吗?一个功能双

二氧化碳(char燃料[],双倍金额)??


谢谢

克里斯
Hi
Ok...I got a problem in reading a file containning information, and
trying to send it to a function...
the file containing information has format:
Gas 10000 Home Gas Meter
Electricity 3000 Home Electricity Meter
Flight 8000 Holiday Abroad
Flight 1000 Long Weekend
Car 1250 Own Car (10000 miles)
Car 500 Holiday Hire Car

I would like to put the first column to be stored in an array called -
char fuel [ ]
second column - double amount
third column - char description [ ]

the first and second will be used by a fuction called double co2(char
fuel [ ], double amount)
and the third will be stored in the int main ....

Second Question...Can you return 2 values from a function double
co2(char fuel [ ], double amount)??

Thanks
Chris



回答第二个问题:

见K& R第1章第8节第29页


" ;当数组的名称用作参数时,传递给函数的值

是数组开头的位置或地址
数组 - 没有复制数组元素。通过下标这个

值,该函数可以访问和更改数组的任何参数。


您的函数将改变原始数组中的值。

Response to the Second Question:
See K&R chapter 1, section 8, page 29

"When the name of an array is used as an argument, the value
passed to the function is the location or address of the beginning of
the array - there is no copying of array elements. By subscripting this
value, the function can access and alter any argument of the array."

Your function will alter the values in the original array.


Jim Barlow< fo ********** @ charter.netwrites:

[...]
Jim Barlow <fo**********@charter.netwrites:
[...]

参见K& R第1章第8节第29页


"当数组的名称用作参数,传递给函数的值

是数组开头的位置或地址
数组 - 没有复制数组元素。通过下标

这个值,该函数可以访问和更改

数组的任何参数。
See K&R chapter 1, section 8, page 29

"When the name of an array is used as an argument, the value
passed to the function is the location or address of the beginning of
the array - there is no copying of array elements. By subscripting
this value, the function can access and alter any argument of the
array."



实际上(正如我不断重复)这是一个特殊情况的更多

一般规则。每当使用数组名称(更一般地,表达式为数组类型的
)时,其值将被隐式转换为指针

到其第一个元素,*除非*它这是一元&的操作数。或

" sizeof"运算符,或者它是用于初始化

字符数组的字符串文字。


-

Keith Thompson(The_Other_Keith ) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

Which is actually (as I keep repeating) a special case of a more
general rule. Whenever an array name (more generally, an expression
of array type) is used, its value is implicitly converted to a pointer
to its first element, *unless* it''s the operand of a unary "&" or
"sizeof" operator, or it''s a string literal used to initialize a
character array.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于简单的输出输入....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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