从文件中读取一行输入然后销毁它! [英] read one line of input from file and then destroying it!

查看:91
本文介绍了从文件中读取一行输入然后销毁它!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果这有意义我想创建一个可以被调用的函数

它从文件读取一行,然后在使用

信息后将其销毁。这样当调用函数时,

信息行不再存在于文件中。


例如我创建了一个简短的例子我在哪里我在,但是我

不知道怎么改变我在做什么,所以我只读了一个




while(fgets(line,STRING_LENGTH,obs_fp)!= NULL){

d-> projectday ++;

if(sscanf(line,"%d%lf%) lf",&(d-> yearday),&(d-> temp),&(d-

Hi,

if this makes sense i want to create a function that can be called so
that it reads a single line from a file, then after using the
information destroys it. Such that when the function is called that
line of info does not exist anymore in the file.

for example i have created a short example of where i am at, but I
dont know how to alter what im doing so that i just read in a single
line

while (fgets(line, STRING_LENGTH, obs_fp) != NULL) {
d->projectday++;
if(sscanf(line, "%d %lf %lf", &(d->yearday), &(d->temp), &(d-


> maxt [k] ))!= 3){
>maxt[k])) != 3) {



fprintf(stderr,"%s:文件中格式错误的输入:%s在行%d

\ n",* argv,obs_fn,(int)k + 1);

退出(1);

}


k ++;

nrobs ++


return(nrobs);


我想说的就像我说的那样能够从文件中调用此单一行信息

并将其从内存中删除,以便当我调用

函数时再次,它将在第二行工作,依此类推。


i希望这是有道理的。


非常感谢


J.

fprintf(stderr, "%s: badly formatted input in file: %s on line %d
\n", *argv, obs_fn, (int)k+1);
exit(1);
}

k++;
nrobs++

return (nrobs);

I want as I said to be able to call this use a single line of info
from the file and remove it from memory, so that when i call the
function again it will be working on the second line and so on.

i hope this makes sense.

Many thanks

J.

推荐答案

wa ********* @ googlemail.com 写道:




如果这有意义我想创建一个可以被调用的函数

它从文件读取一行,然后使用

信息会破坏它。
Hi,

if this makes sense i want to create a function that can be called so
that it reads a single line from a file, then after using the
information destroys it.



销毁在哪里?内存中的行或文件中的行?

Destroys where? The line in memory or the line in the file?


这样当调用函数时

行信息不再存在在文件中。
Such that when the function is called that
line of info does not exist anymore in the file.



你必须重写文件,删除相关的行。

You''ll have to rewrite the file, with the concerned line removed.


例如我已经创建了一个简短的例子,说明我在哪里,但是我不知道如何改变我正在做的事情,所以我只读了一行



while(fgets(line,STRING_LENGTH,obs_fp)!= NULL){

d-> projectday ++;

if(sscanf( line,%d%lf%lf,&(d-> yearday),&(d-> temp),&(d-
for example i have created a short example of where i am at, but I
dont know how to alter what im doing so that i just read in a single
line

while (fgets(line, STRING_LENGTH, obs_fp) != NULL) {
d->projectday++;
if(sscanf(line, "%d %lf %lf", &(d->yearday), &(d->temp), &(d-

maxt [k]))!= 3){
maxt[k])) != 3) {



fprintf(stderr,"%s:文件中格式错误的输入:%s在线% d

\ n",* argv,obs_fn,(int)k + 1);

退出(1);

}


k ++;

nrobs ++


return(nrobs);


我想要,因为我说可以从文件中调用此单一行信息

并将其从我这里删除mory,所以当我再次调用

函数时,它将在第二行上工作,依此类推。

fprintf(stderr, "%s: badly formatted input in file: %s on line %d
\n", *argv, obs_fn, (int)k+1);
exit(1);
}

k++;
nrobs++

return (nrobs);

I want as I said to be able to call this use a single line of info
from the file and remove it from memory, so that when i call the
function again it will be working on the second line and so on.



fgets从FILE流中读取,而不是从内存中读取。它试图读取一个

完整行或STRING_LENGTH - 1个字符,并将文件

指针提前适当的数量。下次fgets被调用它'

从它停止的地方开始读取。


如果你想删除文件本身的行,你''必须

重写文件,丢弃相关的行。如果你想要
丢弃内存中的行,只需用新数据覆盖它。

fgets reads from a FILE stream, not from memory. It tries to read a
complete line or STRING_LENGTH - 1 characters and advances the file
pointer by the appropriate amount. Next time fgets is called it''ll
start reading from where it left off.

If you want to delete the line in the file itself, you''ll have to
rewrite the file with the concerned line discarded. If you want to
discard the line in memory, just overwrite it with new data.


嗨谢谢...


所以,如果我设置了一个计数器,每次调用

函数时都会继续递增,以指示要工作的行少于

NULL即文件的结尾?


file_line ++;

int_read_file(fp,file_line);

- --------

int read_file(FILE * fp,int file_line)

fgets(line,STRING_LENGTH,obs_fp)!= NULL) {


其实对不起我不知道怎么会把它正确的传递给




hi thanks...

so if i set up a a counter which keeps incrementing everytime the
function is called to indicate the line to work on which is less than
NULL ie the end of the file?

file_line++;
int_read_file (fp, file_line);
----------
int read_file (FILE *fp, int file_line)

fgets(line, STRING_LENGTH, obs_fp) != NULL) {

actually sorry im not sure how i would pass it the correct line to
read from


当我看到删除行...我的意思是


想象一下是否有文件(day,temp,maxtemp)

1 12 23

2 14 24

3 16 22

4 5 11


我想要读取此文件的单行并在代码中的其他位置使用此信息

。我也有一种情况会有进一步的观察,如果是这样的话,我想从

返回一个障碍发生的功能。例如。


1 12 23

2 14 24 obs 34


在这种情况下我正在工作第二行我想要

函数返回那个obs = 1.我设想的方式就是

每行读取的信息存储在一个结构中,如果我们有一个障碍物

然后我们返回那天的障碍物数量。更清楚了吗?

再次感谢

when i saw delete the line...I mean

imagine if there is a file (day, temp, maxtemp)
1 12 23
2 14 24
3 16 22
4 5 11

I want to read a single line of this file and use this information
elsewhere in the code. I also have a situation where there will
potentially be a further observation, and if so i want to return from
the function that an obs occurred. eg.

1 12 23
2 14 24 obs 34

in which case when im working on the second line I would want the
function to return that obs = 1. the way i envisage it is that for
each line read the info is stored in a structure and if we have an obs
then we return the number of obs on that day. is that any clearer?
Thanks again


这篇关于从文件中读取一行输入然后销毁它!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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