Problems reading from file.

I have problems reading from the file. In the file there are integers and i have to average them and sum them all up.

Basically i have 2 questions:

1: Why is it giving me errors when im trying to read the file?

2: How do i read each individual integer and average them and then sum them up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
#include <fstream>
#include <string>
#include <sstream>


using namespace std;
int main ()
{
    ifstream file;
     
    file.open = ("/Users/laytoncozad/Desktop/DataFile.txt");

    string fileinput = file.getline;
    
    
    cout<<fileinput; //testing file
    
    return (0);
    
}


The error its giving me is this: Cannot run program "/private/var/folders/cg/42_27_bx64xbqglhxg88n1mr0000gn/T/dlight_laytoncozad/e0aec853/480717646/pty": error=2, No such file or directory
BUILD FAILED (exit value -2, total time: 61ms)

I have also tried leaving it just as "DataFile.txt"


Thanks in advance!
Why are you trying to use getline if you want to read integers? Use the formatted extraction operator >> just like with std::cin.
Would you mind giving me an example? I need a visual example before i will fully understand.
1
2
3
//to read one number:
int number;
file >> number;
makes sense, to read the next number you would just do the same thing? or would you have to put it in a loop?
use a while loop since you don't know how many int's there are in the file


error=2, No such file or directory:

have you tried:
file.open = ("C:\\Users\\laytoncozad\\Desktop\\DataFile.txt");
or
"C:\Users\laytoncozad\Desktop\DataFile.txt"
or
put it in the exe or project directory and just put "DataFile.txt"
Last edited on
Topic archived. No new replies allowed.