Apple XCode, C++, import text file

  • C/++/#
  • Thread starter Jamin2112
  • Start date
  • #1
986
9

Main Question or Discussion Point

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; 
}
 

Answers and Replies

  • #2
DrGreg
Science Advisor
Gold Member
2,287
892
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
986
9
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
AlephZero
Science Advisor
Homework Helper
6,994
291
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
986
9
That Stack Overflow suggestion worked. Thanks.
 

Related Threads on Apple XCode, C++, import text file

  • Last Post
Replies
12
Views
8K
Replies
0
Views
3K
  • Last Post
Replies
14
Views
2K
  • Last Post
Replies
13
Views
1K
  • Last Post
Replies
14
Views
1K
  • Last Post
Replies
5
Views
9K
  • Last Post
2
Replies
33
Views
2K
  • Last Post
Replies
10
Views
11K
Replies
6
Views
863
Replies
12
Views
13K
Top