Reading Integers

P

Pantheronics

// file to read
12 13 14
1 1 2 3
1 2 3 1
;

/// The source file

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

int main()
{
ifstream fp;
ifstream fpusage;
char c;
char newChar[200];
int lines = 0;


fp.open("en.txt");
fpusage.open("en2.txt");
while( fp.getline(newChar,sizeof(newChar)) ){
int len = strlen(newChar);
cout << newChar << endl;
}

return 0;
}


How to read each integer individally?
 
I

indrawati.yahya

Pantheronics said:
// file to read
12 13 14
1 1 2 3
1 2 3 1
;

/// The source file

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

int main()
{
ifstream fp;
ifstream fpusage;
char c;
char newChar[200];
int lines = 0;


fp.open("en.txt");
fpusage.open("en2.txt");
while( fp.getline(newChar,sizeof(newChar)) ){
int len = strlen(newChar);
cout << newChar << endl;
}

return 0;
}


How to read each integer individally?

Have you tried:

int aNumber;
while(fp >> aNumber)
{
//use aNumber here...
}
 
P

Pantheronics

Yes I did, but I want something like buffer, this thing will overwrite
the values after their first use.
 
T

TB

Pantheronics skrev:
// file to read
12 13 14
1 1 2 3
1 2 3 1
;

/// The source file

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

int main()
{
ifstream fp;
ifstream fpusage;
char c;
char newChar[200];
int lines = 0;


fp.open("en.txt");
fpusage.open("en2.txt");
while( fp.getline(newChar,sizeof(newChar)) ){
int len = strlen(newChar);
cout << newChar << endl;
}

return 0;
}


How to read each integer individally?

#include <fstream>
#include <ostream>
#include <algorithm>
#include <vector>

int main(int argc, char* argv[])
{
std::ifstream in("en.txt");
if(in) {
std::vector<int> v;
std::copy(std::istream_iterator<int>(in),
std::istream_iterator<int>(),
std::back_inserter(v));
std::copy(v.begin(),
v.end(),
std::eek:stream_iterator<int>(std::cout," "));
}
return 0;
}
 
P

Pantheronics

Well, the complete file that i have to read is like this:
-----------------
1 2 3
4 5 6
;
1 0 0
0 1 0
0 1 1
;
---------------
If I read it through vector<int> it stops at the semi-colon line and
never reads after that... :'(
Pantheronics skrev:
// file to read
12 13 14
1 1 2 3
1 2 3 1
;

/// The source file

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

int main()
{
ifstream fp;
ifstream fpusage;
char c;
char newChar[200];
int lines = 0;


fp.open("en.txt");
fpusage.open("en2.txt");
while( fp.getline(newChar,sizeof(newChar)) ){
int len = strlen(newChar);
cout << newChar << endl;
}

return 0;
}


How to read each integer individally?

#include <fstream>
#include <ostream>
#include <algorithm>
#include <vector>

int main(int argc, char* argv[])
{
std::ifstream in("en.txt");
if(in) {
std::vector<int> v;
std::copy(std::istream_iterator<int>(in),
std::istream_iterator<int>(),
std::back_inserter(v));
std::copy(v.begin(),
v.end(),
std::eek:stream_iterator<int>(std::cout," "));
}
return 0;
}
 
T

TB

Pantheronics skrev:
TB said:
Pantheronics skrev:
// file to read
12 13 14
1 1 2 3
1 2 3 1
;

/// The source file

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

int main()
{
ifstream fp;
ifstream fpusage;
char c;
char newChar[200];
int lines = 0;


fp.open("en.txt");
fpusage.open("en2.txt");
while( fp.getline(newChar,sizeof(newChar)) ){
int len = strlen(newChar);
cout << newChar << endl;
}

return 0;
}


How to read each integer individally?
#include <fstream>
#include <ostream>
#include <algorithm>
#include <vector>

int main(int argc, char* argv[])
{
std::ifstream in("en.txt");
if(in) {
std::vector<int> v;
std::copy(std::istream_iterator<int>(in),
std::istream_iterator<int>(),
std::back_inserter(v));
std::copy(v.begin(),
v.end(),
std::eek:stream_iterator<int>(std::cout," "));
}
return 0;
}
Well, the complete file that i have to read is like this:
-----------------
1 2 3
4 5 6
;
1 0 0
0 1 0
0 1 1
;

Here you go, I hope you understand it.

#include <fstream>
#include <ostream>
#include <algorithm>
#include <vector>

int main(int argc, char* argv[])
{
std::ifstream in("en.txt");
if(in) {
std::vector<int> v;
while(!in.eof()) {
std::copy(std::istream_iterator<int>(in),
std::istream_iterator<int>(),
std::back_inserter(v));
in.clear();
in.ignore();
}
std::copy(v.begin(),
v.end(),
std::eek:stream_iterator<int>(std::cout," "));
}
return 0;
}
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top