Hello,
I am trying to get a file to open in my C++ code that I run on Codeblocks on my mac. I already have my code mostly set up (below),and I keep getting the error message saying that it can't be opened. Any advice on how to properly set up the .txt file to run in the program?
#include<iostream> //Required for cerr
#include<fstream> //Required for ifstream, ofstream
usingnamespace std;
int main()
{
ifstream fin("monaefile.txt");// ADD CREATIVE COMMENT
ofstream fout("checkedmonaefile.txt"); // ADD CREATIVE COMMENT
//Check for possible errors.
if(fin. fail())
{ cerr << "could not open input file monaefile.txt\n";
exit(1);
}
if(fout.fail())
{
cerr << "Could not open output file monaefile.txt\n";
}
//All files are open. DECLARATION SECTION
double x, F; //ADD ADDITIONAL VARIABLES AS NEEDED
int count(0);
fin >> x >> F; // ADD CREATIVE COMMENT
// CALCULATIONS OR LOGIC SECTION, INCLUDES OUTPUT
while(!fin.eof())
{ ++count;
if (x >= 0 && F>=0)
fout << x << " " << F << endl;// fill in processing data and sending all postitives to your output file and any value with
else
{
cerr << "Negative data encountered on line" << count << endl << x << " " << F << endl;// with a negative as error data to the screen
}
//SETUP POST PROCESSING TO GET AVERAGES OF THE VARIABLES ADD CREATIVE COMMENTS see instruction 4
}//end while
fin.close();
fout.close();
return 0;
}// END MAIN
specifically, try putting the full path to the file in the code.
if that works, its just a pathing problem. Some IDE run the program from another path than where the project/main code lives, visual studio does that. THen it can't find the file you put there. Running from the console instead of the IDE will either fix it or clarify the issue, as you either need a path to the executable file or a path to the file you want to open when the folders are different... if you are where the file is and try to run the program, and its not there, it will make sense...
If I wanted to know where the default directory is when running an app from the IDE I'd take the current project and substitute the following code for what is in the source file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <fstream>
int main()
{
// contains the default flags ios::out | ios::trunc
std::ofstream test("test_file.txt");
if (test.is_open())
{
test << "Some text, blah, blah, blah.....\n";
}
test.close();
}
Compile and run it from the IDE. Then look through the directories to find where that text file is located. That is the IDE's default directory for that project. Recompile your source code and make sure your input file is located in that dir.
Your source code is in some serious need of indentation formatting, it is kinda painful to read without it:
#include <iostream> //Required for cerr
#include <fstream> //Required for ifstream, ofstream
usingnamespace std;
int main()
{
ifstream fin("monaefile.txt");// ADD CREATIVE COMMENT
ofstream fout("checkedmonaefile.txt"); // ADD CREATIVE COMMENT
//Check for possible errors.
if (fin.fail())
{
cerr << "could not open input file monaefile.txt\n";
exit(1);
}
if (fout.fail())
{
cerr << "Could not open output file monaefile.txt\n";
}
//All files are open. DECLARATION SECTION
double x, F; //ADD ADDITIONAL VARIABLES AS NEEDED
int count(0);
fin >> x >> F; // ADD CREATIVE COMMENT
// CALCULATIONS OR LOGIC SECTION, INCLUDES OUTPUT
while (!fin.eof())
{
++count;
if (x >= 0 && F >= 0)
fout << x << " " << F << endl;// fill in processing data and sending all postitives to your output file and any value with
else
{
cerr << "Negative data encountered on line" << count << endl << x << " " << F << endl;// with a negative as error data to the screen
}
//SETUP POST PROCESSING TO GET AVERAGES OF THE VARIABLES ADD CREATIVE COMMENTS see instruction 4
}//end while
fin.close();
fout.close();
return 0;
}// END MAIN
The Windows version of Code::Blocks has a source formatter tool, AStyle, as part of the default install package, maybe the Mac version has it as well.
On my mac, when I create a new project, folder for that project is created on my desktop with "main.cpp" and "SearsG3_Lab13" (name of the project) along with the bin, obj, and debug files in it. And when I run the code it shows:
/Users/monaesears/Desktop/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/monaesears/Desktop/SearsG3_Lab13/bin/Debug/SearsG3_Lab13
My .txt file is also on my desktop but I am not sure where to put it in order to be read by my program.
@OP
Given it sounds like a path naming issue you can get the complete path name and paste it into your program fairly easily:
Use Finder and locate the file.
Click on the file to get the pull down menu.
Press the option key and select "Copy ... as pathname" (the full path is then displayed in the menu)
Paste the result into your program.
That overcomes the immediate problem and please yourself, but why not use Xcode? Xcode won't miraculously overcome path problems but CB is rubbish :)
while (!fin.eof())
{
++count;
if (x >= 0 && F >= 0)
fout << x << " " << F << endl;// fill in processing data and sending all postitives to your output file and any value with
else
{
cerr << "Negative data encountered on line" << count << endl << x << " " << F << endl;// with a negative as error data to the screen
}
//SETUP POST PROCESSING TO GET AVERAGES OF THE VARIABLES ADD CREATIVE COMMENTS see instruction 4
}//end while
Whatever that code is supposed to do, it doesn't. Either the loop body will not execute (fin is at eof) or it will loop indefinitely if fin is not eof as no data is read from fin during the loop!
Also, using .eof() in a loop is usually not right. The usual method would be something like:
1 2 3 4 5 6 7 8 9 10 11
while (fin >> x >> F)
{
++count;
if (x >= 0 && F >= 0)
fout << x << " " << F << endl;// fill in processing data and sending all postitives to your output file and any value with
else
{
cerr << "Negative data encountered on line" << count << endl << x << " " << F << endl;// with a negative as error data to the screen
}
//SETUP POST PROCESSING TO GET AVERAGES OF THE VARIABLES ADD CREATIVE COMMENTS see instruction 4
}//end while