473,394 Members | 1,852 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,394 software developers and data experts.

Simple C question. Please Help

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.

Dec 1 '05 #1
4 1600
danu wrote
(in article
<11**********************@g43g2000cwa.googlegroups .com>):
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/Prog...es/C/clc_faq_e
n/

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Dec 1 '05 #2
danu wrote:
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.

--
Simon.
Dec 1 '05 #3
Thanks so much Simon & Randy. I really appreciate both of your comments

Dec 2 '05 #4

"danu" <da****@gmail.com> wrote
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.
Dec 3 '05 #5

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

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
22
by: lokman | last post by:
Hi, In the following code, can someone tell me the difference between *p++ and p++ ? I can see both achieve the same result. Thanks a lot !
3
by: Peter | last post by:
Hello Thanks for reviewing my question. I would like to know how can I programmatically select a node Thanks in Advanc Peter
0
by: Alan Silver | last post by:
Hello, I am having a problem setting and resetting cookies. I'm sure I just doing something really stupid as this is such a basic issue, but I can find any answer. Please can someone help me? ...
9
by: NewInTheGame | last post by:
I'm a beginner in VB.Net. I'm actually taking a class & so far I understand everything, but I suck when it comes to arrays. I'm sure that this is simple (I'm ashamed of asking :oops: )... I...
2
by: Jeff | last post by:
I'm new to vb.net web developer 2005. I assume that this is the correct web site for this question, but please let me know if there is another that is better. I'm having difficulty finding the...
41
by: SkyBlue | last post by:
Hi, can someone explain why the following simple C code segfaulted? I've stared it for 30mins but couldn't find the problem. Thx in advance #include <stdio.h> int main() { char *one; char...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.