View Full Version : snprintf
#include <windows.h>
#include <stdio.h>
int main(void){
int *p;
char *path, *ic, *rfile;
char buffer[255+1];
// qui non ho postato un pezzo di codice
snprintf(rfile, sizeof(rfile), "n%d", p);
if(GetPrivateProfileString("rfiles", "n0", NULL, buffer, 255, path) != 0){
fprintf(stdout, "%s", buffer);
} else {
fprintf(stderr, "Errore durante l'apertura del file: %s", path);
}
p++;
}
Durante la compilazione mi da questo errore:
undefined reference to `snprintf'
da cosa puņ dipendere?
ciau
<font size=-1>[ Questo messaggio è stato modificato da: Progeny il 2002-02-11 19:04 ]</font>
silentman
11-02-2002, 19:58
Durante la compilazione mi da questo errore:
undefined reference to `snprintf'
da cosa puņ dipendere?
la snprintf non č una funzione di libreria. La trovi *anche* su: http://www.ijs.si/software/snprintf/
il gcc su linux me la dava per buona :smile:
grazie 1000 Silent
anzi ora che ci penso, esiste qualcosa che possa sostituire la snprintf? giusto per non riempire il progetto di sta roba.
bye
<font size=-1>[ Questo messaggio è stato modificato da: Progeny il 2002-02-11 21:23 ]</font>
silentman
12-02-2002, 16:29
Non che io sappia, a meno di non scrivere una snprintf ex-novo. Posso dirti con un ragionevole margine di sicurezza che la snprintf che ti ho dato sia sufficientemente "light" da includerla in qualsiasi progetto tu voglia distribuire
Saluti
Se usi il VC++ puoi usare _snprintf :
int _snprintf( char *buffer, size_t count, const char *format [, argument] ... );
Ad eg:
#include <stdio.h>
int main(int argc, char* argv[]) {
char sz[5];
if (_snprintf(sz, sizeof(sz), "Hello World!") < 0)
sz[sizeof(sz) - 1] = 0;
printf(sz);
return 0;
}
Shaka
<font size=-1>[ Questo messaggio è stato modificato da: Shaka il 2002-02-19 10:48 ]</font>
Non ho vc++ installato, 400mb son troppi :smile:
Sulle MSDN ho trovato cmq qualcosa di interessante:
The sprintf function formats and stores a series of characters and values in buffer. Each argument (if any) is converted and output according to the corresponding format specification in format.
int sprintf( char *buffer, const char *format [, argument] ... );
Example
/* SPRINTF.C: This program uses sprintf to format various
* data and place them in the string named buffer.
*/
#include <stdio.h>
void main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;
/* Format and print various data: */
j = sprintf( buffer, "tString: %sn", s );
j += sprintf( buffer + j, "tCharacter: %cn", c );
j += sprintf( buffer + j, "tInteger: %dn", i );
j += sprintf( buffer + j, "tReal: %fn", fp );
printf( "Output:n%sncharacter count = %dn", buffer, j );
}
Output
Output:
String: computer
Character: l
Integer: 35
Real: 1.732053
character count = 71
Usando sprinft me lo compila bene, solo che mi va in crash l'eseguibile durante l'esecuzione.
#include <windows.h>
#include <stdio.h>
int main(void){
int p, sp;
char *path, *ic;
char buffer[255+1], rfile[255+1];
scanf("%s", path);
sp = sprintf(rfile, "n%d", p);
printf("%s", rfile);
if(GetPrivateProfileString("rfiles", rfile, NULL, buffer, 255, path) != 0){
fprintf(stdout, "%s", buffer);
} else {
fprintf(stderr, "Errore durante l'apertura del file: %s", path);
}
p++;
}
--------------------Configuration: xxxxxxx - Win32 Debug--------------------
Compiling...
xxxxxxx.cpp
D:Tempxxxxxxxxxxxxxx.cpp(24) : warning C4508: 'main' : function should return a value; 'void' return type assumed
D:Tempxxxxxxxxxxxxxx.cpp(12) : warning C4101: 'ic' : unreferenced local variable
D:Tempxxxxxxxxxxxxxx.cpp(15) : warning C4700: local variable 'path' used without having been initialized <-- direi che il problema sta qua :smile:
D:Tempxxxxxxxxxxxxxx.cpp(16) : warning C4700: local variable 'p' used without having been initialized
xxxxxxx.obj - 0 error(s), 4 warning(s)
vBulletin® v3.7.4, Copyright ©2000-2008, Jelsoft Enterprises Ltd.