No such file or Directory

I'm new to I/O streams and working with files when it comes to C++ and I'm trying to write a program that will take data inside a file and output it in an organized way. I'm working on a Mac and my file is saved on the desktop under "Shopping.txt". Whenever I build the project I don't get any errors but when I run it I get the error "No such file or Directory". I'm using CodeLite and I'm not sure if there is something I need to change with my settings. Here is my code if it is any help. Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
	ifstream shoppingList;
	string item;
	int quantity;
	double itemCost, totalCost;

	shoppingList.open("Shopping.txt");
	if (shoppingList.fail()) {
		perror("Shopping.txt");
		return -1;
	}

	while (shoppingList >> item >> quantity >> itemCost >> totalCost) {
		cout << left << setw(15) << item;
		cout << right << setw(3) << quantity;
		cout << right << setw(6) << itemCost;
		cout << right << setw(7) << fixed << setprecision(2) << totalCost;
	}

	shoppingList.close();
}
closed account (SECMoG1T)
 "No such file or Directory" well you will need to provide the path to the file in your program so that it will know where to get the file, alternatively you can save the txt file inside your program
folder and it will find it when searching without providing the path.
Where in the code would I provide the Path?
closed account (SECMoG1T)
Here shoppingList.open("Shopping.txt"); for examples I use Windows i would do something like
ShoppingList.open ("c:\users\andy\desktop\mytxtfiles\Shopping.txt"); something like that

Edit it would be a bad idea to place your program txt on a different directory other tjan you program directory someone might open your file and change its components , advice save it in your program files
Last edited on
Topic archived. No new replies allowed.