473,287 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 4461
// 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: slickn_sly | last post by:
For some reason, I'm getting an error. It doesn't seem to be reading my "input.txt" file. I'm trying to read the "input.txt" file which consits of random integers and use insertion sort to sort...
4
by: Sebastian Becker | last post by:
Hello NG, is fscanf capable of reading a memory adress from a file? I tried to find the right format-parameter, but my code doesn't seem to work... Regards, Sebastian
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
6
by: bas | last post by:
hey, I am having a lot of trouble trying to get this program to work properly. It is supposed to read integers from a file and place them into the categories for inventory. The problem seems to...
4
by: Matthew Crema | last post by:
Hello, Say I have 1000 text files and each is a list of 32768 integers. I have written a C program to read this data into a large matrix. I am using fopen in combination with fscanf to read...
11
by: H. | last post by:
I'm writing a driver for something I wrote, and can't remember how to do this very simple thing. The code I have at this point in time: int myArr; int temp; int ind = 0; cout << "enter some...
4
by: andreas.fabri | last post by:
I have a problem reading integers separated by commas with VC8 This program: ___________________________ // read.C #include <iostream> int main()
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
9
by: Eric Lilja | last post by:
Hi! I have a program with a class that needs to be able to write itself to a file in clear text format. The file has two integers and vector of struct objects. The struct has a string that can...
4
by: C++ Newbie | last post by:
Suppose I have a text file with the input: 1 2 3 4 5 6 7 8 9 10 ! Comment: Integers 1 - 10 How do I write a C++ program that reads in this line into a 10-element vector and ignores the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.