-
Cant open file
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;
}
-
It must be located in the same directory as the executable.
And what "doesn't work"?
-
try putting it in your debug folder?
-
What does your tallfil.dat file look like? The first time I tested your code the program was stuck in an infinite loop because there was no 0 or negative value in the file to terminate that while loop.
Also, what specifically is the problem you're having? Please avoid being so general as "it doesn't work". What happens when you try it?