473,626 Members | 3,304 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3339
wa*********@goo glemail.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*********@goo glemail.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*********@goo glemail.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...@goo glemail.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*********@goo glemail.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8990
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 question is now, where can I find a manual that describes what the read method does with the ifstream object? I'm sitting here with my Linux/Debian machine, but I have not found any
7
12650
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 which is in the "fpdb" folder of my webserver. Its located there so that I can use asp to build a web-based front-end for users to read the database - http://www.nist.ac.th/maths/test1.asp The MBD file is edited using Access (2003) and opening...
18
4875
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) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
40
61198
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
3
2748
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 this app: =============================== static void Main(string args) { if (args.Length > 0) Console.SetIn(new StreamReader(args)); //executes if I don't use the "<", ">" redirection syntax when invoking XmlTextReader xmlin = new...
51
3857
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
4050
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.) ----------- Example c17.bench ---------------- INPUT(1) INPUT(2) INPUT(3) INPUT(6)
0
357
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 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
6
2472
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 from user to calculate the score. Here is a problem. I can read a text file. However, it's read whole file at a time. So,
0
8269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8203
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8711
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7203
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.