Reading from a text file, netbeans c++?


I

I

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.txt");
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;
}
 
Ad

Advertisements

K

Kuroneko

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.txt");

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

Advertisements

I

I

Turns out just had to list the target directory. Thanks though!

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++?

#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.txt");
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;
//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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top