468,458 Members | 1,813 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

Reading Integers

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

Jul 4 '06 #1
6 4136
// file to read "en.txt"
12 13 14
1 1 2 3
1 2 3 1
Pantheronics wrote:
// 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?
Jul 4 '06 #2

Pantheronics wrote:
// 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...
}

Jul 4 '06 #3
Yes I did, but I want something like buffer, this thing will overwrite
the values after their first use.
Have you tried:

int aNumber;
while(fp >aNumber)
{
//use aNumber here...
}
Jul 4 '06 #4
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<intv;
std::copy(std::istream_iterator<int>(in),
std::istream_iterator<int>(),
std::back_inserter(v));
std::copy(v.begin(),
v.end(),
std::ostream_iterator<int>(std::cout," "));
}
return 0;
}

--
TB @ SWEDEN
Jul 4 '06 #5
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<intit stops at the semi-colon line and
never reads after that... :'(

TB wrote:
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<intv;
std::copy(std::istream_iterator<int>(in),
std::istream_iterator<int>(),
std::back_inserter(v));
std::copy(v.begin(),
v.end(),
std::ostream_iterator<int>(std::cout," "));
}
return 0;
}

--
TB @ SWEDEN
Jul 4 '06 #6
TB
Pantheronics skrev:
TB wrote:
>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<intv;
std::copy(std::istream_iterator<int>(in),
std::istream_iterator<int>(),
std::back_inserter(v));
std::copy(v.begin(),
v.end(),
std::ostream_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
;
---------------
If I read it through vector<intit stops at the semi-colon line and
never reads after that... :'(
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<intv;
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::ostream_iterator<int>(std::cout," "));
}
return 0;
}

--
TB @ SWEDEN
Jul 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by slickn_sly | last post: by
4 posts views Thread by Sebastian Becker | last post: by
8 posts views Thread by Yeow | last post: by
6 posts views Thread by bas | last post: by
4 posts views Thread by Matthew Crema | last post: by
4 posts views Thread by andreas.fabri | last post: by
10 posts views Thread by Tyler | last post: by
9 posts views Thread by Eric Lilja | last post: by
4 posts views Thread by C++ Newbie | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.