![]() |
Reading from a text file, netbeans c++?
Hello All,
Does anyone know how and/or where to add a text file if I want to practice reading in text using Netbeans C++? Thanks Ian code: #include <iostream> #include <fstream> #include <string> #include <cstring> #include <cstdlib> using namespace std; int main() { int sum=0, count; ifstream test; ifstream readin; std::string tester; test.open("Alligator.txt"); //test.open("C:\\Users\\Ian\\C++\\Text\\Alligator.tx t"); if(!test.is_open()) { cout<<"Error opening file!"; exit(EXIT_FAILURE); } char test_array[100]; test.getline(test_array,100); //cout<<test_array; //getline(test,tester); while(test.good()) { //cout<<tester<<endl; ++sum; //getline(test,tester); } if(test.eof()) cout<<"End of file reached!\n"; else if(test.fail()) cout<<"Error of the unknown type?\n"; cout<<sum; system("pause"); return 0; } |
Re: Reading from a text file, netbeans c++?
On Thursday, 11 October 2012 17:38:21 UTC-4, I wrote:
> Hello All, > > > > Does anyone know how and/or where to add a text file if I want to practice reading in text using Netbeans C++? > > > > Thanks > > Ian > > > > code: > > > > #include <iostream> > > #include <fstream> > > #include <string> > > #include <cstring> > > #include <cstdlib> > > > > using namespace std; > > > > int main() > > { > > int sum=0, count; > > ifstream test; > > ifstream readin; > > std::string tester; > > test.open("Alligator.txt"); > > //test.open("C:\\Users\\Ian\\C++\\Text\\Alligator.tx t"); > > if(!test.is_open()) > > { > > cout<<"Error opening file!"; > > exit(EXIT_FAILURE); > > } > > char test_array[100]; > > test.getline(test_array,100); > > //cout<<test_array; > > //getline(test,tester); > > while(test.good()) > > { > > //cout<<tester<<endl; > > ++sum; > > //getline(test,tester); > > > > } > > if(test.eof()) > > cout<<"End of file reached!\n"; > > else if(test.fail()) > > cout<<"Error of the unknown type?\n"; > > cout<<sum; > > system("pause"); > > return 0; > > } Put it in the same directory as the compiled binary. (I think.) |
Re: Reading from a text file, netbeans c++?
Turns out just had to list the target directory. Thanks though!
On Thursday, October 11, 2012 8:10:46 PM UTC-5, Kuroneko wrote: > On Thursday, 11 October 2012 17:38:21 UTC-4, I wrote: > > > Hello All, > > > > > > > > > > > > Does anyone know how and/or where to add a text file if I want to practice reading in text using Netbeans C++? > > > > > > > > > > > > Thanks > > > > > > Ian > > > > > > > > > > > > code: > > > > > > > > > > > > #include <iostream> > > > > > > #include <fstream> > > > > > > #include <string> > > > > > > #include <cstring> > > > > > > #include <cstdlib> > > > > > > > > > > > > using namespace std; > > > > > > > > > > > > int main() > > > > > > { > > > > > > int sum=0, count; > > > > > > ifstream test; > > > > > > ifstream readin; > > > > > > std::string tester; > > > > > > test.open("Alligator.txt"); > > > > > > //test.open("C:\\Users\\Ian\\C++\\Text\\Alligator.tx t"); > > > > > > if(!test.is_open()) > > > > > > { > > > > > > cout<<"Error opening file!"; > > > > > > exit(EXIT_FAILURE); > > > > > > } > > > > > > char test_array[100]; > > > > > > test.getline(test_array,100); > > > > > > //cout<<test_array; > > > > > > //getline(test,tester); > > > > > > while(test.good()) > > > > > > { > > > > > > //cout<<tester<<endl; > > > > > > ++sum; > > > > > > //getline(test,tester); > > > > > > > > > > > > } > > > > > > if(test.eof()) > > > > > > cout<<"End of file reached!\n"; > > > > > > else if(test.fail()) > > > > > > cout<<"Error of the unknown type?\n"; > > > > > > cout<<sum; > > > > > > system("pause"); > > > > > > return 0; > > > > > > } > > > > Put it in the same directory as the compiled binary. (I think.) |
| All times are GMT. The time now is 09:31 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.