PDA

View Full Version : snprintf


Progeny
11-02-2002, 19:01
#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 &egrave; 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/

Progeny
11-02-2002, 21:14
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 &egrave; 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

Shaka
19-02-2002, 10:45
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 &egrave; stato modificato da: Shaka il 2002-02-19 10:48 ]</font>

Progeny
19-02-2002, 19:16
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 &lt;stdio.h&gt;

void main( void )
{
char buffer[200], s[] = &quot;computer&quot;, c = 'l';
int i = 35, j;
float fp = 1.7320534f;

/* Format and print various data: */
j = sprintf( buffer, &quot;tString: %sn&quot;, s );
j += sprintf( buffer + j, &quot;tCharacter: %cn&quot;, c );
j += sprintf( buffer + j, &quot;tInteger: %dn&quot;, i );
j += sprintf( buffer + j, &quot;tReal: %fn&quot;, fp );

printf( &quot;Output:n%sncharacter count = %dn&quot;, 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 &lt;windows.h&gt;
#include &lt;stdio.h&gt;

int main(void){

int p, sp;
char *path, *ic;
char buffer[255+1], rfile[255+1];

scanf(&quot;%s&quot;, path);
sp = sprintf(rfile, &quot;n%d&quot;, p);
printf(&quot;%s&quot;, rfile);
if(GetPrivateProfileString(&quot;rfiles&quot;, rfile, NULL, buffer, 255, path) != 0){
fprintf(stdout, &quot;%s&quot;, buffer);
} else {
fprintf(stderr, &quot;Errore durante l'apertura del file: %s&quot;, path);
}
p++;
}

Shaka
19-02-2002, 21:43
--------------------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)