Thread: Reading all contents of a file

  1. #1
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47

    Reading all contents of a file

    Hey I'm using ifstream to read a file, but I need to read and print all the contents of a file.
    for instance:

    ifstream inl(filename.c_str());

    inl>>stuff;

    cout<<stuff<<endl;


    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    Try something like this (look up the correct syntax of getline)

    Code:
    ifstream file("file");
    while(!file.eof());
    {
    file.getline(var,1000);
    cout<<var;
    }
    file.close();
    If everythings right this should help you.

    [edit]Whoops, forgot to close the file [/edit]

  3. #3
    Unregistered
    Guest
    since getline() removes the terminating char from the istream buffer you may want to use plain old >> to read in one char at a time using R@mon's loop, unless you know the file is less than 1000 char long. If you know getline() is terminated by the terminating char rather than by length consideration, you can add back the terminating char if you want to, but char by char input prevents the need to do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Reading from file into structs..
    By dankas in forum C Programming
    Replies: 14
    Last Post: 10-16-2002, 10:33 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM