# c++ extract single character value from text file into an array



## n00bsmith (Sep 10, 2009)

There are 12 char values in a text file all separated by a space on the first line. I want to pull them from the text file and assign them two an array.
I have found tutorials online, either I cant yet understand them or they are
not pinpointing my specific problem.

( with the code I have now I keep getting my "Unable to open file" error )
ex:

tester.txt
b A b B C b D b E F b G

code-i-have-so-far.cpp

#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;

int main()
{
SetConsoleTitle ("Guitar Scales");
char notout;
//int scale = 0;
//char key = 0;
char x[12];
ifstream inFile;

inFile.open("C:\Users\user\Desktop\tester.txt");

if (!inFile) {
cout << "Unable to open file";
cout << "\tQ to exit: ";
cin >> notout;
exit(1); // terminate with error
}
else {
inFile >> x[0] 
>> x[1]
>> x[2]
>> x[3]
>> x[4]
>> x[5]
>> x[6]
>> x[7]
>> x[8]
>> x[9]
>> x[10]
>> x[11]
}

cout 
<< endl << endl
<< "E |--" << x[9] << "--|--" << x[10] << "--|--" << x[11] << "--|--" << x[0] << "--|--" << x[1] << "--|--" << x[2] << "--|--"
<< x[3] << "--|--" << x[4] << "--|--" << x[5] << "--|--" << x[6] << "--|--" << x[7] <<"--|--" << x[8] << "--|\n"
<< "B |--" << x[4] << "--|--" << x[5] << "--|--" << x[6] << "--|--" << x[7] << "--|--" << x[8] << "--|--" << x[9] << "--|--"
<< x[10] << "--|--" << x[11] << "--|--" << x[0] << "--|--" << x[1] << "--|--" << x[2] <<"--|--" << x[3] << "--|\n"
<< "G |--" << x[0] << "--|--" << x[1] << "--|--" << x[2] << "--|--" << x[3] << "--|--" << x[4] << "--|--" << x[5] << "--|--"
<< x[6] << "--|--" << x[7] << "--|--" << x[8] << "--|--" << x[9] << "--|--" << x[10] <<"--|--" << x[11] << "--|\n"
<< "D |--" << x[7] << "--|--" << x[8] << "--|--" << x[9] << "--|--" << x[10] << "--|--" << x[11] << "--|--" << x[0] << "--|--"
<< x[1] << "--|--" << x[2] << "--|--" << x[3] << "--|--" << x[4] << "--|--" << x[5] <<"--|--" << x[6] << "--|\n"
<< "A |--" << x[2] << "--|--" << x[3] << "--|--" << x[4] << "--|--" << x[5] << "--|--" << x[6] << "--|--" << x[7] << "--|--"
<< x[8] << "--|--" << x[9] << "--|--" << x[10] << "--|--" << x[11] << "--|--" << x[0] <<"--|--" << x[1] << "--|\n"
<< "E |--" << x[9] << "--|--" << x[10] << "--|--" << x[11] << "--|--" << x[0] << "--|--" << x[1] << "--|--" << x[2] << "--|--"
<< x[3] << "--|--" << x[4] << "--|--" << x[5] << "--|--" << x[6] << "--|--" << x[7] <<"--|--" << x[8] << "--|\n"
<< endl << endl;

inFile.close();

cout << "\tQ to exit: ";
cin >> notout;

return 0;

}A. I know there must be a problem with how I am opening the text file but to the best of my knowledge idk why.
B. I am assuming there is something I am missing when it comes to implementing the txt file values into the array[12]

I know the code is flawed this is obvious I just don't know where to start on fixing it I have tried many different combinations from what I have learned any help would be much appreciated.


----------



## IMM (Feb 1, 2002)

In a string - a single backslash functions as an escape

"C:\Users\user\Desktop\tester.txt"
should be written as
"C:\\Users\\user\\Desktop\\tester.txt"


----------



## n00bsmith (Sep 10, 2009)

Thank you! I completely looked over that. It is funny how the littlest things can ruin a whole block of code.


----------

