ifstream error!

I am learning and I don't get what happen here... saw on internet

name.get(letter);

but I don't wanna use char, my n in an int! is that the problem ?

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
 
  #include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
    int number;

    ifstream nFile("asdFile.txt");

    if(! nFile)
    {
        cout<<"Error!";
        return -1;
    }else
    {
        nFile.get(number);

        //for(int i=0;i<=n)
    }

    cout<<n;
    return 0;
}
You can read from ifstream the same way you read from cin.

 
nFile >> number;
Hello,

I'm just a beginner, so not sure if this is valid advice, but building on what Peter87 said, if you use

1
2
nFile >> number;
[code]
[/code]

You will begin to extract some characters from the text in that file. But I think if you put that in a for loop

1
2
3
4
5
6
7

string str

for (int i = 0; i < nfile.end(); i++)

nfile >> str;


I am not sure about the nfile.end(), I cannot recall what the condition or syntax is for the end character. For that you'll have to do some research.

Then you can store the extracted information as a string. Then you will need to use another for loop to extract the characters you want from the string, maybe with the use of if-else conditions.
Hello,

Also, from reading my old textbook, you could use the getline(fileObject, strObj, termChar) command to extract the chars into a string until the file being read reaches a terminating character.
Topic archived. No new replies allowed.