file operation

M

Magix

Hi,

I have following in my data file:

<12345><Benjamin><Single>
<4567><Stephen><Married>
<8383245><George><Married>

I want to write a function that display according to the line number given
(Max line number is 5), without the '<' and '>'
Example:
DisplayInfo(1) will display:-

12345 Benjamin
Single

DisplayInfo(2) will display:-

4567 Stephen
Married

.... and so on.

#define LINE_SIZE 100;
static char buffer[LINE_SIZE];

void DisplayInfo(int linenum)
{
//... file open here, if error, return.
while(fgets(buffer, sizeof(buffer), fp)!=NULL)
{
// check for line number
}
fclose(fp);
}


Any hints/tips will be great. Thanks.
 
A

Alex Fraser

Magix said:
I have following in my data file:

<12345><Benjamin><Single>
<4567><Stephen><Married>
<8383245><George><Married>

I want to write a function that display according to the line number given
(Max line number is 5), without the '<' and '>'
Example:
DisplayInfo(1) will display:-

12345 Benjamin
Single

It would perhaps be better to make DisplayInfo(0) return the first line. Its
up to you.

[snip]
#define LINE_SIZE 100;
static char buffer[LINE_SIZE];

Why do you use a file-scope buffer rather than moving this (minus the
'static') into the function?
void DisplayInfo(int linenum)
{
//... file open here, if error, return.
while(fgets(buffer, sizeof(buffer), fp)!=NULL)
{
// check for line number
}
fclose(fp);
}

Any hints/tips will be great. Thanks.

Once you've opened the file, to get to the start of line linenum, you should
discard (linenum - 1) lines (with the first line being numbered 1). This is
equivalent to reading and discarding everything up to and including the
(linenum - 1)th '\n', which you can easily do using getc() or fgetc() in a
loop. Don't forget to check for EOF.

After the above, you are in position to read the desired line. You can use
scanf(), or fgets() followed by sscanf() (preferred), to read and convert
the fields into suitably-typed variables. Or you can continue using
getc()/fgetc(): skip until you see '<', then write each character you read
until you see '>' (this displays the number), and similarly for the other
fields. Again, don't forget error checking.

Alex
 
M

Magix

Magix said:
Hi,

I have following in my data file:

<12345><Benjamin><Single>
<4567><Stephen><Married>
<8383245><George><Married>

I want to write a function that display according to the line number given
(Max line number is 5), without the '<' and '>'
Example:
DisplayInfo(1) will display:-

12345 Benjamin
Single

DisplayInfo(2) will display:-

4567 Stephen
Married

... and so on.

#define LINE_SIZE 100;
static char buffer[LINE_SIZE];

void DisplayInfo(int linenum)
{
//... file open here, if error, return.
while(fgets(buffer, sizeof(buffer), fp)!=NULL)
{
// check for line number
}
fclose(fp);
}


Any hints/tips will be great. Thanks.

or how can I set the file pointer to beginning of line num "linenum" ?
 
M

Mike Wahler

Magix said:
Magix said:
Hi,

I have following in my data file:

<12345><Benjamin><Single>
<4567><Stephen><Married>
<8383245><George><Married>

I want to write a function that display according to the line number given
(Max line number is 5), without the '<' and '>'
Example:
DisplayInfo(1) will display:-

12345 Benjamin
Single

DisplayInfo(2) will display:-

4567 Stephen
Married

... and so on.

#define LINE_SIZE 100;
static char buffer[LINE_SIZE];

void DisplayInfo(int linenum)
{
//... file open here, if error, return.
while(fgets(buffer, sizeof(buffer), fp)!=NULL)
{
// check for line number
}
fclose(fp);
}


Any hints/tips will be great. Thanks.

or how can I set the file pointer to beginning of line num "linenum" ?

Each time you call 'fgets()', the 'file pointer' will point to
the character immediately following a newline ('\n'). So just
loop until you've called 'fgets()' 'linenum' times. (Checking
for errors and end-of-file as you go.)

unsigned int line_count = 0;
while(fgets(buffer, sizeof buffer, fp)
printf("%u line(s) read\n", ++line_count;

-Mike
 
K

Keith Thompson

Mike Wahler said:
Each time you call 'fgets()', the 'file pointer' will point to
the character immediately following a newline ('\n'). So just
loop until you've called 'fgets()' 'linenum' times. (Checking
for errors and end-of-file as you go.)

*Unless* the input line is longer than the buffer.
 
J

Joe Wright

Keith said:
*Unless* the input line is longer than the buffer.

The three cases for fgets():

1. Returns a complete line with \n\0.
2. Returns NULL (End of file or error).
3. Returns a short line. Either buffer too short or line w/o '\n'.

It is this third case that is of greatest interest. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top