Name der Datei mit DoFileOpen, anschließendes Öffnen funktioniert nicht
-
Ersteinmal fröhliche Weihnachten an alle.
Hier ist mein Problem:
ich habe mit DoFileOpen(HWND hwnd) ein Dialogfenster erstellt, dass eine Datei abfragen soll. Der string szFile beinhaltet die vollständige Adresse des Files. Wenn ich allerdings versuche, mit fopen (szFile, "r") die Datei zu öffnen, bekomme ich eine Fehlermeldung. Allerdings zeigt die Messagebox den vollständigenPfad an, also ist der Inhalt vom szFile richtig? Bin im Moment etwas ratlos.
/ OPEN FILE START #include <windows.h> #include <stdio.h> FILE *datei; double pointsx[]; double pointsy[]; int i; int DoFileOpen(HWND hwnd) { OPENFILENAME ofn; // common dialog box structure char szFile[260]; // buffer for file name //HWND hwnd; // owner window HANDLE hf; // file handle // Initialize OPENFILENAME ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFile = szFile; // Set lpstrFile[0] to '\0' so that GetOpenFileName does not // use the contents of szFile to initialize itself. ofn.lpstrFile[0] = '\0'; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0"; ofn.nFilterIndex = 1; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = 0; ofn.lpstrInitialDir = NULL; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; // Display the Open dialog box. if (GetOpenFileName(&ofn)==TRUE) hf = CreateFile(ofn.lpstrFile, GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL); //datei = fopen(szFile, "r"); if((datei = fopen(szFile,"r")) == NULL) printf("Konnte Datei nicht oeffnen.\n"); //else for(i=0;i<=5;i++) // { fscanf(datei,"%lf %lf",&pointsx[i], &pointsy[i]); // printf("%lf , %lf" pointsx[i], pointsy[i]);} // Now simpley display the file name MessageBox ( NULL , ofn.lpstrFile , "File Name" , MB_OK); return 0; }
-
Dieser Thread wurde von Moderator/in SeppJ aus dem Forum C (C89, C99 und C11) in das Forum WinAPI verschoben.
Im Zweifelsfall bitte auch folgende Hinweise beachten:
C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?Dieses Posting wurde automatisch erzeugt.
-
Du öffnest die Datei mit CreateFile ohne sie zu schließen.
fopen kann die Datei nicht erneut öffnen weil diese gesperrt ist.Fehlernummer der CRT Funktion kontrollieren.