I am trying to get data to an array from a text file. But the data is in scientific notation. So my program reads wrong!
So can anybody please show me the way....
my coding is:
int main()
{
double b[19][6];
ifstream myfile1("data_store.txt");
if(myfile1.is_open())
{
while(!myfile1.eof())
{
for(int i=0;i<19;i++)
{
for(int j=0;j<6;j++)
{
myfile1>>b[i][j];
}
}
}
myfile1.close();
}
From what you've said the error is because the file is not open.
What kind of compiler/ide are you using? Is it an IDE or a command-line compiler?
I'm guessing you are using an IDE.
If so, go to your bin/debug or bin/release folder (whichever you are building.. debug or release) and put your text file in there. Then run the program.
The only other problem I could think of is that you may not be running the program with the correct permissions. Make sure you have full permissions on your operating system if this fails.
Line 9 shouldn't even run because you aren't opening a file or anything. You say it works, but that's probably because of Visual Studio's ability to fix some syntax errors on its own. It should have given you a warning.
Actually, you may not be aware of this, but ifstream allows the programmer to pass in the name of the file to be opened in the constructor. ifstream will automatically attempt to open the file passed.
At risk of beating it to death, here are the constructors for an ifstream; as can be seen, it happily accepts a filename to open (and since C++11, will accept it as a proper C++ string).