Results 1 to 5 of 5

Thread: Reading in characters

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Reading in characters

    I want to write a program that opens a file with ifstream and reads each character in the file and prints it out. This is what I have so far:
    PHP Code:
    #include <fstream>
    #include <iostream>
    using namespace std;

    int main()
    {
        
    char a;
        
    ifstream fin;

        
    fin.open("test.txt");
        
    fin >> num;
        
    cout << num// Works OK

        
    while (!fin.eof()) // ?
        
    {
            
    // What goes in here?
        
    }
        
    fin.close();

        return 
    0;

    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    Lively Member
    Join Date
    Jan 2000
    Posts
    123
    Not sure where your 'num' name is coming from... but anyway fstream works like cin for the most part... you would most likly need a deliminating character between each char input... or you could simply create a character array... then fin to the array.. then you can use each character individually... but it will end input to the array when a space of CR is reached.

    char a[256];//or however big you need it


    fin >> char ;

    cout<< char[0];
    cout<<char[1];

    ect...

    or use a loop to do the same.

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Sorry, it used to be 'char num' but I changed it. What's a space of CR?
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4
    jim mcnamara
    Guest
    I think he means newline = \n

  5. #5
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    you can also do this:

    Code:
    char ch;
    
    while(the_file.get(ch))
            cout << ch;
    or make a 2-dimensional char array....

    Code:
    char info[100][20];
    
    for(int i=0;i<500;i++)
    the_file >> info[i];
    [p r a e t o r i a n]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width