Simple C question. Please Help

D

danu

I'm trying to read data from a file (line by line) and trying to assign
the values to the structure members. I'm kind of lost in this case
since the books I'm using for reference doesn't give any examples about
this case.

typedef struct{
char magicNum[2];
int width;
int height;
int maxGrey;
int pixels[ROW][COLUMN];
} image

and the file i'm trying to read is looks like this. (it's a .pgm image)

P2
24 7 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0
0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15

My question is : If I read the input line by line, how do I assign
values to structure members?

I was trying to use some approach like:
while(fgets(buffer, MAX, infile)!=NULL){
strcpy(image.magicNum, getWord(buffer, MAX);
image.width= getWord(buffer, MAX);
....
.....
}
(getWord is a function that I used to get word by word
from the buffer after I executed the fgets function)


I would highly appreciate any help. I don't need the exact source code.
I just need any idea about this procedure.
thanks.
 
R

Randy Howard

danu wrote
(in article
I'm trying to read data from a file (line by line) and trying to assign
the values to the structure members. I'm kind of lost in this case
since the books I'm using for reference doesn't give any examples about
this case.

typedef struct{
char magicNum[2];
int width;
int height;
int maxGrey;
int pixels[ROW][COLUMN];
} image

and the file i'm trying to read is looks like this. (it's a .pgm image)

P2
24 7 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0
0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15

My question is : If I read the input line by line, how do I assign
values to structure members?

I was trying to use some approach like:
while(fgets(buffer, MAX, infile)!=NULL){
strcpy(image.magicNum, getWord(buffer, MAX);
image.width= getWord(buffer, MAX);

You need to parse the lines into subcomponents, and decide which
structure element is associated with which components. You
probably need some error checking code in there as well.

It's a bit of a pain, but there is a standard function which is
used for this sort of thing, strtok(). It has some unexpected
side-effects for the newcomer, so be sure and read the docs
carefully. Once you have the input line broken into pieces, you
then have to convert them into the correct data type for storing
in the array. strtol() and company might be helpful for some of
this.

There have been a lot of 'alternatives' to strtok() coded up
that are easier to use, which you might investigate, but you
need one in source form to have a shot at your code being
portable.

Another approach is to read in the characters one at a time
instead, and parse them by hand as you go, using fgetc()
instead. You might or might not find that an easier way to go.

Some folks would use scanf() or sscanf() instead, but that
function has more 'features' than most people learn in a
lifetime, so I wouldn't recommend until you've been involved in
C programming much longer.

Also, if you haven't read it yet, the comp.lang.c FAQ document
might be very helpful to you as a general resource. Reading it
through front to end is an excellent way to jump start your C
knowledge.

http://docs.mandragor.org/files/Programming_languages/C/clc_faq_e
n/
 
S

Simon Biber

danu said:
I'm trying to read data from a file (line by line) and trying to assign
the values to the structure members. I'm kind of lost in this case
since the books I'm using for reference doesn't give any examples about
this case.

typedef struct{
char magicNum[2];
int width;
int height;
int maxGrey;
int pixels[ROW][COLUMN];
} image

and the file i'm trying to read is looks like this. (it's a .pgm image)

Do you want a program that reads any plain PGM, or just the specific
form below? The header need not be structured as exactly two lines like
this; and it may contain comments. Characters from a ’#’ to the next
'\n', before the maxGrey line, are comments and should be ignored.
P2
24 7 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0
0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15

My question is : If I read the input line by line, how do I assign
values to structure members?

I was trying to use some approach like:
while(fgets(buffer, MAX, infile)!=NULL){
strcpy(image.magicNum, getWord(buffer, MAX);
image.width= getWord(buffer, MAX);
...
....
}

The way your loop stands, you will try to get a magicNum and width from
every line of the file. That's not the right approach; you should grab
the header separately, then go on to read the rest of the file in a
loop. Once you know the image width and height, you know exactly how
long you will loop for.

I know that people often suggest fgets for reading user input, but in
the case of this type of structured input, fscanf is certainly simpler.
This code is untested.

if(5 == fscanf(infile, "%c%c%d%d%d", image.magicNum,
image.magicNum + 1,
&image.width,
&image.height,
&image.maxGrey))
{
int y, x;

assert(image.magicNum[0] == 'P' && image.magicNum[1] == '2');
assert(image.height >= 0 && image.height < ROW);
assert(image.width >= 0 && image.width < COLUMN);

for(y = 0; y < image.height; y++)
{
for(x = 0; x < image.width; x++)
{
fscanf(infile, "%d", image.pixels[y] + x);
}
}
}

If your specification requires you to use fgets, then you can change
this to use a combination of fgets and sscanf. Continue along the line
until there are no more data left, and only then read in another line.

This code also doesn't handle comments, that's an extension you can add.
 
M

Malcolm

danu said:
and the file i'm trying to read is looks like this. (it's a .pgm image)

P2
24 7 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0
0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15

My question is : If I read the input line by line, how do I assign
values to structure members?

I would highly appreciate any help. I don't need the exact source code.
I just need any idea about this procedure.
It's a nuisance. The standard library functions like a defined number of
fields on each line. If you wnat to parse a line with a variable number of
fields you basically have to roll your own.

Fortunately C is such a flexible language that this isn't too hard.
Ypu wnat a function like

int parseints(char *line, int *ret)

that parses integers in the line line, puts them into the array ret, and
then returns the number of integers it found, or -1 on fail. It's a bit
messy to write but not too bad (use a travelling pointer and strtol()).
You must make sure ret is long enough to hold the longest possible input.
You can get round this problem using malloc(), at the cost of even greater
complexity.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top