Apple XCode, C++, import text file

In summary, I was trying to figure out how to set where Xcode checks for an fstream object, and I found a suggestion that worked.
  • #1
Jamin2112
986
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
  • #2
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)?
 
  • #3
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.
 
  • #4
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.
 
  • #5
That Stack Overflow suggestion worked. Thanks.
 

FAQ: Apple XCode, C++, import text file

What is Apple XCode?

Apple XCode is an Integrated Development Environment (IDE) created by Apple for developing software for macOS, iOS, watchOS, and tvOS. It includes a suite of tools for coding, debugging, and testing, as well as a graphical user interface (GUI) builder for creating user interfaces.

What is C++?

C++ is a high-level programming language that is widely used for developing a variety of software, including operating systems, web browsers, games, and more. It is an extension of the C programming language and provides features such as object-oriented programming, memory management, and low-level access to hardware.

How do I import a text file in XCode?

To import a text file in XCode, you can use the #include preprocessor directive to include the fstream library. Then, you can use the ifstream class to open and read the contents of the text file. Example code: ifstream inputFile("textfile.txt");

Can I use C++ in XCode?

Yes, XCode supports C++ as one of its main programming languages. You can create C++ projects in XCode and use its features and tools to develop and test your code.

How can I write data to a text file in C++ using XCode?

To write data to a text file in C++ using XCode, you can use the ofstream class along with the fstream library. You can open and write to the file using the ofstream object and the open() and close() methods. Example code: ofstream outputFile("textfile.txt");
outputFile << "This is a line of text.";
outputFile.close();

Similar threads

Replies
8
Views
2K
Replies
22
Views
3K
Replies
75
Views
5K
Replies
6
Views
10K
Replies
8
Views
6K
Replies
1
Views
1K
Back
Top