How to read numbers from a text file and output them into other text file

Dear Sir,

I am trying to read numbers from a file and output these numbers into other text file.

Practice.txt

Shri
6000001
6000002
Ram
6000003
6000004

My program is like

#include<iostream>
#include<cstring>
#include<fstream>
#include<iomanip>
using namespace std;



int main()
{
long int i=6000001,j=6000004,k;
string line;
ifstream pch;
pch.open("Practice.txt");

ofstream file2;
file2.open("File2.txt");

while(!pch.eof())
{
getline(pch,line);

for(k=i;k<=j;k++)
{

pch>>k;
file2<<k;
}
}

system("pause");
return 0;
}


I CAN ABLE TO READ AND WRITE NUMBERS INTO A FILE NAMED File2.txt

BUT THE NUMBERS ARE GETTING REPEATED AND FILE SIZE IS BECOMING INFINITE.

THIS MAY BE SOME LOOP ERROR.

PLEASE HELP
hey,look here:
----------------
for(k=i;k<=j;k++)
{

pch>>k;
file2<<k;
}
----------------
you give k a value,but pch gives k another value,so...when to reach the end?
what do you do in
1
2
3
4
5
6
for(k=i;k<=j;k++)
			{

				pch>>k;
				file2<<k;
			}


just do like it :
1
2
3
4
5
6
7
while ( !eof of filein )
{
char c;
fileint.get( c );
if ( isdigit( c ) )
fileout.put(c);
}
Last edited on
it just write digit in output file but if you want you can change format of output file because all of you number put in the one line !!! just use "isdigit"
and if you want a little complexity code :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iterator>
#include <fstream>
#include <cctype>

using namespace std;

//////////////////////////////////////////////////////////////////////////

template<class InIter, class OutIter, class Pr>
OutIter myCopy(InIter InBeg, InIter InEnd, OutIter OutBeg, Pr pr)
{
	while ( InBeg != InEnd )
	{
		if ( pr( *InBeg ) )
			*OutBeg++ = *InBeg;

		InBeg++;
	}

	return OutBeg;
}

void main()
{
	ifstream fin( "a.txt" );
	ofstream fout( "b.txt" );

	myCopy(istreambuf_iterator<char>( fin ), istreambuf_iterator<char>(), ostreambuf_iterator<char>( fout ), isdigit);

	fin.close();
	fout.close();
}


if you change this place if ( pr( *InBeg ) ) to if ( pr( *InBeg ) || *InBeg == '\n' )
then your enter line write in your file
Last edited on
Dear Sir,

I am trying to use stringstream function for my program in which I am trying to change integer number to characters and then use it for further find and replace functions.
So I am giving a little program for this :

Suppose j is varying from 6000001(x) to 6000010(y).

for( j=x;j<=y;j++)
{
j;
}
std::stringstream tc;
tc<<j;
string search=tc.str();

BUT I COULD NOT STORE ALL VALUES RANGING FROM x(6000001) to y(6000010) in tc .

Actually I am not geting what is happenging because if I give directly j=6000001 then my program is giving correct results but if i ask j to vary from x to y and store all 10 integers into characters, I think it does not store the values.

Can you please help me.

Thank you
Topic archived. No new replies allowed.