Witam! Zastosowałem niżej podany kod do sprawdzanie czy istnieje plik txt (baza danych):
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int fileExists (const char* fileName)
{
struct stat buf;
/* Odczytywanie statystyk pliku */
if ( stat(fileName, &buf) == 0 )
return 1;
else
return 0;
}
int main (int argc, char* argv[])
{
if ( fileExists("dane.txt") )
printf("Plik istnieje!\n");
else
printf("Plik nie istnieje!\n");
return 0;
}
U mnie działa to bardzo dobrze (kompilator Dev c++) i gdy plik nie istnieje pokazuje stosowny komunikat. U kolegi zaś gdy plik nie istnieje program się zawiesza (kompilowane na Dev C++ i Borland C++). Czym może to być spowodowane?
@Szewy
W/w fragment kodu znalazłem na tym forum. Ogólnie to mam takie biblioteki dodane:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
a funkcja wygląda tak:
argv[1] = "test.txt";
int rozm = rozmiar(argv[1])-1;
if ( fileExists(argv[1]))
{
cout << "Plik istnieje! Milej zabawy !\n";
}
else
{
cout << "Plik nie istnieje! utworz nowy plik!\n";
int n;
do
{
cout << " Ilu uczniow chcesz wprowadzic do bazy? \n";
cin>>n;
}while(n <1);
uczen *s = new uczen [n];
dodaj_do_bazy(argv[1],s,n);
delete [] s;
}