fstream not working

Hi, my code:

#include<fstream>
#include<iostream>
using namespace std;
void main()
{
char flv[] = "fails.txt";
int i;
ifstream failas(flv);
//failas.open(flv, ios::in);
failas>>i;
cout<<i<<endl;
failas.close();
}

Why i don't get correct result? File "fails.txt" contains one symbol '1'. I've tried several variants to write that, but no success...
I've put that file both in .cpp and in debug directory.
Last edited on
What are you setting 'i' to?

And why is main() void? It should be int.
try this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<fstream>
#include<iostream>
using namespace std;

void pause();
void pause()
{
    cout << "hit enter to continue..." << endl;
    cin.get(); 
}

int main()
{
   
    int i;
    ifstream fin("fails.txt");
    
    if (!fin.is_open())
    {
       cout << "couldn't find input file...\nexiting now..." << endl;
       pause();
       return 0;
    }
    
    fin>>i;
    if (!fin)
    {
       cout << "error reading data...\nexiting now..."<< endl;
       pause();
       return 0;
    }
    
    fin.close();
    
    cout<<i<<endl;
    
    pause();
    return 0;
}


this way u ll know where the problem is
Last edited on
Topic archived. No new replies allowed.