Clockowl 56 Posting Whiz

Not to be an asshole but poster says he wants to capture and store image data from a webcam. That's pretty specific, not?

jephthah commented: haha +2
Clockowl 56 Posting Whiz

http://msdn.microsoft.com/en-us/library/ms630343%28VS.85%29.aspx

I think you should start there, but it's gonna be quite a lot of work I'm afraid. I've never done it myself, but that link seems to be good. If you are on Windows, that is.

If you like examples and hadn't found it yet: http://msdn.microsoft.com/en-us/library/ms630343%28VS.85%29.aspx

Clockowl 56 Posting Whiz

Well, I tend to use them when it saves me adding numerous parameters to some functions. I think you can use global variables, but you should at least limit most of them to one source file with the static modifier (or without, since it's the default). When you write a throw away program, it's okay to use them, but globals aren't built to last.

Clockowl 56 Posting Whiz

I've already explained it. The bold part.

C does all your pointermath for you. If you create a pointer to a structure and then increment it, it adds sizeof(structure) bytes to the pointer.

For example:
When an array is declared, the name of the array without element brackets is a pointer to the first element.

struct productos *productPointer = p;

after this, p[1] is the same element as *(productPointer + 1), p[2] == *(productPointer + 2), etc.
However, it's often easier not to dereference the pointer like that, instead use the arrow operator. This operator sees the previous variable as a pointer to a structure.
p[1].precio is the same as (productPointer + 1)->precio.

you can also change productPointer itself with increments and decrements. Just rememeber that you are not incrementing in bytes, but in sizes of the type you're pointing to. In this case, that's sizeof(struct productos).

Clockowl 56 Posting Whiz

No, people on OIFY do. ;)

... It's from my friends. I'm not an active OIFY member due to too much pictures of animals like sung of in Bloodhound Gans's "The Bad Touch"

Clockowl 56 Posting Whiz

No, people on OIFY do. ;)

... It's from my friends. I'm not an active OIFY member due to too much pictures of animals like sung of in Bloodhound Gans's "The Bad Touch"

Clockowl 56 Posting Whiz

...and solved again.

Found out that I only need to include the header files. I think I understand how it works now. Compiler compiles all the .c source files to .o object files. The linker then looks to the code and with header files determines what object links to the other ones. So main doesn't need to know WHAT function X does, just what it's name is (so the linker can look it up) and what it's parameters are so it can check parameters.

Phew. It works. yeey.

Byebye. :D

Clockowl 56 Posting Whiz

I almost fixed it, but there's one problem I can't seem to fix: Multiple definitions. I've marked every header file with it's own "header notifier", but the compiler/linker can't help but scream:

obj\Debug\main.o||In function `drawScene':|
C:\Code\TetriminoFalls\renderCode.c|6|multiple definition of `drawScene'|
obj\Debug\renderCode.o:C:\Code\TetriminoFalls\renderCode.c|6|first defined here|
obj\Debug\main.o||In function `drawMenu':|
C:\Code\TetriminoFalls\renderCode.c|14|multiple definition of `drawMenu'|
...
||=== Build finished: 10 errors, 0 warnings ===|

Any ideas on howto? I've uploaded the update version with header files!

Thanks in advance, again,

PS:
Small view of a header file:

#ifndef TETRIMINORENDERH
void drawScene(void);
void drawMenu(enum menuOptions menuType);
void drawText(GLint fontlist, char *text, GLfloat x, GLfloat y, GLfloat z);
#define TETRIMINORENDERH
#endif
Clockowl 56 Posting Whiz

I figured it out thanks to this nice tutorial. Thanks for your help.

Clockowl 56 Posting Whiz

Thanks, I'm not Japanese but Dutch. Where'd you get that idea?

Anyway,

I know that you can use multiple files like that, and I've used them like that, putting functions in one file, then including it. Problem is I want some globals to be used in the included files as well. Example:

main.c

enum menuOptions {NO_MENU, MAIN_MENU} menu = MAIN_MENU;
...
//herein all the draw functions
#include "renderCode.c"

renderCode.c

void drawMenu(extern enum menuOptions menuType){
...
  switch(menuType){
    case MAIN_MENU:
...

I want to be able to use the enum menuOptions in renderCode.c AND in main.c. I tried some things with extern, but I couldn't figure it out. I know it's possible, but how?

Thanks in advance,

Clockowl 56 Posting Whiz

Nice job reviving a 2 year old thread? ;)

Clockowl 56 Posting Whiz

hai.

I'm trying to compile a program split up across multiple files. The code is far from complete, but I noticed lots of compiler warnings and errors. This ought to be due to splitting the code up: I thought the preprocessor simply copy and pasted the code into main.c when I used #include.

It seems it does not, but I can't get it to work. The code is included in a zip file. The project file is a Code::Blocks one.

I want to learn how to effectively split a program up into multiple files. I've googled a bit, but couldn't find a tutorial that I understood completely.

Thanks in advance,

Clockowl 56 Posting Whiz

Just open it for reading and writing. Write to it, read from it.

What's the problem? You can find how fopen works for reading and writing on numerous C reference sites.

Clockowl 56 Posting Whiz

It's correct, the pseudocode. It's very VBish, but it's correct.

Clockowl 56 Posting Whiz

Open it for reading and writing?

Clockowl 56 Posting Whiz

C does all your pointermath for you. If you create a pointer to a structure and then increment it, it adds sizeof(structure) bytes to the pointer.

For example:
When an array is declared, the name of the array without element brackets is a pointer to the first element.

struct productos *productPointer = p;

after this, p[1] is the same element as *(productPointer + 1), p[2] == *(productPointer + 2), etc.
However, it's often easier not to dereference the pointer like that, instead use the arrow operator. This operator sees the previous variable as a pointer to a structure.
p[1].precio is the same as (productPointer + 1)->precio.

you can also change productPointer itself with increments and decrements. Just rememeber that you are not incrementing in bytes, but in sizes of the type you're pointing to. In this case, that's sizeof(struct productos).

fread doesn't need "b" to my experience, nor does it say so on all of the reference sites I consult. Don't worry about it.

Clockowl 56 Posting Whiz

If you need to reopen it for writing, why don't you open it for reading and writing "rw" or reopen it with freopen()?

feof() returns a nonzero value when the FILE* has reached the EOF. Your while-loop checks if it doesn't equal one, but it should check if it equals 0: it may not return 1, it may return any nonzero number. This doesn't have to be a problem, but it's more secure.

If you haven't read anything about memset, you don't know how to initialize arrays. Which is kinda bad. You can set the whole c[], p[] and other arrays to 0 or any value you prefer with memset. Read up on it. It'd make the program more readable and faster.

By the way, here's a nice reference:
c(pp)reference

Clockowl 56 Posting Whiz

in inicializar():
I wouldn't initialize strings like that. Better to use memset to clear them, or only set the first byte to the NULL byte. Doesn't really matter, but it makes the code a bit more readable. I think.

in inicializarporarchivo:
char tid[1];
char ttelefono[9];
char tcantcompras[1];
char tnumportal[2];
char tpiso[1];

tid, tcantcompras, tpiso: char arrays of a single char?

How do your files look? Your only using fgets, which reads in a file until it encounters a '\n'. Is that correct?

You're trying to use putc on a file which you opened as "r"eadonly. That' can't be correct.

You're testing "feof()" with "!= 1". Better would it be to continue when "feof == 0", since TRUE isn't defined as 1 but as any non-zero number.

Clockowl 56 Posting Whiz

You really oughta post some of the code here. I'm sure most people here have dealt with oddly name variables.

Clockowl 56 Posting Whiz

A bit more details would be nice. What integrity check?

Clockowl 56 Posting Whiz

fopen creates a FILE* to the given directory. It doesn't support spaces (at least, not on Windows). It returns NULL when a file doesn't exist.

fread reads in a FILE*, parsing can be done with sscanf and your own mix of code.

Going to the last line can't be done like that. You must search for the end, then search for a newline character perhaps or something that defines a line.

fclose closes the given FILE*.

Hope this helps a bit. Finding a C reference site will help explaining the functions in more detail.

Clockowl 56 Posting Whiz

So first you open it, then you read it in, parse the file in your structure, and move on to the next.

Reading in a file: fread()
Parsing: Your own mix and a bit of sscanf();

writing would be exactly the opposite: sprintf() and fwrite().

sprintf and sscanf work like printf and scanf, except the first argument is a pointer to a character array which is read from/printed to.

Clockowl 56 Posting Whiz

Solved:

glDrawElements(GL_QUADS, quadcount, GL_UNSIGNED_INT, quadindex);
  glDrawElements(GL_TRIANGLES, trianglecount, GL_UNSIGNED_INT, triangleindex);

-->

glDrawElements(GL_QUADS, quadcount*4, GL_UNSIGNED_INT, quadindex);
  glDrawElements(GL_TRIANGLES, trianglecount*3, GL_UNSIGNED_INT, triangleindex);

I'll read function descriptions more carefully next time, fo'sho. :D

Clockowl 56 Posting Whiz

Hai guys,

I have quite a large program here that I don't seem to understand. The program, Bow Vice Jet (an anagram for object view) is meant to display WaveFront .obj files. However, it quits when it loads a specific file while on my computer, while on another it does not. I can't find the bug, and it's very annoying. Maybe it has got something to do with memory allocation, maybe something else. If anyone sees any mistake in the code, please point it out and I will correct it. I know it's a vague question, "Find the bug", but I've been searching for quite some time now and can't figure it out.

The code is attached, together with the "quiting" file and a file that almost works. To see how these files should look in 3D space, download GLC Player or use your favorite 3D mesh viewing program.

Thanks in advance,
Nick

Clockowl 56 Posting Whiz

Ooooooooooh, really newb mistake right there, haha. Thanks for *pointing* it out. No pun intended ofc. ;)

Clockowl 56 Posting Whiz

Hai all,

I ran into a strange issue I never experienced before. Pointers are passed to function, the function allocates memory to them with calloc, and right before the return sanity checks if the pointers still != NULL. That check succeeds, but upon returning and sanity checking in the calling function, all pointers == NULL.

The code is pretty big, so I won't copypaste it all in here but instead i've attached them.

For those unfamiliar with OpenGL: The compiler line for this is:
gcc.exe BowViceJet.c -o BowViceJet.exe -lopengl32 -lglu32 -lgdi32 -Wall

The function I'm talking about is loadObj. As you can see, right before returning it checks:

if(vertices == NULL || normals == NULL || verttext == NULL || triangleindex == NULL || trianglenormals == NULL || quadindex == NULL || quadnormals == NULL){
    printf("Failed to allocate enough memory, exiting...\n");
    exit(1);
  }
  return 0;
}

and in the calling function it checks again:

loadObj("C:\\ObjTest.obj", vertices, normals, triangleindex, quadindex, &trianglecount, &quadcount);
  if(vertices == NULL || normals == NULL || triangleindex == NULL || quadindex == NULL){
    printf("Error! One of the essential pointers == NULL!");
    exit(1);
  }

and there it happily prints the message and exits.

How come? :(

Thanks in advance,
Nick

PS: Also included "ObjTest.txt" so you can experience it in it's full glory. Heh. ;) Don't forget to either adjust source code to load "ObjTest.txt" or change extension to ".obj" so that it reads "ObjTest.obj".