hey all,
I would like to know how one can read integers from a file,
I've have instanced an ifstream, but with fileName.get()
it seems that you can only read chars?
cheers
Paul
hey all,
I would like to know how one can read integers from a file,
I've have instanced an ifstream, but with fileName.get()
it seems that you can only read chars?
cheers
Paul
quick example, text.txt contains 3 4 5 on the first line.
Code:#include <fstream.h> void main( ) { ifstream fileIn( "test.txt" ); int array[ 3 ]; int i; for( i = 0; i < 3; i++ ) { fileIn >>array[ i ]; cout << array [ i ] << endl; } fileIn.close; }
Couldn't think of anything interesting, cool or funny - sorry.
hey mate thx for the reply,
but how can you excactly read an integer when there are a lot of other chars in the same file:
char oldKar, kar, getal;
char in[20];
char uit[20];
int aantal;
while( !invoer.eof() )
{
if( kar == 92 )
{
oldKar = kar;
invoer.get( kar );
if( oldKar == kar ) {
if( invoer >> aantal )
{
for( int i = 0; i < getal ; i++ )
{
uitvoer.put( kar );
}
}
else
{
uitvoer.put( kar );
}
}
ok that was a very simple example, not very helpful it seems
Do you know the format of the file? Are the number separated from the characters by anything? If its is just contained as a single long string you will have to parse the whole string to find the integers
Couldn't think of anything interesting, cool or funny - sorry.
yeah I know, but basically it's an assignment for school, where you have to decode and encode a text file encoding works fine,
but it's the decoding that give me a headache, say you have this
string in the file:
e4
now this needs to be decoded to:
eeee
but I may only use the "get(.....)" method
so I guess I have to find something else?
Assuming that each item is stored in the file like so:
c4a16g12
You can use something like this to decode it:
If the strings are more complicated or surrounded by whitespace then you will have to handle several special cases. This job is dramatically harder since you are restricted to get(), but it can be done.Code:#include <iostream> #include <fstream> #include <cstdlib> #include <cctype> void print_n ( char item, int n ) { for ( int i = 0; i < n; ++i ) std::cout<< item <<std::flush; } int main() { std::ifstream input ( "test.txt" ); if ( !input.is_open() ) { std::cerr<<"Error opening file for reading\n"; exit ( EXIT_FAILURE ); } int count = 0; char i_val, save; while ( input.get ( i_val ) ) { if ( !isdigit ( i_val ) ) { print_n ( save, count ); count = 0; save = i_val; } else count = 10 * count + ( i_val - '0' ); if ( input.peek() == EOF ) print_n ( save, count ); } input.close(); return 0; }
>void main( )
I officially demote you from a C++ black belt to a C++ white belt for using void main endo.
My best code is written with the delete key.
I get lazy when writing examples, I shouldn't really but if they want my help they get what they're givenOriginally posted by Prelude
>void main( )
I officially demote you from a C++ black belt to a C++ white belt for using void main endo.
Couldn't think of anything interesting, cool or funny - sorry.
>> get lazy when writing examples, I shouldn't really but if they want my help they get what they're given
I think you're a bit hazy on how to be lazy. At the moment you are listening to the master on laziness -- you're on my turf!Code:int = (keystroke * 3); void = (keystroke * 4);