By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,038 Members | 1,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,038 IT Pros & Developers. It's quick & easy.

read one line of input from file and then destroying it!

P: n/a
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) {
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.

Apr 4 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
wa*********@googlemail.com wrote:
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.
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) {
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 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.

Apr 4 '07 #2

P: n/a
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

Apr 4 '07 #3

P: n/a
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

Apr 4 '07 #4

P: n/a
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

Apr 4 '07 #5

P: n/a
wa*********@googlemail.com wrote:
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 recommend that you use fgets and then pick apart the line read with
sscanf.
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?
No. If I'm understanding you right, you're saying that _if_ and
observation occured on line N, you want the function where the
observation occured to return N - 1? Further you're storing the values
converted from each line of your file into a structure object.

I don't get what exactly your problem is. Do you want to skip over a
line, do you want to go back a line to the previous one, do you want
to delete a line from the file, ...?

Apr 4 '07 #6

P: n/a
wa*********@googlemail.com wrote:
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?
No. NULL is a macro that evaluates to a null pointer constant value.
It has nothing to do with the end-of-file being reached on streams
except that some library functions like fgets return NULL to indicate
an error. To determine whether it was caused by end-of-file or due to
an I/O error you need to use feof or ferror.
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
I'm guessing you want to read an arbitrary line from the file, say
line N? In C to do that in a fully portable manner you'll basically
have to read in, (and either discard or store), all the previous
lines, keeping count, until you encounter the line you want. You can
store the offsets of the beginning of each line as you're reading the
file for the first time, and as long as you don't write to the file,
you can use those offsets to directly go a particular line later on.
You can use the fgetpos function to store the line offsets in an array
of fpos_t objects. Then you can use the fsetpos function to position
the file pointer at an offset stored in any of the fpos_t objects.

Of course if you write to the stream you'll have to refresh all your
offsets.

An easier method is to read the whole file into a memory buffer and do
all processing on the buffer, writing back to the stream only when
you're finally done.

You cannot tell fgets to read a particular line. It'll always read
from the current file pointer's position upto and including a '\n'
character or one less than the number of bytes of it's buffer argument.

Apr 4 '07 #7

P: n/a
waffle.h...@googlemail.com wrote:

I'm probably being thick, but I'm having trouble understanding what
you
are trying to do.
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.
what is "it". Do you mean it reads and processes a single line?
Do you mean it deletes the processed line from the file?

Such that when the function is called that
line of info does not exist anymore in the file.
so you want to alter the file...
You'll have to write a copy of the file to another
file and not copy the line to be "destroyed". Then
replace the original with the copy.
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) {
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.
well you are calling fgets() in a loop so it reads many lines. To
read
one line call it once. You don't have to do anything to "remove it
from
memory", the next call with over write the infomation.

int process_line (FILE* f)
{
char line[STRING_LENGTH];

if (fgets(line, STRING_LENGTH, obs_fp) != NULL)
{
do_something_to_line(line);
return 1; /* ok */
}
else
return 0; /* failed/finished */
}

is that what you want (code untested, uncompiled etc.)
--
Nick Keighley

Apr 4 '07 #8

P: n/a
wa*********@googlemail.com wrote:
hi thanks...
Please quote a relevant portion of the previous message to provide
context. See the vast majority of posts in the group. Goggle Groups
does that automatically for you now, so there's little excuse.


Brian
Apr 4 '07 #9

P: n/a
Sorry for confusing everyone?! Its not easy to articulate what im
trying to do as im not that experiened at coding so i apologise.

I have a file with inputs ie day, temp, max temp etc. What I want to
do is read each line in. ok fine simple enough I have worked out how
to do that. In the first instance this data is stored in a structure
and used to run a model. The problem comes when instead of the usual
input I have an observation like line 2. Now what I want to do is if I
also have this observation is to not only read the line into the
structure but return the number of observations, as potentially there
could be multiple inputs on a day see line 3.

1 12 23
2 14 24 obs 34
3 3 25 obs 22 obs 24

What I came up with to do this was...
read_input_file ( )

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

d->projectday++;

if(sscanf(line, "%d %lf %lf", &(d->yearday), &(d->temp), &(d-
>maxt[k])) != 3) {
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);

Apr 4 '07 #10

P: n/a
Sorry for confusing everyone?! Its not easy to articulate what im
trying to do as im not that experiened at coding so i apologise.

I have a file with inputs ie day, temp, max temp etc. What I want to
do is read each line in. ok fine simple enough I have worked out how
to do that. In the first instance this data is stored in a structure
and used to run a model. The problem comes when instead of the usual
input I have an observation like line 2. Now what I want to do is if I
also have this observation is to not only read the line into the
structure but return the number of observations, as potentially there
could be multiple inputs on a day see line 3.

1 12 23
2 14 24 obs 34
3 3 25 obs 22 obs 24

What I came up with to do this was...
read_input_file (FILE *obs_fp, structure *d )
{
while (fgets(line, STRING_LENGTH, obs_fp) != NULL) {

d->projectday++;

if(sscanf(line, "%d %lf %lf", &(d->yearday), &(d->temp), &(d-
>maxt[k])) != 3) {
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);
}
The problem is the way I have it defined I read in the whole of the
file. When what I really want to do is just read in each day
sequentially as the function is called so that it allocated the file
inputs to the structure and just returns the number of observations on
that day. So what I have been tryin to ask is how I can ammend what I
have been doing to just read in a single line, have some knowledage of
what line we are up to so that the next time the function is called it
reads the next line etc.

Sorry all I hope that makes sense.

Apr 4 '07 #11

P: n/a
wa*********@googlemail.com wrote:
>
Sorry for confusing everyone?! Its not easy to articulate what im
trying to do as im not that experiened at coding so i apologise.
So why did you ignore the request to not top-post?

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/ (taming google)
<http://members.fortunecity.com/nnqweb/ (newusers)

--
Posted via a free Usenet account from http://www.teranews.com

Apr 5 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.