473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

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
11 3289
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
7
by: Graham Taylor | last post by:
I've tried posting this in the 'microsoft.public.access' but I will post it here also, as I think it might be the webserver which is causing my problem. --------- I have an Access 2003 database...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
3
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
51
by: moosdau | last post by:
my code: do { printf("please input the dividend and the divisor.\n"); if(!scanf("%d%d",&dend,&dor)) { temp1=1; fflush(stdin); } else
9
by: olson_ord | last post by:
Hi, My ascii file is not exactly a comma separated file. The following is a small but complete example of such a file. (This is the ISCAS circuit file format that I need to read in.) ...
0
by: waffle.horn | last post by:
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...
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.