[C++] Odczyt z pliku.txt

mój kod

#include 

#include 

#include 

#include 

#include 

#include 

using namespace std;


int main()

{


	double liczba;

	while(1)



		FILE *fp;

	fp=fopen("plik.txt", "rt");

	if(fp==NULL) {

		return 1;

	}


	string temp("");

	for(int i=0;i<4;i++)

	{

		temp=temp+fp[i];

		liczba = atoi(temp.c_str());

		if (liczba>10000) printf("\n hoho\n");



	}



	fclose( fp );

	system("del c:\\plik.txt");

}

Wyskakuje taki błąd.

c:\Users\Administrator.siec\Documents\Visual Studio Projects\zegarek\zegarek.cpp(33): error C3861: ‘fp’: identifier not found, even with argument-dependent lookup

c:\Users\Administrator.siec\Documents\Visual Studio Projects\zegarek\zegarek.cpp(17): error C2065: ‘fp’ : undeclared identifier

c:\Users\Administrator.siec\Documents\Visual Studio Projects\zegarek\zegarek.cpp(18): error C3861: ‘fp’: identifier not found, even with argument-dependent lookup

c:\Users\Administrator.siec\Documents\Visual Studio Projects\zegarek\zegarek.cpp(25): error C3861: ‘fp’: identifier not found, even with argument-dependent lookup

W pliku.txt znajduje się liczba, którą próbuje na razie w prosty sposób przetworzyć…

Pomocy

Kod jest w C. W temacie podajesz, ze jest to C++. Dostaniesz odpowiedz do C++.

#include 

#include 


using namespace std;


int main()

{

int liczba;

ifstream plik;

plik.open("Nazwa.txt");

plik>>liczba;

cout<
return 0;

}

Ponieważ kompilator odczytuje to jak:

   while(1)

Odczyt jest żle musisz użyć fscanf() doodczytu i fprintf() do zapisu

Gotowy kod:

// reading a text file

#include 

#include 

#include 

using namespace std;


int main () 

{

	string line;

	ifstream myfile ("example.dat");

	if (myfile.is_open())

	{

		while (! myfile.eof() )

		{

			getline (myfile,line);

			cout << line << endl;

		}

		myfile.close();

	}


	else cout << "Unable to open file";


	getchar();


	return 0;

}

Prosty do zrozumienia, działa - sprawdzałem…