// 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? 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?
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...
}
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...
}
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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()
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
| |