#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
FILE *fp = NULL;
fp = fopen("test.txt", "r");
if (!fp) {
puts("Fehler beim Öffnen der Datei");
return 1;
}
char date[11] = { 0 };
char time[9] = { 0 };
double temps[3];
char buf[64];
fgets(buf, 64, fp);
char *token = NULL;
int index = 0;
token = strtok(buf, ";");
do {
switch (index) {
case 0:
strcpy(date, token);
break;
case 1:
strcpy(time, token);
break;
case 2:
break;
case 3:
case 4:
case 5:
*(strchr(token, ',')) = '.';
temps[index-3] = atof(token);
break;
}
index++;
token = strtok(NULL, ";");
} while (token != NULL);
printf("Datum: %s, Uhrzeit: %s\n", date, time);
printf("Temp1: %e, Temp2: %e, Temp3: %e\n", temps[0], temps[1], temps[2]);
fclose(fp);
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
FILE *fp = NULL;
fp = fopen("test.txt", "r");
if (!fp) {
puts("Fehler beim Öffnen der Datei");
return 1;
}
char date[11] = { 0 };
char time[9] = { 0 };
double temps[3];
double internal;
while (!feof(fp)) {
char buf[64];
fgets(buf, 64, fp);
char *token = NULL;
int index = 0;
token = strtok(buf, ";");
do {
switch (index) {
case 0:
strcpy(date, token);
break;
case 1:
strcpy(time, token);
break;
case 2:
*(strchr(token, ',')) = '.';
internal = atof(token);
break;
case 3:
case 4:
case 5:
*(strchr(token, ',')) = '.';
temps[index-3] = atof(token);
break;
}
index++;
token = strtok(NULL, ";");
} while (token != NULL);
}
printf("Datum: %s, Uhrzeit: %s\n", date, time);
printf("Internal: %f, Temp1: %f, Temp2: %f, Temp3: %f\n", internal, temps[0], temps[1], temps[2]);
fclose(fp);
}
pyro@exuma:~/Coding/C $ gcc -o test test.c
pyro@exuma:~/Coding/C $ cat test.txt
2010-12-02;18:34:01;34,4;21,3;21,3;20,0;-3276,8;-3276,8;
2010-12-02;18:34:01;34,5;21,3;21,3;20,0;-3276,8;-3276,8;
2010-12-02;18:34:01;34,6;21,3;21,3;20,0;-3276,8;-3276,8;
2010-12-02;18:34:01;34,7;21,3;21,3;20,0;-3276,8;-3276,8;
2010-12-02;18:34:01;34,8;21,3;21,3;20,0;-3276,8;-3276,8;
pyro@exuma:~/Coding/C $ ./test
Datum: 2010-12-02, Uhrzeit: 18:34:01
Internal: 34.800000, Temp1: 21.300000, Temp2: 21.300000, Temp3: 20.000000
pyro@exuma:~/Coding/C $
Ich habe das jetzt mal in das Program eingefügt und ausprobiert, allerdings stürtzt das Programm bei jedem Start ab.
Ich würde jetzt eher vermuten, dass ein Zugriff auf Speicher erfolgt, der einem net gehört...Klingt mir fast so als würde es den Ram vollschreiben. Was wiederum bedeuten würde das eine Schleife Falsch wäre.
[COLOR=Red]while (!feof(fp)) { char buf[64];
fgets(buf, 64, fp);
char *token = NULL;
int index = 0;
token = strtok(buf, ";");
[COLOR=Red]do { switch (index) {
case 0:
strcpy(date, token);
break;
case 1:
strcpy(time, token);
break;
case 2:
*(strchr(token, ',')) = '.';
internal = atof(token);
break;
case 3:
case 4:
case 5:
*(strchr(token, ',')) = '.';
temps[index-3] = atof(token);
break;
[COLOR=Red] [COLOR=Black]}
index++;
token = strtok(NULL, ";");
[COLOR=Red] } while (token != NULL);[COLOR=Red]}
while (!feof(fp))
{
char buf[64];
char *token = NULL;
if(fgets(buf, 64, fp))
{
for(int index = 0;
index <= 5 && (token = strtok(buf, ";")) != NULL;
index++)
{
switch (index)
{
case 0:
strcpy(date, token);
break;
case 1:
strcpy(time, token);
break;
case 2:
*(strchr(token, ',')) = '.';
internal = atof(token);
break;
case 3:
case 4:
case 5:
*(strchr(token, ',')) = '.';
temps[index-3] = atof(token);
break;
}
}
}
}