setherj
setherj

Reputation: 91

Unable to open file to array in C++

I am trying to open a file and read the contents into a couple arrays. I can't find what I am doing wrong. I am using the absolute path. The input file looks something like this:

Sam     100    23   210
Bob     1     2    12
Ted     300   300   100
Bill    223   300   221
James   234   123   212
Luke    123   222   111
Seth    1     2     3
Tim     222   300   180
Dan     222   111   211
Ben     100   100     2

Here is my code:

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
string name;
ifstream inFile;
string bowlerNames[10];
int bowlerScores[10][3] = {0};


inFile.open("C:\\Users\Seth\Documents\bowlingData.txt");

if (inFile.is_open()) //Checking if the file can be opened
{

for (int i = 0; i < 10; i++)
{
    inFile  >> bowlerNames[i] 
            >> bowlerScores[i][0]
            >> bowlerScores[i][1]
            >> bowlerScores[i][2];
}

for (int i = 0; i < 10; i++)
{
    cout    << bowlerNames[i] 
            << bowlerScores[i][0]
            << bowlerScores[i][1]
            << bowlerScores[i][2];
}
}
else cout << "Unable to open file..." <<endl; //Gives that sentence if the file can't be opened

inFile.close();

cout << endl; //spacing
system("Pause");
return 0;
}

Upvotes: 1

Views: 218

Answers (1)

Floris
Floris

Reputation: 46435

All the backslashes have to be double backslash!

inFile.open("C:\\Users\\Seth\\Documents\\bowlingData.txt");

Confirm this for yourself by doing something like this:

string fileName = "C:\\Users\\Seth\\Documents\\bowlingData.txt");
cout << fileName << endl;

Upvotes: 2

Related Questions