Thread: how to read integers from a file( novice )

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    how to read integers from a file( novice )

    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

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    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.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    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 );
    }
    }

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    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.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    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?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Assuming that each item is stored in the file like so:

    c4a16g12

    You can use something like this to decode it:
    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;
    }
    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.

    >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.

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by Prelude

    >void main( )
    I officially demote you from a C++ black belt to a C++ white belt for using void main endo.
    I get lazy when writing examples, I shouldn't really but if they want my help they get what they're given
    Couldn't think of anything interesting, cool or funny - sorry.

  8. #8
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    >> get lazy when writing examples, I shouldn't really but if they want my help they get what they're given

    Code:
    int = (keystroke * 3);
    void = (keystroke * 4);
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. c script help to read a file & update
    By indy in forum C Programming
    Replies: 8
    Last Post: 12-01-2003, 11:32 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM