[C] Text Datei lesen und Schreiben

Hallo

wer kann mir sagen, weshalb folgendes kleines Programm immer den letzten Eintrag in einer Textdatei wiederholt?

Das Programm soll aus einer INI Datei eine bestimmte Zeile herauslöschen. Dazu lese ich die INI Datei Zeilenweise und lasse die Zeielen, die ich nicht löschen will in eine Temp Datei schreiben. Danach soll dann die alte INI durch die neue INI Datei ersetzt werden. Das einzige Problem ist, sobald in der INI Datei am Ende eine Leerzeiel ist, wird die davorige Zeile zweimal in die Temp Datei geschrieben. Wenn in der mitte der Datei eine Leerzeile ist, passiert dies nicht. Hat mir jemand einen Tipp was ich dagegen tun kann?

Vielen Dank
Christian Hund

#include
#include
#include

main()
{

FILE* fpdatei;
FILE* fpdateineu;
char suchen[500];
char gesucht[255];
char* gefunden;
char dateiname[255];
int i = 0;

if (fpdatei = fopen(dateiname, „r“) ==NULL)
{
exit 1;
}
fpdateineu = fopen(„edit2.txt“, „w“);
while (!feof(fpdatei))
{
fgets(suchen, 500, fpdatei);

gefunden = (char*)strstr(suchen, gesucht);
if (gefunden == NULL)
{
fputs(suchen, fpdateineu);
}
else
{
printf(„Gefunden\n“);
}
}
fclose(fpdatei);
fclose(fpdateineu);
return 0;
}

Der feof wird ausgelöst wenn Du versuchst über das Ende etwas auszulesen.

… Das ist die Lezte Zeile

[Bei dieser Antwort wurde das Vollzitat nachträglich automatisiert entfernt]

hehe eingerückt ist das bei mir schon :smile: nur hier sehen die Tabs ziemlich schlecht aus.

Aber vielen Dank für deinen Tipp. werde es mal probieren. Ja ist schon ein Windows INI File aber ich dachte das Probelm wird es auch unter Linux geben (wo ich auch da eine aoder andere progge) :smile: deshalb hab ich auch Textdatei geschrieben.

Wegen der Windows API:
Kannst du mir das etwas näher erläutern mit dem :WritePrivateProfileString(Section,Key,NULL,IniFileName);
weil Windows Programmierung hab ich bisher nix gemacht … nur Konsole.

Nochmals Danke
Gruß
Christian Hund

Der feof wird ausgelöst wenn Du versuchst
über das Ende etwas auszulesen.

… Das ist die Lezte Zeile

Och, wegen Ini Files und anderen System mach Dir mal kein Kopf. Werd noch ein wenig an meinem Object basteln und dann gibt es ein recht ausgereiftest Object mit dem man Ini Files sehr bequem handhaben kann. (Auch Konvertieren :wink:))))

Den Quelltext werd ich hier noch veröffentlichen. z.B. morgen :wink:

Gruß

  • Michael -

[Bei dieser Antwort wurde das Vollzitat nachträglich automatisiert entfernt]

Dann mal vielen Dank.

Bin mal gespannt darauf.

Viele Grüße
Christian Hund

Sehr Knapp gehalten (keine Fehlerkontrolle etc.)

Im Borland als Windows Konsolenanwendung Compiliert.

#include

int main()
{
char test[256];

WritePrivateProfileString(„Section“,„Key“,„Item“,„c:\Test.ini“);
GetPrivateProfileString(„Section“,„Key“," ",test,256,„c:\Test.ini“);
MessageBox(NULL,test,„Das ists hier:“,NULL);

}

Gruß

  • Michael -

[Bei dieser Antwort wurde das Vollzitat nachträglich automatisiert entfernt]

Sry, Syntax:

BOOL WritePrivateProfileString(

LPCTSTR lpszSection, // address of section name
LPCTSTR lpszKey, // address of key name
LPCTSTR lpszString, // address of string to add
LPCTSTR lpszFile // address of initialization filename
);
Parameters

lpszSection

Points to a null-terminated string containing the name of the section to which the string will be copied. If the section does not exist, it is created. The name of the section is case-independent; the string can be any combination of uppercase and lowercase letters.

lpszKey

Points to the null-terminated string containing the name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this parameter is NULL, the entire section, including all entries within the section, is deleted.

lpszString

Points to a null-terminated string to be written to the file. If this parameter is NULL, the key pointed to by the lpszKey parameter is deleted.
Windows 95: This platform does not support the use of the TAB (\t) character as part of this parameter.

lpszFile

Points to a null-terminated string that names the initialization file.

Return Value

If the function successfully copies the string to the initialization file, the return value is TRUE.
If the function fails, or if it flushes the cached version of the most recently accessed initialization file, the return value is FALSE. To get extended error information, call GetLastError.

DWORD GetPrivateProfileString(

LPCTSTR lpAppName, // points to section name
LPCTSTR lpKeyName, // points to key name
LPCTSTR lpDefault, // points to default string
LPTSTR lpReturnedString, // points to destination buffer
DWORD nSize, // size of destination buffer
LPCTSTR lpFileName // points to initialization filename
);
Parameters

lpAppName

Points to a null-terminated string that specifies the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer.

lpKeyName

Points to the null-terminated string containing the key name whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.

lpDefault

Points to a null-terminated string that specifies the default value for the given key if the key cannot be found in the initialization file. This parameter cannot be NULL.

lpReturnedString

Points to the buffer that receives the retrieved string.

nSize

Specifies the size, in characters, of the buffer pointed to by the lpReturnedString parameter.

lpFileName

Points to a null-terminated string that names the initialization file. If this parameter does not contain a full path to the file, Windows searches for the file in the Windows directory.

Return Value

If the function succeeds, the return value is the number of characters copied to the buffer, not including the terminating null character.
If neither lpAppName nor lpKeyName is NULL and the supplied destination buffer is too small to hold the requested string, the string is truncated and followed by a null character, and the return value is equal to nSize minus one.
If either lpAppName or lpKeyName is NULL and the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters. In this case, the return value is equal to nSize minus two.

Sehr Knapp gehalten (keine
Fehlerkontrolle etc.)

Im Borland als Windows Konsolenanwendung
Compiliert.

#include

int main()
{
char test[256];

WritePrivateProfileString(„Section“,„Key“,„Item“,„c:\Test.ini“);
GetPrivateProfileString(„Section“,„Key“,"
",test,256,„c:\Test.ini“);
MessageBox(NULL,test,„Das ists
hier:“,NULL);

}

Gruß

  • Michael -

Dann mal vielen Dank.

Bin mal gespannt darauf.

Viele Grüße
Christian Hund