Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

boyongo

macrumors newbie
Original poster
Nov 8, 2010
3
0
Hi everyone Im using xcode in c++ language and I am having trouble finding the place to save my txt file so the program would recognize it.

This is the code i have.


#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char * const argv[]) {
int x;
ifstream in;
in.open("Input.txt");
if(!in)
{
cout<<"\nProgram cannot find the input"<<endl;
return 1;
}
in>>x;
while (in) {
cout<<"Number"<<x<<endl;
in>>x;
}
in.close();
return 0;
}
The program displays the cout inside the if so its clearly not finding the txt file.
If anyone can help me please do.

Thanks :)
 
You should store the directory to the file in a string or char array and access it from there.

Code:
const char file[] = "/Users/Home/Desktop/file.txt";
ifstream in(file);
if (! in)
     exit(1);
else
     do stuff

Hope that helps.
 
File input/output problem.

Hey dissolve i tried it but i dont not worked. If it does not bother you perhaps you can be more clearly with the code. Try modifying the code a placed in the thread so i can have a better idea. Sorry for bothering so much.
 
Problem solve!

I solved the problem.

This is the code i had.

#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char * const argv[]) {
int x;
ifstream in;
in.open("Input.txt");
if(!in)
{
cout<<"\nProgram cannot find the input"<<endl;
return 1;
}
in>>x;
while (in) {
cout<<"Number"<<x<<endl;
in>>x;
}
in.close();
return 0;
}

The in.open("Input.txt"); part were you put the direction were your txt file is located is wrong the way it should be is:

in.open("/Users/the name of your computer/name of the file.txt")

if you have it on your desktop it should be like this:

("/Users/the name of your computer/Desktop/name of the file.txt")

If you have it on a folder in your desktop. After desktop put the name of the folder between / /:

("/Users/the name of your computer/Desktop/name of the folder/name of the file.txt").
And so on.

Hope this help.
If not ask in the thread and i will try to help you. :)
 
I'm sorry, I should have been more clear. Yes, "Home" is referring to your Home directory. Glad you got it figured out :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.