PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Datei auslesn miit C



Enterprise
06-03-2004, 18:27
Hallo !!!

Ich möchte eine Textedatei die ab und zu erneuert wird auslesen mit C.
Es kann jeder Zeit etwas in die Datei rein geschrieben werden.
Das soll das C Programm mitbekommen , dass etwas rein geschrieben worden ist.
Soll kontrolieren ,ist das wichtig für mich oder nicht.
Sollte sich meken welche Zeile er zu letzt gesehn hat.
Forne ist das Datum und Uhrzeit da könnte man es ja fest machen.
Die Zeilen sehen so aus.

05/03/04 11:15:06 001001M|--/--/-/---|=0:2043 =Los of the 6 CRYSTAL
05/03/04 11:15:06 001001M|--/--/-/---|=0:2063 =IPC

Für mich wäre jetzt wichtig die Fehlernummer 2043 erscheint.
Es sind ca. fünf verschiedene Nummern wo das Programm verschieden reagieren soll.
Z.B:ein anderes Programm starten das den Befehl reset ausführt oder so..
Nun mein Problem ist ich bin nicht so fit in C.
Kann mir jemand helfen :confused: , wie könnte der Ansatz aussehen ?:confused:

mfg

Rainer

Thomas Engelke
10-03-2004, 14:05
Muß es unbedingt in C sein? Oder soll das nur einfach funktionieren und du greifst nach der Sprache, die du am Besten kannst? Es gibt Lösungen für solche Probleme, eine davon heißt "reguläre Ausdrücke".

Falls du dafür weitere Informationen benötigst, sag' Bescheid.

AD!

nobody0
11-03-2004, 10:02
Man kann dazu die Dateilänge bestimmen.
Aus den C-FAQs:



int
file_length (char *fname)
{
int length = -1L;
FILE *fptr;

fptr = fopen (fname, "rb");
if (fptr != NULL)
{
fseek (fptr, 0L, SEEK_END);
length = ftell (fptr);
fclose (fptr);
}
return length;
}

wraith
11-03-2004, 10:23
Original geschrieben von nobody0
Man kann dazu die Dateilänge bestimmen.
Aus den C-FAQs:

Was soll denn das für eine C-FAQ gewesen sein?
http://www.faqs.org/faqs/C-faq/faq/


19.12: How can I find out the size of a file, prior to reading it in?

A: If the "size of a file" is the number of characters you'll be
able to read from it in C, it is difficult or impossible to
determine this number exactly.

Under Unix, the stat() call will give you an exact answer.
Several other systems supply a Unix-like stat() which will give
an approximate answer. You can fseek() to the end and then use
ftell(), or maybe try fstat(), but these tend to have the same
sorts of problems: fstat() is not portable, and generally tells
you the same thing stat() tells you; ftell() is not guaranteed
to return a byte count except for binary files. Some systems
provide functions called filesize() or filelength(), but these
are obviously not portable, either.

Are you sure you have to determine the file's size in advance?
Since the most accurate way of determining the size of a file as
a C program will see it is to open the file and read it, perhaps
you can rearrange the code to learn the size as it reads.

Die einzige portable Methode ist es,das File Zeichen für Zeichen zu lesen.

nobody0
22-03-2004, 13:25
Im C-Standard (www.ansi.org), der nach den C-FAQs rausgekommen ist steht (7.19.9.4):

## The ftell function obtains the current value of the file position indicator
## for the stream pointed to by stream. For a binary stream, the value is the
## number of characters from the beginning of the file.

Also reicht es aus die Datei appending zu öffnen und ftell anzuwenden:



// file size
long int
file_length (const char * const fname)
{
long int length = -1L;
FILE *fptr;

fptr = fopen (fname, "ab");
if (fptr != NULL)
{
length = ftell (fptr);
fclose (fptr);
}
return length;
}


Unschön ist nur, dass die Datei mit Schreibberechtigung geöffnet werden muß; da habe ich noch keine 100 % ig portable Lösung mit Schreibschutz.

wraith
22-03-2004, 17:17
Jetzt brauchst du nur noch eine Methode, um die Dateigröße von Dateien zu bestimmen, die die Größe von long sprengen.
Tja, und damit wären wir wieder am Anfang "Die einzige portable Methode ist es, das File Zeichen für Zeichen zu lesen." (aber nicht mit long ;)).

nobody0
22-03-2004, 19:46
Unter __AMD64__ und anderen 64bitern sollte long int 64 Bit haben.
Das ist natürlich wenig portabel.