|
From: Per W. <pw...@ia...> - 2008-03-30 07:15:08
|
No, I do not think you understand the other warnings.
You should not treat them as warnings, since in reality they are not
warnings but errors. You have completely misunderstood the strcat()
function.
It's prototype is:
char *strcat(char *dest, const char *src);
Notice that the second parameter has the 'const' keyword, but not the
first. That is absolutely vital! Why? Because the first parameter is a
target buffer that will be _modified_ by the call! The second parameter is
const because it will only be read.
Anything pointer or buffer you use as first parameter must have write
access and have enough room to hold the resulting string.
What do you think happens when you do:
err_msg = strcat ("Cannot open the configuration file: ",
strcat(in_conf_file_name, "\n"));
Hint:
Your file name in in_conf_file_name will be modified to also contain a
"\n".
But in_conf_file_name lies in memory you do not own, and hence are not
allowed to write to.
Also, the text string "Cannot open the configuration file: " will also be
used as a destination buffer, and will have the file name and a "\n"
appended to it. But where is the room for that extra text? And MinGW will
place the string literal in read-only memory, so your program will be
terminated!
strcat() is not a function that magically glues together two strings and
returns a pointer to some internal magic result.
It is a function that takes a first argument and extends it with a second
string. After that, it will return a pointer to this first (now already
modified) argument.
So, you can use strcat() as:
char buf[100];
char *res;
strcpy(buf,"Hello");
strcat(buf," World!");
printf("%s\n",buf);
or you can write:
strcpy(buf,"Hello");
printf("%s\n",strcat(buf," World!"));
or you can write:
printf("%s\n",strcat(strcpy(buf,"Hello")," World!"));
or you can write:
res = strcat(strcpy(buf,"Hello")," World!");
printf("%s\n",res);
but you can not write:
res = strcat("Hello"," World!");
But in the end, why not just write:
char error_buf[500];
snprintf(error_buf,sizeof(error_buf),"Can not open the configuration file
%s",conf_file_name);
strcat is used for glueing together strings.
s(n)printf is used for creating formatted output.
You are not interested in concatenating any strings...
/pwm
On Sun, 30 Mar 2008, Jan Mura wrote:
> Hello,
>
> maybe it is clear but I am a little bit confused.
>
> This is the source code:
>
> #include<stdio.h>
> #include<string.h>
>
> const char *module_name = "conf_proces.c";
>
> const char *conf_file_name = "..\\conf\\conf.xml";
>
> int conf_file_read(const char *in_conf_file_name)
> {
> const char *function_name = "conf_file_read";
> const char *err_msg;
> int i;
> FILE *p_fo;
>
> /*open and read the conf file*/
> if ((p_fo = fopen (conf_file_name, "r")) == NULL)
> {
> /* ERROR PROCESSING */
> err_msg = strcat ("Cannot open the configuration file: ", strcat
> (in_conf_file_name, "\n"));
> /* write_err (module_name, function_name, err_msg); */
> }
>
>
> if ( (i = (fclose (p_fo) != 0)))
> {
> /* ERROR PROCESSING */
> err_msg = strcat ("Cannot close the configuration file: ", strcat
> (in_conf_file_name, "\n"));
> /* write_err (module_name, function_name, err_msg); */
> }
> return 0;
> }
>
> And this is the gcc outpput:
> C:\Documents and Settings\Jan Mura\_myhome\dev\dialogues\src>gcc
> conf_process.c
> conf_process.c: In function `conf_file_read':
> conf_process.c:27: warning: passing arg 1 of `strcat' discards qualifiers
> from p
> ointer target type
> conf_process.c:36: warning: passing arg 1 of `strcat' discards qualifiers
> from p
> ointer target type
> C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../libmingw32.a(main.o)(.text+0x10
> 6):main.c: undefined reference to `WinMain@16'
> collect2: ld returned 1 exit status
>
> It is ok with these warnings, I understand it.
> But I don't understand what means the error message about the WinMain
> function. There is no main neither WinMain. I don't use windows.h, Win
> components or anything like that.
>
> Jan Mura
> jan...@vo...
|