Hi guys o/ !

I'm trying to make a program that write in text file some datas.

Then, i wrote a header that have all functions I need to write in this file.

thas is my header:

#ifndef _SNAPSHOT_H
#define	_SNAPSHOT_H

#define LAST_PRICE 0;
#define BEST_ASK 1;
#define BEST_BID 2;
#define HIGH 3;
#define LOW 4;
#define OPEN 5;
#define CLOSE 6;
#define TRADES 7;

int snapwrite(char *_symbol, int _typeval, char *_val) {

    int sw;

    //Descritor do arquivo
    FILE *sfl;

    char *sflparam;

    sflparam = malloc(strlen(_symbol)+5);

    switch (_typeval) {

        case 0:
            //Escreve
            strcpy(sflparam,_symbol);
            strcat(sflparam,".lst");
            sfl= fopen(sflparam,"w");
            sw = fprintf(sfl,"LAST     \t : %s\n", _val);
            break;
        case 1:
            strcpy(sflparam,_symbol);
            strcat(sflparam,".bsk");
            sfl= fopen(sflparam,"w");
            sw = fprintf(sfl,"BEST_ASK \t : %s\n", _val);
            break;
        case 2:
            strcpy(sflparam,_symbol);
            strcat(sflparam,".bsd");
            sfl= fopen(sflparam,"w");
            sw = fprintf(sfl,"BEST_BID \t : %s\n", _val);
            break;        
        default:
            sw = -1;
            break;
    }

    //Fecha descritor salvando alterações
    fclose(sfl);

    return sw;

}

#endif	/* _SNAPSHOT_H */

And then, I use in my source file :

snapwrite(file, LAST_PRICE, "text");

But, when I put LAST_PRICE, does not works, but if I put 0, does work very well.

I wanna to use the define tags, because this header will use in another programs.

What i'm doing wrong?

Thanks for any help;

All your #defines have a ; at the end of them which will be prducing invalid syntax, remember a #define is a text substitution.

Also you should note define functions, like snapwrite, in a header. You should declare the function in the header

int snapwrite(char *_symbol, int _typeval, char *_val);

and define the function in a C file somewhere.

right, i remove the ;

can u give me a example how declare in my header the function and how write the code in other file?

plz!

I modify my header to this, see if is right now:

#ifndef _SNAPSHOT_H
#define	_SNAPSHOT_H

#define LAST_PRICE 0
#define BEST_ASK 1
#define BEST_BID 2
#define HIGH 3
#define LOW 4
#define OPEN 5
#define CLOSE 6
#define TRADES 7


int snapwrite(char *_symbol, int _typeval, char *_val);


int snapwrite(char *_symbol, int _typeval, char *_val) {

    int sw;

    //Descritor do arquivo
    FILE *sfl;

    char *sflparam;

    sflparam = malloc(strlen(_symbol)+5);

    switch (_typeval) {

        case 0:
            //Escreve
            strcpy(sflparam,_symbol);
            strcat(sflparam,".lst");
            sfl= fopen(sflparam,"w");
            sw = fprintf(sfl,"LAST     \t : %s\n", _val);
            break;
        case 1:
            strcpy(sflparam,_symbol);
            strcat(sflparam,".bsk");
            sfl= fopen(sflparam,"w");
            sw = fprintf(sfl,"BEST_ASK \t : %s\n", _val);
            break;
        
        default:
            sw = -1;
            break;
    }

    //Fecha descritor salvando alterações
    fclose(sfl);

    return sw;

}

#endif	/* _SNAPSHOT_H */

Make a header file.... Say file1.h. Include all the header file and function declarations is this file. For ex

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

void display(int,int);

Then make a .c file. Put all the function declarations in this file. Include the header file, file1.h in the source code file

#include "file1.h"

int display(int a,int b)
{
   // Your code here
}

int main()
{
   display(1,2);
}

Make a header file.... Say file1.h. Include all the header file and function declarations is this file. For ex

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

void display(int,int);

Then make a .c file. Put all the function declarations in this file. Include the header file, file1.h in the source code file

#include "file1.h"

int display(int a,int b)
{
   // Your code here
}

int main()
{
   display(1,2);
}

but this way i'll need to write my code in c file, i want make a header file to use in others programs to, like <stdio.h> has the basic functions we need, my header will have all functions that i need on my work.

that way i type above, with the declaration and after the code, works very well and i saw that on tutorial on web.

That does not right?

right, i remove the ;

can u give me a example how declare in my header the function and how write the code in other file?

plz!

My previous post answered this question. But I guess for your application, you can use your method

header files are good programming practice especially for large projects with mulitple files.

but, no, you can't just plop a header file into some other program and have it work without code available. you do need to have the source code -- in one form or another -- included as well as the header.

in many cases, such as standard libraries like <stdio.h>, the source code is part of a library that is included in the build when the header file is included. these libraries are the source code, and are already compiled for your specific operating system, and are typically provided as .lib or .dll files

so for your code to be portable, you would have to distribute the source code, either as the .c source file or in an external library, if you want to allow that functionality to be ported to some other program. merely including the header prototypes will not do it.

but it is good practice to put all the prototypes and defines and global declarations and typedefs and etcetera in their own header files. as your projects get bigger, these header files can and will become fairly substantial, and it becomes necessary to maintain programmer sanity to keep everything organized.

also it's important to not redefine elements, so the header files are framed with their own define to keep them from being called more than once.

/**********************************
*  myproject.h
*
*  contains definitions and prototypes
*  for myproject.c
*
**********************************/

#ifndef __MYPROJECT_H__
#define __MYPROJECT_H__

#define ONE      1
#define TWO      2
#define TEN      10

typedef struct {
     int  number;
     char string1[ONE];
     char string2[TWO];
     char string3[TEN];
} MyStruct_t;

int prototype1(int,int,int);
int prototype2(int,int,int);

#endif  // __MYPROJECT_H__

.

So... i'll will need to write a lib first. Because, all this functions will be used on anothers projects, for me or for othres on the crew.

And if i understood what you explained, i write this lib, compile for my o.s ( by the way is linux -- debian ) then write the header with all define and declarations, and finally distribute this lib and the headers for the others.

That's it, right?

yeah, pretty much ... the rest is just details :P

just to be clear, you write the code and the header file, then you compile and build it as a library file, rather than a binary executable. the resulting library file(s) along with the .h file are distributed.

Note for MS Windows .DLLs, you have to specify in the code which functions will be externally visible. And Linux has completely different methods to create and include shared libraries. See the link above.

it's some extra work, but the benefit of doing it this way, rather than just handing them the .C source code, is that libraries can be maintained/revised without affecting the programs that use them (unless the prototypes are changed) also the library build is controlled and can't be modified in a way not intended by the author, other than allowing them to tweak #define values in the header


.

What hasn't been mentioned yet is if you left the function defined in the header file and then included that header into 2 C files in a project and tried to compile and link those 2 files you would get a "symbol defined multiple times" error from the linker because the function that is in the header would be present in both object files when you try to link them and the linker is unable to determine which one of the 2 functions to use.

Every function must appear in a program precisely 1 time. For that reason you must avoid putting functions in headers and for the same reason you must avoid defining variables in headers.

What hasn't been mentioned yet is if you left the function defined in the header file and then included that header into 2 C files in a project and tried to compile and link those 2 files you would get a "symbol defined multiple times" error from the linker because the function that is in the header would be present in both object files when you try to link them and the linker is unable to determine which one of the 2 functions to use.

Every function must appear in a program precisely 1 time. For that reason you must avoid putting functions in headers and for the same reason you must avoid defining variables in headers.

What you are saying is if i make my lib and then create my header file, and when i'll use in my project, for example, have main.c and other.c, and include the header in both files, the linker will thrown an error?

If yes, why the linker does not thrown an error with the stdio.h and others headers basics?

No if you take your lib you will be fine.

If you took your header file in its original form with no C file and no lib then you would get the error I described.

Understood.

But as i was talking with jephthah, i'll need make a lib, because this
functions will use in more 3 projects, and if i need to write each function
in each project, and then if someone change a little thing in the
function, we will have a lot of problems.

And then, if a make a lib, and distribute it, the people can't change
any thing on the function and if have an update of the lib, just need
each one update your lib files.

We have all these projects already made with java, but we want to update
because the systems are very low.

Thanks for all that helped me.

If i have any question about make a lib, i'll ask here.

i may be (re)stating the obvious, but just for clarity's sake:

what Banfa meant, i think, is that you will need to take the references to the prototypes out of the C file and leave them only in the header file. in the C file(s), you will have the single line #include "myheaderfile.h" or whatever you call it.

the other programs will link the library when they are built. Depending on how the library is built, those programs' C files that use the library's external functions will probably need to have that #include "myheaderfile.h" line, as well.

and you may already know all this.

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.