Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Reading from a text file, netbeans c++? (http://www.velocityreviews.com/forums/t953270-reading-from-a-text-file-netbeans-c.html)

I 10-11-2012 09:38 PM

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

Kuroneko 10-12-2012 01:10 AM

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.)

I 10-14-2012 08:20 PM

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 01:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.