29. november 2002 - 08:47Der er
10 kommentarer og 1 løsning
Læse og skrive til filer
Hejsa
Jeg har surfet noget rundt på ekserten, og har fundet et par spørgsmål hvor i har gemmengået det med at læse filer, men der er ingen af dem der har virket for mig i øjeblikket.
jeg ville mene at hvis jeg skulle åbne en fil skulle det se sådan du:
Men jeg får stadig fejl, hvis jeg prøver at compile den.
c:\cpp\filer.cpp 12:cannot convert 'FILE *' to 'char *' in function main() c:\cpp\filer.cpp 12: Type mismatch in parameter '__s' (wanted 'char*', got 'FILE *') in function main() c:\cpp\filer.cpp 12:Too few parameters in call to 'fgets(char *,int,FILE *)' in funktion main() c:\cpp\filer.cpp 12:Illegal structure operation in function main() c:\cpp\filer.cpp 17:Undefined symbol 'nyrow' in function main()
Du bruger igen fopen til at åbne/oprette en fil. I ovenstående bruger du "r", her kan du se hvilke muligheder der er... Fra MSDN:
"r"
Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"w"
Opens an empty file for writing. If the given file exists, its contents are destroyed.
"a"
Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.
"r+"
Opens for both reading and writing. (The file must exist.)
"w+"
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+"
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.
Du kan så bruge fputs (modsætningen til fgets), til at skrive til filen...
Ellers er der jo de udmærkede funktioner i Windows:
#include <string> #include <windows.h>
void main() {
string tekst = "Dette vil jeg skrive til filen"; HANDLE hFile; hFile = CreateFile("c:\\minfil.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Jeg skulle bare bruge en enkelt forstålig løsning, ellers tak
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.