Announcement: Be excellent to each other.


Caravel Forum : Other Boards : Anything : I survived (with your help)
Page 1 of 2
2
New Topic New Poll Post Reply
Poster Message
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon I survived (0)  
Hey

<insert scared badger sound here, which is like squeaking combined with fast barking>

I need some programing help QUICKLY, or they'll kick me off my studies. I had to pause my studies because of health reasons, and now that I'm back I need to complete some tasks I was simply unable to do earlier. The deadline is 28.II.2005.

I would do this myself except I have very limited time and I'm really poor at C. I can't quite afford experimenting at this point, the stake is too high.
Can I ask you for some guidelines at least ?
-----------------

The program(s) is meant to read short uniform text files (meteo data from a month), extract some numbers (temperature, humidity and some more) and make several files of them. It has to be done in such way so each output file can be used to create one line (graph) using gnuplot.
I can handle the gnuplot, know how it works good enough, and know rules how to write gnuplot input files manually. But I can't really handle C.


- which function should I use for reading meteo files ? They suggest one of scanf family...
- how do I make C write files, cat and so on ? My idea would be to use system(blah blah) function, and some linux text tools I'm familiar with, but then I'd get bashed for writing non-portable program.
- any fast tips you might have for _bad_ C coder ? The most complex programs I've written so far were ones for calculating pi using several various algorithms.
- don't suggest me any very advanced/clever ways because that would look highly suspicious in my case.

I will delete this topic at 28.II, have mercy

[Edited by b0rsuk at Local Time:02-20-2005 at 10:08 PM]

[Edited by b0rsuk at Local Time:02-20-2005 at 10:09 PM]

[Edited by b0rsuk at Local Time:02-28-2005 at 07:18 PM]

[Edited by b0rsuk at Local Time:03-02-2005 at 05:16 PM]

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-20-2005 at 10:05 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
ErikH2000
Level: Legendary Smitemaster
Avatar
Rank Points: 2794
Registered: 02-04-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
b0rsuk wrote:
I need some programing help QUICKLY, or they'll kick me off my studies. I had to pause my studies because of health reasons, and now that I'm back I need to complete some tasks I was simply unable to do earlier. The deadline is 28.II.2005.
Well, there's a number of people here who are good programmers. I can help, and others too, I'm sure. As long as you don't ask for code to be written for you. So you've got a week, and good mentors here. You should be okay.
- which function should I use for reading meteo files ? They suggest one of scanf family...
Try this:
1. Get the size of the meteo file using fopen(), fseek(), and ftell(). (There are more direct ways to get file size, but this works on any platform.)
2. Allocate a buffer that matches the size of the file.
3. Read the file into your buffer using fread().

So you do all that because then you can parse the buffer in memory. It would probably also be possible to use fscanf() to do what you want, but then you can't use all of the parsing techniques available to you like you can when the file is loaded into memory.

For parsing you need to read through the buffer and keep track of state as you go. Some bytes you want to skip over, and other bytes you want to copy into variables for later use. If there is a specification someplace for the format of meteo files then that will be a good thing to have. I can explain parsing code better if that is something you want help with.
- how do I make C write files, cat and so on ? My idea would be to use system(blah blah) function, and some linux text tools I'm familiar with, but then I'd get bashed for writing non-portable program.
There's no need to use other programs to write your files. Use fopen() to open a file for writing (see docs on it). Use fwrite() to write to the end of the file from a buffer.
- any fast tips you might have for _bad_ C coder ? The most complex programs I've written so far were ones for calculating pi using several various algorithms.
Divide this up into three main tasks.
1. Read meteo file into a buffer.
2. Parse the buffer to put all the data you need into variables.
3. Write a new file for gnuplot to use.

The code for each one of these tasks isn't really affected by the others. So think of this as writing three smaller programs instead of one big one.
- don't suggest me any very advanced/clever ways because that would look highly suspicious in my case.
None of the above is very advanced--don't worry.

I will delete this topic at 28.II, have mercy
No need. I like to keep topics around unless there is a good reason to delete them. Just remember to post what happens with your assignment so we know how it went.

-Erik


[Edited by ErikH2000 at Local Time:02-20-2005 at 11:20 PM]

____________________________
The Godkiller - Chapter 1 available now on Steam. It's a DROD-like puzzle adventure game.
dev journals | twitch stream | youtube archive (NSFW)
02-20-2005 at 11:19 PM
View Profile Send Email to User Show all user's posts This architect's holds Quote Reply
eytanz
Level: Smitemaster
Avatar
Rank Points: 2708
Registered: 02-05-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
Whether you should follow Erik's advice depends on how fixed the format of the files are (do you know in advance exactly what information structure they contain?) and how much you know about memory handling. If you don't know/aren't supposed to know about allocating variable-sized buffers, and can get away with it (i.e. you know exactly how much information you need to pick out of the file in advance), just use fscanf() and parse as you go, then use fprintf() to write the new file. If you are supposed to be able to use dynamic memory allocation, Erik's advice is better.

____________________________
I got my avatar back! Yay!
02-21-2005 at 12:38 AM
View Profile Send Private Message to User Show all user's posts This architect's holds Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
Thanks for your input. I'm back from work and about to start messing with code. Thanks for pointing in right (I hope ;) direction, I have internet most of the time + good C book by Kerningham&Ritchie. So finding documentation shouldn't be a problem.
And "plenty" of free time (only 4 hours of work per day, I had to change studies to "compressed weekend edition", as I call them. But saturday and sunday were brutal.)

Here are some examples how do the files look like:

Gdansk-Rebiechowo, Poland (EPGD) 54-23N 018-28E 138M
Mar 28, 2004 - 05:30 PM EDT / 2004.03.28 2130 UTC
Wind: from the W (260 degrees) at 5 MPH (4 KT):0
Visibility: greater than 7 mile(s):0
Sky conditions: mostly cloudy
Temperature: 37 F (3 C)
Dew Point: 33 F (1 C)
Relative Humidity: 86%
Pressure (altimeter): 30.03 in. Hg (1017 hPa)
ob: EPGD 282130Z 26004KT 9999 BKN023 03/01 Q1017
cycle: 21


Gdansk-Rebiechowo, Poland (EPGD) 54-23N 018-28E 138M
Mar 28, 2004 - 07:30 PM EDT / 2004.03.28 2330 UTC
Visibility: greater than 7 mile(s):0
Sky conditions: mostly cloudy
Temperature: 35 F (2 C)
Dew Point: 33 F (1 C)
Relative Humidity: 93%
ob: EPGD 282330Z 9999 BKN030 02/01 1017
cycle: 23


Gdansk-Rebiechowo, Poland (EPGD) 54-23N 018-28E 138M
Mar 28, 2004 - 11:00 PM EDT / 2004.03.29 0300 UTC
Wind: from the WSW (240 degrees) at 3 MPH (3 KT):0
Visibility: 4 mile(s):0
Sky conditions: partly cloudy
Temperature: 35 F (2 C)
Dew Point: 33 F (1 C)
Relative Humidity: 93%
ob: EPGD 290300Z 24003KT 8000 SCT026 SCT200 02/01 1017
cycle: 3


Gdansk-Rebiechowo, Poland (EPGD) 54-23N 018-28E 138M
Mar 29, 2004 - 02:30 AM EDT / 2004.03.29 0630 UTC
Wind: from the WSW (250 degrees) at 8 MPH (7 KT) (direction variable):0
Visibility: 1 mile(s):0
Sky conditions: mostly cloudy
Weather: light rain; mist
Temperature: 39 F (4 C)
Dew Point: 37 F (3 C)
Relative Humidity: 93%
Pressure (altimeter): 30.06 in. Hg (1018 hPa)
ob: EPGD 290630Z 25007KT 220V300 2500 -RA BR BKN003 BKN014 04/03 Q1018
cycle: 6


183 files total. Entire month, more or less. I have no idea why some of them are displayed so bad here, but there are no blank lines.
As you can see, data was taken every 3 hours. Each file may not contain Wind, Weather or Pressure line, it appears. There seems to be no rule, I guess they skipped it when there was nothing interesting happening.
I'll have to make my program insert some data "into blank lines", preferably mean value of previous and next hour. It looks very bad otherwise, as if there was vacuum. Actually I have to do this only for pressure, it will make sense for wind and weather.

As for scared badger sound I was refering to, I was unable to locate it on the net. I found only 2 low grunts. Perhaps only young badgers emit it, and only when taken to a vet while being recorded for Animal Planet channel.

I must not fall asleep. Sleep is the time-killer. Sleep is the little-death that brings total obliteration. I will face my sleep. I will permit it to pass over me. And when it has gone past I will turn the inner eye to see its path. Where the sleep has gone there will be nothing.
Only I will remain.



[Edited by b0rsuk at Local Time:02-21-2005 at 03:01 PM]

[Edited by b0rsuk at Local Time:02-21-2005 at 03:03 PM]

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-21-2005 at 02:55 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
stigant
Level: Smitemaster
Avatar
Rank Points: 1182
Registered: 08-19-2004
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
It looks very bad otherwise, as if there was vacuum

Yeah, that would suck :-)

I would probably break down the parsing into functions like this:

parse_file(...)
parse_segment(...)
parse_header(...)
parse_line(...)
parse_wind(...)
parse_visibility(...)
etc

parse_file would call parse_segment until the end of file is reached

parse_segment would call parse_header to get any relevant data, then call parse_line until the end of the segment is reached

parse_line would read the first word on each line and call the appropriate function: parse_wind, parse_vis, etc depending on the value for that word.

parse_wind etc would read in the wind value and store it in an appropriate place (most likely a global array of wind values, though I shudder at the thought of global variables in general...). ALternatively, it could just return the numeric value, and parse line could deal with storing the values in the appropriate places.

____________________________
Progress Quest Progress
02-21-2005 at 04:06 PM
View Profile Send Private Message to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
-_-

So far I feel more and more stupid with each sentence read. It's some kind of Dark Knowledge (as in: Dark Matter). Tell me, I can't avoid using these tables/pointers ? Every example of use fsomething functions contains that scary * mark....

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-22-2005 at 07:06 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
ErikH2000
Level: Legendary Smitemaster
Avatar
Rank Points: 2794
Registered: 02-04-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
b0rsuk wrote:
So far I feel more and more stupid with each sentence read. It's some kind of Dark Knowledge (as in: Dark Matter). Tell me, I can't avoid using these tables/pointers ?
When you say "tables/pointers" I'm not sure what you mean here. By "pointers", maybe you mean the buffer that I mentioned in my first reply post. It occured to me after reading Eytan's post, that you might be better off with fscanf(), because then you wouldn't need to learn the file size and allocate a buffer. And "tables" haven't been brought up yet in this topic, so I don't know what you mean by that.

But honestly, I feel like you need to commit to an overall plan for your program. Once that is done, if you get stuck on a detail then post here and we can work through it. If you say something like "How do I open a file for reading," or "Why does my program crash when I call fscanf?" then the answer is going to be straightforward.

For a plan, how about:

1. Design data structures that will hold meteo information.
2. Open meteo file for reading (fopen) and parse data into data structures (fscanf). Close file when done (fclose).
3. Open gnuplot file for writing (fopen) and write to file from data structures (fwrite). Close file when done (fclose).

So does that plan at least make sense to you as an overall approach? If you don't know how to do one of the steps, then post here and explain which part exactly gives you problems. I think if we keep talking about general issues, i.e. "Can I do this without tables/pointers?", this won't go very well.

-Erik

____________________________
The Godkiller - Chapter 1 available now on Steam. It's a DROD-like puzzle adventure game.
dev journals | twitch stream | youtube archive (NSFW)
02-22-2005 at 07:38 PM
View Profile Send Email to User Show all user's posts This architect's holds Quote Reply
stigant
Level: Smitemaster
Avatar
Rank Points: 1182
Registered: 08-19-2004
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
Sadly, pointers are a necessary evil in C. Though not in Scheme! Although, if you know the length of the file, or a section of the file, or if you know the MAXIMUM length that each section/line/word can be, you could statically allocate enough room in your program rather than dynamically allocating it:

BYTE * pBytes;
pBytes = new BYTE[200];
fread( ...., pBytes, .... );

vs

BYTE pBytes[200];
fread( ..., pBytes, .... );

ought to do the same thing.

____________________________
Progress Quest Progress
02-22-2005 at 07:39 PM
View Profile Send Private Message to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
I HATE IT !


[Edited by b0rsuk at Local Time:02-23-2005 at 07:16 PM]

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-22-2005 at 07:54 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
Update: on a trail.

I've found curious piece of code in my book:

while(getline(line, sizeof(line)) > 0 {
  if (sscanf(line, "%d %s %d", &day, monthname, &year) == 3)
   printf("poprawna: %s\\n", line); /*format: 25 Grudnia 1988*/
  else if (...)
  else (...)
}


It reads and prints only lines fitting into certain format.

I believe I can shamelessly use it. I guess I would need to use fgets instead of getline, fprintf instead of printf... Of course it would handle only one file - and probably after somehow putting it "between" fopen and fwrite (still have to read about them). Each block beginning with "else(if)" would be used to check if current line fits into one of templates (wind, humidity etc). If it does, fprintf would put it into one of gnuplot data files. Last else would be used for skipping a line. Perhaps I will survive. If no pressure data is given, I'll make it print 0.

Then I'll try to write a program which edits first stage gnuplot pressure files and replaces each 0 with mean value of previous and next "3 hour block". It can get tricky if there are 2+ zeros in a row, but I think it's just a matter of dividing by appropriate value.

So no, no functions so far. I will settle for crude, but working program. Then, if some time remains, I'll try to polish it. I'll start messing with code soon... after I finish rereading the chapter about pointers.
At certain level of learning languages you can understand fairly well, but have problems saying something using proper grammar&vocabulary. I guess this happens with programming languages, too.
UPDATE
Weeeeeee, a segfault !

[Edited by b0rsuk at Local Time:02-23-2005 at 01:11 PM]

[Edited by b0rsuk at Local Time:02-23-2005 at 01:16 PM] :thumbsup

[Edited by b0rsuk at Local Time:02-23-2005 at 05:33 PM]

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-23-2005 at 01:03 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
Here's my greatest achievement so far:

#include <stdio.h>

main ()
{
  int number;
  char c[20];
  
  int *ip;
  
  
  
  scanf ("%s", c);
  printf ("%s\\n", c);

  scanf ("%d", &number);
  printf ("%d\\n", number);
}


I decided to get familiar with basic scanf and printf before I use advanced stuff. Figuring out syntax and ways to store strings and integers took me several hours. This stuff works. I tried to use fscanf and display it with printf, but I can't figure out the damned syntax. I keep getting various compile errors. Even with so-called documentation. It never occured to them to include single example how to use this function. :angry
I bought new motherboard, now my mouse and soundcard doesn't work. Yes, soundcard too. I have to recompile the kernel, this may be messy and take some time. I'm going to fix it later, but...
Does anyone use Cygwin ? How to compile anything with it if there's no "cc" or "gcc" command ? I'm too dumb for it, I guess.
UPDATE
Not to mention I know no convenient commandline file editors. I tend to use mc.

[Edited by b0rsuk at Local Time:02-24-2005 at 01:17 PM]

[Edited by b0rsuk at Local Time:02-24-2005 at 01:22 PM]

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-24-2005 at 01:12 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
Schik
Level: Legendary Smitemaster
Avatar
Rank Points: 5381
Registered: 02-04-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
b0rsuk wrote:
This stuff works. I tried to use fscanf and display it with printf, but I can't figure out the damned syntax. I keep getting various compile errors. Even with so-called documentation. It never occured to them to include single example how to use this function. :angry
To use fscanf, you have to have an open file to scan from. Open a file with fopen(), for example:
  FILE* pFile;
  pFile = fopen("Schik.txt", "r");
That "r" means to open the file for reading. Use "w" if you want to write to a file. Once you have that done, fscanf works just like scanf, except you add an extra argument at the very beginning for the file. To convert your first scanf call to fscanf, given the file I opened up there, you'd have this:
  fscanf(pFile, "%s", c);


____________________________
The greatness of a nation and its moral progress can be judged by the way it treats its animals.
--Mahatma Gandhi
02-24-2005 at 01:16 PM
View Profile Send Private Message to User Send Email to User Show all user's posts High Scores Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
I finally managed to tame fscanf, fopen, and fgets functions. I think sscanf, too.

I intend to use combination of fgets/sscanf.

I ran into a problem: I don't know how to skip strings/integers in scanf-style functions. Most obvious way works, but it feels like an abuse.

I mean something like this: (f)scanf( "%s" &blah )
It assigns first available string to blah... thingy. Ok, so it works. But what if I want to assign third available string into blah ? I can do something like this: scanf("%s %s %s" &blah, &blah, &blah) but it looks like an abuse for me. Is there no smarter way ? There's something about * mark, but I don't quite understand how to use it.

Same goes for fgets: I can load third line by calling it three times. No other way ?

It appears that when I use scanf-style function (or fgets), and then once again, the new "search" starts where the last ended. Any way to prevent it and scan always from beginning ? B0rsuk says I could use fclose fopen combo. Is that necessary ?

If I was able to start each search from beginning, perhaps I could just scan entire file for visiblity line, then for wind, etc.
But it seems tricky with scanf. I miss regular expressions. I tried to find something like "literally_string_1 literally_string_2 %s", &blah) and it works.... only as long as imput stuff starts JUST LIKE that format. fgets/scanf may be better solution.
UPDATE
Yes, yes, it appears sscanf fits here perfectly. I can check same
c[20] thingy over and over, because it's just like fclose+fread for fscanf.
But is there any smart way to skip strings/lines ?

[Edited by b0rsuk at Local Time:02-24-2005 at 07:19 PM]

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-24-2005 at 07:06 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
Schik
Level: Legendary Smitemaster
Avatar
Rank Points: 5381
Registered: 02-04-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
b0rsuk wrote:
I ran into a problem: I don't know how to skip strings/integers in scanf-style functions. Most obvious way works, but it feels like an abuse.

I mean something like this: (f)scanf( "%s" &blah )
It assigns first available string to blah... thingy. Ok, so it works. But what if I want to assign third available string into blah ? I can do something like this: scanf("%s %s %s" &blah, &blah, &blah) but it looks like an abuse for me. Is there no smarter way ? There's something about * mark, but I don't quite understand how to use it.
Just do
scanf("%*s, %*s, %s", blah);
to skip over the first two strings.
Same goes for fgets: I can load third line by calling it three times. No other way ?
For fgets, nope, you'll have to get each line.
It appears that when I use scanf-style function (or fgets), and then once again, the new "search" starts where the last ended. Any way to prevent it and scan always from beginning ?
You said you're using fgets and sscanf (which is what I would do as well). sscanf will always start at the beginning of the string you pass into it. fgets is reading from a file, and the FILE* will keep track of where you are in the file. If you *really* need to restart from the beginning of the file, look up fseek. But I really don't see any reason you would need to do that for this project.

____________________________
The greatness of a nation and its moral progress can be judged by the way it treats its animals.
--Mahatma Gandhi
02-24-2005 at 07:21 PM
View Profile Send Private Message to User Send Email to User Show all user's posts High Scores Quote Reply
ErikH2000
Level: Legendary Smitemaster
Avatar
Rank Points: 2794
Registered: 02-04-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
b0rsuk wrote:
I mean something like this: (f)scanf( "%s" &blah )
It assigns first available string to blah... thingy. Ok, so it works. But what if I want to assign third available string into blah ? I can do something like this: scanf("%s %s %s" &blah, &blah, &blah) but it looks like an abuse for me. Is there no smarter way ? There's something about * mark, but I don't quite understand how to use it.
Maybe there is something you can do with scanf itself, but it should also work to put your calls to scanf in a loop. If you want to read something 3 times, then:

  int i;
  for (i = 0; i < 3; ++i)
    scanf("%s", &blah);

It appears that when I use scanf-style function (or fgets), and then once again, the new "search" starts where the last ended. Any way to prevent it and scan always from beginning ? B0rsuk says I could use fclose fopen combo. Is that necessary ?
You can use fseek() to set the position in the file wherever you like. But if possible, try to write your program to parse the data in one pass--it is more efficient and better coding style. (I know, you are just trying to get the stupid thing done.)
If I was able to start each search from beginning, perhaps I could just scan entire file for visiblity line, then for wind, etc.
Yeah, you might get a bad grade on your assignment with that. Like I said, it is much better coding style to read and parse the data in one pass.
fgets/scanf may be better solution.
But is there any smart way to skip strings/lines ?
Well, I could be wrong but it looks like in meteo files there are no pieces of information that are broken up in the middle by a line (CR/LF chars). You could read in one line at a time with fgets(), skip the lines you don't want, and parse the lines you want. Use scanf() (not fscanf() )to parse the lines that are read into a buffer by fgets().

-Erik

____________________________
The Godkiller - Chapter 1 available now on Steam. It's a DROD-like puzzle adventure game.
dev journals | twitch stream | youtube archive (NSFW)
02-24-2005 at 07:30 PM
View Profile Send Email to User Show all user's posts This architect's holds Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
Schik wrote:
Just do
scanf("%*s, %*s, %s", blah);
to skip over the first two strings.

As far as I remember I was getting compile errors this way. It appears I have to give it as many variables as many strings I read. So I think I need "blah blah blah".

For fgets, nope, you'll have to get each line.

Huh ? I can "skip" by getting a line, and "overwriting" it by getting again (without parsing).

You said you're using fgets and sscanf (which is what I would do as well). sscanf will always start at the beginning of the string you pass into it.

At the moment I was writing it I wasn't sure for 100%, it started working only recently. But there's another problem with sscanf... I think... damn. I may know a workaround. I'll have to check, but can't do it now.

Maybe there is something you can do with scanf itself, but it should also work to put your calls to scanf in a loop. If you want to read something 3 times, then:

int i;
for (i = 0; i < 3; ++i)
scanf("%s", &blah);

Now this looks like Obfuscated C challange for me, almost. I prefer overwriting... No, no just kidding, but my dumber solution looks cleaner. I would keep looking at this loop and asking myself "huh ? what for ? aaah, right, to get the 3rd one". With dumber solution I can determine at first glance.

Yeah, you might get a bad grade on your assignment with that. Like I said, it is much better coding style to read and parse the data in one pass.

Yeah, I may also get a bad grade for lack of makefile, lack of make clean, and details like "I was supposed to write this program almost a year ago". Formally speaking I didn't waste a year, because they allowed me to take the holidays.

Current problem:

#include <stdio.h>

main ()
{
  int number;
  char c[80];
  int upton=80;
  
int *ip;
  
  
FILE *pFile;
pFile = fopen("ofiara.met", "r");  

  fgets (c ,upton, pFile);  /* skip two first lines */
  fgets (c ,upton, pFile);  
 
  fgets (c ,upton, pFile);  /* print third line */
  sscanf (c, "%d", &number);
  printf ("%d\\n", number);
  
/*  scanf ("%s", c); , wczytuje stringa (char) na zmienna(tablice) c [C*/
}



This program reads 1st string from 3rd line of a file. Well, I mean it worked well for strings. I hoped I could get away with just getting strings, even if they're numbers. I don't need to make any calculations with them, after all, just write to gnuplot imput file.
But there are strings, technically speaking, like (3C. (temperature) I can't quite get only 3 from it, I have to take whole (3C if I take strings.

So I modified my problem to get fish out an integer (number) from that line (c). I guess it is possible, because I have similar example in my book, but they use getline() function, some kind simplified/modified gets.
Judging from what it prints it's some kind of problem with types, (large negative number, if I type letters into integer-eating scanf it behaves like this, too) but I have no idea why. It's almost the same as in book.

I may just allow it to take "filthy (3C strings), write to a file, and make another program using getchar putchar stuff to remove all ( marks, and C letters, and so on.... It's simpler than this stuff with scanf, but I don't quite remeber it so extra learning would be necessary.
Besides, there are such things like ambitions. :?

[Edited by b0rsuk at Local Time:02-24-2005 at 08:37 PM]

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-24-2005 at 08:34 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
Schik
Level: Legendary Smitemaster
Avatar
Rank Points: 5381
Registered: 02-04-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
b0rsuk wrote:
Just do
scanf("%*s, %*s, %s", blah);
to skip over the first two strings.

As far as I remember I was getting compile errors this way. It appears I have to give it as many variables as many strings I read. So I think I need "blah blah blah".
Nope. Unless you're using a broken compiler.
For fgets, nope, you'll have to get each line.
Huh ? I can "skip" by getting a line, and "overwriting" it by getting again (without parsing).
Well, I said you have to get each line. Not that you have to parse it.
Current problem:

This program reads 1st string from 3rd line of a file. Well, I mean it worked well for strings. I hoped I could get away with just getting strings, even if they're numbers. I don't need to make any calculations with them, after all, just write to gnuplot imput file.
But there are strings, technically speaking, like (3C. (temperature) I can't quite get only 3 from it, I have to take whole (3C if I take strings.

So I modified my problem to get fish out an integer (number) from that line (c). I guess it is possible, because I have similar example in my book, but they use getline() function, some kind simplified/modified gets.
Judging from what it prints it's some kind of problem with types, (large negative number, if I type letters into integer-eating scanf it behaves like this, too) but I have no idea why. It's almost the same as in book.
Look at your input files. Of the ones you posted earlier, I don't see any that start with a number, and that's what you're trying to do - parse the first thing on the line as a number. Also, of the 4 input files, three have Wind in the third line, and one has Visibility. So you can't make an assumption about what's on the third line anyways. What you should probably do is parse out the first string, and depending on what it is, do various things - discard it if you don't care, or parse the rest of the line in various ways if you DO care.


____________________________
The greatness of a nation and its moral progress can be judged by the way it treats its animals.
--Mahatma Gandhi
02-24-2005 at 08:56 PM
View Profile Send Private Message to User Send Email to User Show all user's posts High Scores Quote Reply
ErikH2000
Level: Legendary Smitemaster
Avatar
Rank Points: 2794
Registered: 02-04-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
b0rsuk wrote:
Just do
scanf("%*s, %*s, %s", blah);
to skip over the first two strings.

As far as I remember I was getting compile errors this way. It appears I have to give it as many variables as many strings I read. So I think I need "blah blah blah".
Your compiler isn't going to check that the format parameter ("%*s, %*s, %s") matches the following arguments. If you have a compile error, then there is something else you've gotten wrong with the syntax. You only need to supply one variable for the last "%s" to go into. The preceding "%*s" aren't copied into variables because they have an asterisk. If you don't put enough variables at the end to match your format parameter, then you won't get a compile error, but you might get a segment fault when you run your program.
Huh ? I can "skip" by getting a line, and "overwriting" it by getting again (without parsing).
Yes, that works fine.
Maybe there is something you can do with scanf itself, but it should also work to put your calls to scanf in a loop. If you want to read something 3 times, then:
Now this looks like Obfuscated C challange for me, almost. I prefer overwriting... No, no just kidding, but my dumber solution looks cleaner. I would keep looking at this loop and asking myself "huh ? what for ? aaah, right, to get the 3rd one". With dumber solution I can determine at first glance.
I don't think my idea here was as good as Schik's, which is to use "*" in your format parameter to sscanf(). But my larger point is really that you can break up one complicated call to sscanf() into smaller calls if you want. That isn't obfuscated C; that's just a technique that can be useful.
Yeah, I may also get a bad grade for lack of makefile, lack of make clean, and details like "I was supposed to write this program almost a year ago". Formally speaking I didn't waste a year, because they allowed me to take the holidays.
Okay, I understand your priorities. But it really shouldn't be more work to do it the right way, and you will be a better programmer for it. I understand that if this stands in the way of you getting your assignment done, the best choice is to do it the "wrong" way and save your academic butt.
So I modified my problem to get fish out an integer (number) from that line (c). I guess it is possible, because I have similar example in my book, but they use getline() function, some kind simplified/modified gets.
Judging from what it prints it's some kind of problem with types, (large negative number, if I type letters into integer-eating scanf it behaves like this, too) but I have no idea why. It's almost the same as in book.
Show the code and maybe I can point out the problem.
I may just allow it to take "filthy (3C strings), write to a file, and make another program using getchar putchar stuff to remove all ( marks, and C letters, and so on.... It's simpler than this stuff with scanf, but I don't quite remeber it so extra learning would be necessary.
Ah, but then you'll hit some other problem with your different approach and get stuck there. The plan you began with will work. I caution you against abandoning your plan midway for another plan that sounds more complex than the first. Post more specifically about the problems you are having with sscanf() and include code, and we can work through them.

Or not! But that is my recommendation.

-Erik

____________________________
The Godkiller - Chapter 1 available now on Steam. It's a DROD-like puzzle adventure game.
dev journals | twitch stream | youtube archive (NSFW)
02-24-2005 at 09:10 PM
View Profile Send Email to User Show all user's posts This architect's holds Quote Reply
ErikH2000
Level: Legendary Smitemaster
Avatar
Rank Points: 2794
Registered: 02-04-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
B0rsukm, Schik actually answered your question about problems scanning for numbers, so ignore my request to post source code for that.

In general, if you say something doesn't work and want help with it, please post the source code that gives you problems too and it will help us get through it quickly.

-Erik

____________________________
The Godkiller - Chapter 1 available now on Steam. It's a DROD-like puzzle adventure game.
dev journals | twitch stream | youtube archive (NSFW)
02-24-2005 at 09:13 PM
View Profile Send Email to User Show all user's posts This architect's holds Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
Just do
scanf("%*s, %*s, %s", blah);
to skip over the first two strings.

My fault, somehow I didn't notice the asterisk. Programming in C makes me want play Crimsonland. My head hurts. I will start "coding" since morning, because I go to work at 16:00 tomorrow.

Look at your input files. Of the ones you posted earlier, I don't see any that start with a number, and that's what you're trying to do - parse the first thing on the line as a number.

:look
Somehow I assumed it would grab first number available.... If it works this way, there may be no point in scanning integers. Hmm.

Also, of the 4 input files, three have Wind in the third line, and one has Visibility. So you can't make an assumption about what's on the third line anyways.

I was going to use something along the lines while fgets blah blah != NULL.
Sscanf blah blah sscanf blah blah...
I just wanted to successfully parse single line before constructing something more complex.

What you should probably do is parse out the first string, and depending on what it is, do various things - discard it if you don't care, or parse the rest of the line in various ways if you DO care.


There's a nice polish saying "Don't hold too many magpies by tails", more less.
Ah, but then you'll hit some other problem with your different approach and get stuck there. The plan you began with will work. I caution you against abandoning your plan midway for another plan that sounds more complex than the first.
I don't mean abandoning it, I mean that I will break it into two stages if really necessary (write stage one data files, remove certain ugly stuff from files).
I already assumed long time ago I will need to make a separate program for replacing 0's (pressure) with mean value of previous and next elements. If I do it in same program, I'll get lost in bugs. It may be just easier to understand for me.

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-24-2005 at 10:56 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  

Mine !



#include <stdio.h>
#include <string.h>

#define twodigits 2

main()
{
    int number;
    int upton = 80;
      
    char line[80];
    char word1[20];
    char word2[20];
    char word3[20];
    
    

    FILE *pFile;
    pFile = fopen("ofiara.met", "r");
    
    while (fgets(line, upton, pFile) != NULL) {	/* skip two first lines */

	if (sscanf
	    (line, "Wind: from the %s (%s degrees) at %s", word1, word2,
	     word3) == 3) {
	    printf("Wind from: %s \\n", word1);
	    printf("Direction (deg): %s \\n", word2);
	    printf("Wind speed: %sMPH \\n", word3);

	}
	/* UWAGA - linia vis w 2 wersjach */
	else if (sscanf(line, "Visibility: greater than %s", word1) == 1) {
	    printf("Visibility > %smiles \\n", word1);
	}
	else if (sscanf(line, "Visibility: %s", word1) == 1) {
	    printf("Visibility: %smiles \\n", word1);
	}
        else if (sscanf(line, "Sky conditions: %s %s", word1, word2) == 2) {
	    printf("Sky conditions: %s %s \\n", word1, word2);
	}
        else if (sscanf(line, "Weather: %s %s %s", word1, word2, word3) >= 1) {
	    printf("Weather: %s %s %s \\n", word1, word2, word3);    
	}
	else if (sscanf(line, "Temperature: %s", word1) == 1) {
	    printf("Temperature: %sF \\n", word1);
	} 
	else if (sscanf(line, "Dew Point: %s", word1) == 1) {
	    printf("Dew Point: %sF \\n", word1);
	} 
	else if (sscanf(line, "Relative Humidity: %s", word1) == 1) {
	    /* here */
	    printf("Relative Humidity: %s \\n", word1);
	}
	else if (sscanf(line, "Pressure %*s %*s %*s %*s (%s", word1) == 1) {
	    printf("Pressure: %shPa \\n", word1);    
	}
    }

}


Hey

I'll soon go to work.

I stumbled upon some problems. The program I pasted here works,(parses single file) but with a glitch or two. It if you examine any meteo file, you'll see that humidity line goes like this: "Relative Humidity: 93%". As you may expect, it takes "93%" as a string. What if I want to take only number, without % ? I tried using strncpy from string.h, I inserted wrote "strncpy(line, line, twodigits);" /* here */ and ... nothing happened. That hideous % mark again. I though I could copy only 2 first characters from line to line. What's wrong with it ? Or with me ?
I also though about something like "line[3] = \\0", or another mark, whatever. What would be the best way ?

The program parses "Weather" line only barely. It's very tricky, because it may look like this:
Weather: mist
Weather: light rain, mist
Weather: light rain

....
Can you imagine any way to parse weather line, sane way ?

I'd like to assign numbers to certain values of strings. For example if string=mist (I could use string comparing function from string.h), fprintf would print 1. Every possible weather would have a number. How to do this ?

How do I define an array (c[20]-like stuff) which contains "mist" since start of program, without taking it from some kind of scanf function ?

Sky conditions parsing is simplified, too. It merely prints words, and I could assign a number to each sky condition (never more than one simultaneously, as opposed to weather), if I knew how.

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-25-2005 at 02:33 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
stigant
Level: Smitemaster
Avatar
Rank Points: 1182
Registered: 08-19-2004
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
I stumbled upon some problems. The program I pasted here works,(parses single file) but with a glitch or two. It if you examine any meteo file, you'll see that humidity line goes like this: "Relative Humidity: 93%". As you may expect, it takes "93%" as a string. What if I want to take only number, without % ? I tried using strncpy from string.h, I inserted wrote "strncpy(line, line, twodigits);" /* here */ and ... nothing happened. That hideous % mark again. I though I could copy only 2 first characters from line to line. What's wrong with it ? Or with me ?
I also though about something like "line[3] = \\0", or another mark, whatever. What would be the best way ?

else if (sscanf(line, "Relative Humidity: %s", word1) == 1) {
/* here */
printf("Relative Humidity: %s \\n", word1);
}

If you change your sscanf line to "Relative Humidity: %d%s" it should read the number up to the % sign, then read the % and everything after it. You'll have to add another parameter to hold the number:

int number1;
sscanf(line, "Relative Humidity:%d%s\\n", &number1, word1);

alternatively, I snipped this from msdn:
The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%.

So I think you do this:

sscanf(line, "Relative Humidity: %s%%%s\\n", word1, word2)

the first %s will read everything between the : and the %, and store it in word1. The %% will make the function skip over the %, and the second %s\\n will read everything to the end of the line (you may not need that part...) into word2.

I don't have a compiler to try this out at work, so I may be wrong, but its something you should be able to try quickly.

____________________________
Progress Quest Progress
02-25-2005 at 02:46 PM
View Profile Send Private Message to User Show all user's posts Quote Reply
stigant
Level: Smitemaster
Avatar
Rank Points: 1182
Registered: 08-19-2004
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
as a last resort, you could use this function to remove the % sign:

void remove_percent( char * word ) {
int i;

/* scan down the word until you find a % and set it to '\\0' and return */
for ( i = 0; word[i] != '\\0'; i++ ) {
  if ( word[i] == '%' ) {
     word[i] = '\\0';
     return;
  }
}


Then do your line as above:

sscanf( line, "Relative Humidity: %s", word1 );
remove_percent(word1);

(Again, this is untested code...)


Edit: my [i] got interpreted as itallics even though it was in a [code] segment... is that intended behavior?

[Edited by stigant at Local Time:02-25-2005 at 02:54 PM]

____________________________
Progress Quest Progress
02-25-2005 at 02:51 PM
View Profile Send Private Message to User Show all user's posts Quote Reply
stigant
Level: Smitemaster
Avatar
Rank Points: 1182
Registered: 08-19-2004
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
Can you imagine any way to parse weather line, sane way ?

Do you mean short of a large if-then-else structure to see what the actual values are? No.

If you mean, is there way to simple way to use sscanf to parse out the (possibly) multiple conditions, I would say No again. However, there's another function called strtok which is a little bit more powerful. Here's a page in msdn that describes its usage:
msdn

____________________________
Progress Quest Progress
02-25-2005 at 03:02 PM
View Profile Send Private Message to User Show all user's posts Quote Reply
trick
Level: Legendary Smitemaster
Rank Points: 2580
Registered: 04-12-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
I'd just like to point out that if you're doing stuff like...
scanf("%s %s %s", &blah, &blah, &blah)
...the result is actually undefined. I'm pretty sure it's up to the implementation what order to process the parameters. It's similar to doing this:
int i = 0;
printf("%i %i %i", ++i, ++i, ++i);
...which is also undefined. Which argument is incremented first ?

Also, I don't think strtok is part of the ANSI C standard. It is part of POSIX, though.

In any case, stigant's sscanf-line should work (modified to discard the ending string):
sscanf(line, "Relative Humidity: %d%*s\\n", &number1);

By the way, fun fact: A single space in the format string of the scanf-family matches any amount of whitespace in the input, including none. For example, the sscanf-line above matches all of the following strings:
Relative Humidity: 9%
Relative       Humidity:          30 per cent
RelativeHumidity:990000e^pi


:)

- Gerry
02-26-2005 at 02:23 AM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
Finally ! I got rid of dreaded %.
But it seems I'll have to use word[not_i] = '\\0' anyway, when I get words like "rain," from weather line. I don't think I can scan semicolon into integer. Actually we use semicolons istead of dots here in Poland (for numbers). But C doesn't seem to care ;]

One problem remained unsolved. I'd like to check if, for instance, word1 = 'mist', and in such case print some number.
I tried something like if (word1 == '100'), but it whined I compare pointer to integer.

I know how compare single chars from an array: word1[not_i] == 'c'. Not tested but should work. How do I compare entire word at once ? I know there's a strcmp function in string.h which allows to compare one string to another ... string.
But I know no easy way to make word1 contain word "kitten".
First masochistic way would be comparing letter after letter:
(word1[0] == 'r' && word1[1] == 'a' && word1[2] == 'i' && word1[3] == 'n')
Stupid, eh ?

Then how about makin "weather" file manually:
rain
light rain
mist
blah

Then scan each line into another pointer:
weather1[10]
weather2[10]

....
Then use strcmp function to compare word1 with weather1, weather2, ... and put it ito some kind of if else if monstrosity. A bit better, but still stupid. Any ideas ?

[Edited by b0rsuk at Local Time:02-26-2005 at 03:34 PM]

[Edited by b0rsuk at Local Time:02-26-2005 at 03:36 PM]

[Edited by b0rsuk at Local Time:02-26-2005 at 03:38 PM]

[Edited by b0rsuk at Local Time:02-26-2005 at 03:39 PM]

[Edited by b0rsuk at Local Time:02-26-2005 at 03:41 PM]

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-26-2005 at 03:33 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
stigant
Level: Smitemaster
Avatar
Rank Points: 1182
Registered: 08-19-2004
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
I'm a little confused... you want a way to make word1 be "kitten"?

how about:
char word1[100];
strcpy(word1, "kitten");

Edit:
Also, in C++, you can do this:
char word1[] = "kitten";

but I don't think that will work in C since you can't do a variable assignment at the same time as a variable declaration (correct me if I'm wrong, oh C gurus).

Edit:
Rereading your post, I think what you are trying to do is create an array of strings to use in your comparison block. But you can use literal strings in the strcmp function:

if ( strcmp(word1, "light rain") == 0 ) { ... }

Its probably not great programming style (typically you want to use constants so that if you decide to change "light rain" to "sprinkles" at a later date you only have to do it in one place), but if this is just a down and dirty programming assignment, you can probably get away with it.

[Edited by stigant at Local Time:02-26-2005 at 04:19 PM]

____________________________
Progress Quest Progress
02-26-2005 at 04:11 PM
View Profile Send Private Message to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
stigant wrote:

if ( strcmp(word1, "light rain") == 0 ) { ... }

Its probably not great programming style (typically you want to use constants so that if you decide to change "light rain" to "sprinkles" at a later date you only have to do it in one place), but if this is just a down and dirty programming assignment, you can probably get away with it.

[Edited by stigant at Local Time:02-26-2005 at 04:19 PM]

Thanks for help.
So the first way could be better, after all ? I'd have all the strings in one place at beginning of the file, plain and clear. No need to change them anyway, unless I want to reuse as many variables as possible. It would be no good in this case, I suppose.

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-26-2005 at 05:17 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (+1)  
Silence ! B0rsuk speaks:

#include <stdio.h>
#include <string.h>

/*clear sky*/
#define vis1 0
#define vis2 1
#define vis3 2
#define vis4 3
#define maxl 80

/*mist*/
#define wea1 0
/*fog*/
#define wea2 1
/*freezing fog*/
#define wea3 2
/*shallow fog*/
#define wea4 3
/*patches of fog*/
#define wea5 4
/* light rain, zeby sie odroznialo od zgrai mgiel */
#define wea6 8
/* chmurki, zeby sie odroznialo */
#define wea7 12
/* light rain showers, zeby sie odroznialo od zgrai mgiel */
#define wea8 9


main()
{
    int smiec;

    /* przyporzadkowywane odpowiedniemu niebu/pogodzie */

    char line[80];
    char word1[20];
    char word2[20];
    char word3[20];

    FILE *pFile;
    pFile = fopen("ofiara.met", "r");

    /*    
       windchill ! (385 b)
     */
    while (fgets(line, maxl, pFile) != NULL) {
	/* zczytywanie i przepisywanie wiatru */
	if (sscanf
	    (line, "Wind: from the %*s (%s degrees) at %s", word1,
	     word2) == 2) {
	    printf("Direction (deg): %s \\n", word1);
	    printf("Wind speed: %s MPH \\n", word2);

	}
	/* zmienny wiatr */
	else if (sscanf(line, "Wind: Variable at %s", word1) == 1) {
	    printf("Wind variable, at %sMPH \\n", word1);
	}
	/* Widocznosc - wiecej niz... */
	else if (sscanf(line, "Visibility: greater than %s", word1) == 1) {
	    printf("Visibility > %s miles \\n", word1);
	    /* widocznosc */
	} else if (sscanf(line, "Visibility: %s", word1) == 1) {
	    printf("Visibility: %s miles \\n", word1);
	    /* niebo */
	} else
	    if ((sscanf(line, "Sky conditions: %s %s", word1, word2) == 2)
		|| (sscanf(line, "Sky conditions: %s %s", word1, word2) ==
		    1)) {
	    if (strcmp(word1, "clear") == 0)
		printf("Sky conditions: CLEAR %d \\n", vis1);
	    else if ((strcmp(word1, "mostly") == 0)
		     && (strcmp(word2, "clear") == 0))
		printf("Sky conditions: MOSTLY CLEAR %d \\n", vis2);
	    else if ((strcmp(word1, "partly") == 0)
		     && (strcmp(word2, "cloudy") == 0))
		printf("Sky conditions: PARTLY CLOUDY %d \\n", vis3);
	    else if ((strcmp(word1, "mostly") == 0)
		     && (strcmp(word2, "cloudy") == 0))
		printf("Sky conditions: MOSTLY CLOUDY %d \\n", vis4);
	    else
		printf("Nieznane zachmurzenie");
	    /* pogoda */
	} else if (sscanf(line, "Weather: %s %s %s", word1, word2, word3)
		   >= 1) {
	    if (strncmp(line, "Weather: mist", 13) == 0) {
		if ((strcmp(word2, "shallow") == 0)
		    && (strcmp(word3, "fog") == 0))
		    printf("Weather: mist, shallow fog %d %d", wea1, wea4);
		else
		    printf("Weather: mist %d \\n", wea1);
	    }
	    if (strncmp(line, "Weather: fog", 12) == 0)
		printf("Weather: fog %d \\n", wea2);
	    if (strncmp(line, "Weather: freezing fog", 21) == 0)
		printf("Weather: fog %d \\n", wea3);
	    if (strncmp(line, "Weather: shallow fog", 20) == 0)
		printf("Weather: shallow fog %d \\n", wea4);
	    if (strncmp(line, "Weather: patches of fog", 23) == 0)
		printf("Weather: patches of fog %d \\n", wea5);
	    if (strncmp(line, "Weather: light rain", 19) == 0) {
		if (strcmp(word3, "showers") == 0)
		    printf("Weather: light rain %d \\n", wea8);
		else if (strcmp(word3, "mist") == 0)
		    printf("Light rain;mist %d %d \\n", wea8, wea1);
		else
		    printf("Weather: light rain %d \\n", wea6);
	    }
	    if (strncmp(line, "Weather: Cumulonimbus clouds observed", 37)
		== 0)
		printf("Chmurki %d \\n", wea7);
	    /* Temperatura w stopniach Fahrenheita, zeby bylo dokladniej */
	} else if (sscanf(line, "Temperature: %s", word1) == 1) {
	    printf("Temperature: %sF \\n", word1);
	    /* Windchill jakis... */
	} else if (sscanf(line, "Windchill: %s", word1) == 1) {
	    printf("Windchill: %sF \\n", word1);
	    /* Punkt rosy, cokolwiek by to bylo */
	} else if (sscanf(line, "Dew Point: %s", word1) == 1) {
	    printf("Dew Point: %sF \\n", word1);
	    /* wilgotnosc */
	} else if (sscanf(line, "Relative Humidity: %d%*s", &smiec) == 1) {
	    printf("Relative Humidity: %d\\n", smiec);
	    /* cisnienie */
	} else if (sscanf(line, "Pressure %*s %*s %*s %*s (%s", word1) ==
		   1) {
	    printf("Pressure: %shPa \\n", word1);
	}
    }
return 0;
}


Now go ahead and praise me.
It reads one file correctly, taking even weird stuff like Windchill into account. Damned if I know what is it.
It replaces things like "light rain" or "mist" with defined numbers.
It works only for one file defined it source code. If I want to allow for parameters (filenames), I should make entire program a function, right ?
Now the fun stuff: with mess like this, probably the best way to make it write to files would be taking (almost) every printf line between "fopen" and "fclose", right ? Not to mention changing it to fprintf...
As if I had not enough of monotonous work today already....

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-27-2005 at 04:09 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: SOS help with ANSI C before 28.II.2005 ! (0)  
My program can process only one file at a time, segfaults if parameter given doesn't match valid filename, looks ugly, doesn't print data in format understandable by Gnuplot (didn't have enough time to learn how point coordinates should be given). And puts data on stdin instead of files (i'm working on it right now, as it seems its most lacking feature).

I have to show it tommorow. Do you think I will be spared ? -_-
At least it compiles without any warnings when I use cc -Wall. Yeah, VERY funny.

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
02-27-2005 at 08:07 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
Page 1 of 2
2
New Topic New Poll Post Reply
Caravel Forum : Other Boards : Anything : I survived (with your help)
Surf To:


Forum Rules:
Can I post a new topic? No
Can I reply? No
Can I read? Yes
HTML Enabled? No
UBBC Enabled? Yes
Words Filter Enable? No

Contact Us | CaravelGames.com

Powered by: tForum tForumHacks Edition b0.98.8
Originally created by Toan Huynh (Copyright © 2000)
Enhanced by the tForumHacks team and the Caravel team.