Salut,

J'écris une fonction qui se charge de remplacer à la manière de printf les symboles d'une chaîne formatée afin d'écrire la nouvelle chaîne dans un fichier texte. L'écriture n'est pas un problème mais j'ai du mal à remplacer les symboles.

Voici ma fonction :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
int addLog(int logLevel, char *strLogFormat, ...)
{
	char buffer[BUFF_SIZE];
	char strLog[BUFF_SIZE];
	char *strLevel;
	int i = 0;
	int count = 0;
 
	va_list ap;
	va_start(ap, strLogFormat);
 
	if (!fd)
	{
		openLog();
	} else {
		switch (logLevel)
		{
			case LOGINFO:
				strLevel = "INFO";
				break;
			case LOGDEBUG:
				strLevel = "DEBUG";
				break;
			case LOGWARNING:
				strLevel = "WARNING";
				break;
			case LOGERROR:
				strLevel = "ERROR";
				break;
			case LOGFATAL:
				strLevel = "FATAL";
				break;
			case LOGNOTICE:
			default:
				strLevel = "NOTICE";
				break;
		}
 
		do
		{
			if (strLogFormat[i] == '%')
			{
				switch (strLogFormat[i+1])
				{
					case 's':
						sprintf(strLog, "%s%s", buffer, va_arg(ap, char *));
						break;
					case 'c':
						sprintf(strLog, "%s%c", buffer, (char) va_arg(ap, int));
						break;
					case 'd':
						sprintf(strLog, "%s%d", buffer, va_arg(ap, int));
						break;
					default:
						break;
				}
				count = 0;
				i++;
			} else {
				buffer[count] = strLogFormat[i];
				count++;
			}
			i++;
		} while (strLogFormat[i] != '\0');
		va_end(ap);
 
		fprintf(fd, "%s: %s\n", strLevel, strLog);
	}
	return 0;
}
Mais ça ne fonctionne pas très bien