Hey, im trying to open my file but it dosent work, what is wrong?
Ive created a file called "tallfil.dat" and it contains some numbers. It lies under the same path as the c++ file.
Code:
#include <fstream> //Må inkluderes for å jobbe med datafiler
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
const string filnavn = "tallfil.dat";
int main()
{
ifstream innfil;
innfil.open(filnavn.c_str());
if(!innfil)
{
cout << "Feil ved aapning av innfil" << endl;
exit(EXIT_FAILURE);
}
int tall;
int sum = 0;
innfil >> tall;
while(tall > 0)
{
sum += tall;
innfil >> tall;
}
cout << "Summen er " << sum << endl;
innfil.close();
return 0;
}