Apple XCode, C++, import text file

  • Context: C/C++ 
  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    apple File Text
Click For Summary

Discussion Overview

The discussion revolves around issues related to importing a text file in an XCode C++ project, specifically focusing on file path and working directory concerns. Participants explore the relationship between the project's file structure and the executable's working directory.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in locating a text file within their XCode project, questioning how to set the file path for an fstream object.
  • Another participant seeks clarification on whether "the same location as my project" refers to the location of the executable file created by XCode.
  • A follow-up question asks if there is a specific name for the location where the executable is stored.
  • Another participant emphasizes that the working directory used by XCode when running the executable is crucial, rather than the location of the executable itself.
  • It is noted that the project structure allows for files to be stored in various locations, and that the project is essentially a set of links to these files.
  • A participant confirms that a suggestion from Stack Overflow resolved their issue.

Areas of Agreement / Disagreement

Participants express differing views on the significance of the executable's location versus the working directory, indicating a lack of consensus on the best approach to file management in XCode.

Contextual Notes

Participants highlight the potential confusion surrounding file paths and working directories in XCode, suggesting that assumptions about file locations may not align with how XCode operates.

Jamin2112
Messages
973
Reaction score
12
I can't figure out why my text file isn't being found. I have it saved in the same location as my project. Anyone know how I can set where XCode checks for an fstream object?

Code:
int main (int argc, char* const argv[]) {

	std::fstream file1("CollegeIsAWaste.txt", std::ios_base::in);
        std::vector<std::vector<std::string> > fileSentences = get_file_sntncs(file1);
	
	return 0;
}

std::vector<std::vector<std::string> > get_file_sntncs(std::fstream& file) { 
	// The sentences will be stored in a vector of vectors of strings:
	std::vector<std::vector<std::string> > retvec; 
	if(file.fail()) {
		std::cout << "Could not find the file. :( " << std::endl;
    } else { 
		char thischar;
		std::string thisword. 
		std::vector<std::string> thissentence;
		// --- TEST: Print to make sure it's reading correctly ----
		while (file >> std::noskipws >> thischar) {
			std::cout << thischar; 
	    // --------------------------------------------------------
		}
		// P
		
		// ... 
		
	}
	return retvec; 
}
 
Technology news on Phys.org
When you say "the same location as my project", is that where the executable file is created by your development system (which might not be the same place as other project files)?
 
DrGreg said:
When you say "the same location as my project", is that where the executable file is created by your development system (which might not be the same place as other project files)?

Is there a special name for that location? I need to figure out where it is.
 
It doesn't matter where the executable file is stored. The important thing is what Xcode uses as the working directory when it runs it. See http://stackoverflow.com/questions/3396378/change-the-working-directory-in-xcode

FWIW "the same location as my project" doesn't mean much. In most "real world" development systems you can store your files anywhere. The "project" is just a set of links to the files. For example, if you want to use your own code library and its header files in several projects, you don't want to end up with several copies, which would inevitably end up different when you fixed some bugs or added some new functions.

But the default location for creating new file most likely IS somewhere in the project directory tree.
 
That Stack Overflow suggestion worked. Thanks.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K