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

    Okay,
    I am trying to get this program to read all contents of a text file and then print it. I have a file called tanner1.txt already made. The program only opens up and never gets through the loop. If anyone could help me, it would be appreciated.

    This is what I have:

    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>



    int main()
    {
    double var;


    ifstream file("tanner1.txt");
    while(!file.eof());
    {
    file>>var;
    cout<<var;
    }
    file.close();
    system("PAUSE");
    return 0;

    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Try this instead, it's safer than using !file.eof() and works just fine on my system. Make sure the data file is in the same directory as your .exe as well.
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ( void )
    {
      double var;
      ifstream IN ( "tanner1.txt" );
    
      while ( IN>>var )
        cout<<var<<endl;;
      IN.close();
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    you cannot read a double from a file.

    while(!file.eof());
    {
    file>>var;
    cout<<var;
    }

    =BAD

    ********************
    char character;

    while(!file.eof())
    {
    file.get (character);
    cout << character;
    }
    ********************

    string str1;

    while(!file.eof())
    {
    file >> str1;
    cout << str1;
    }

    ********************

    char array[50];
    int i=0;

    while((!file.eof())&&(i < 49))
    {

    file.get(array[i]);
    ++i;
    }

    array [i +1] = '\n';

    cout << array;

    *******************
    Blue

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Wink to read all the contents in a file

    Y do u go for too much of complexity try this one better .


    #include<iostream>
    #include<fstream>

    using namespace std;

    int main(int argc,char *argv[])
    {
    int count =0;
    if(argc!=2)
    {
    cout<<"Usage:"<<argv[0]<<"<filename>"<<endl;
    return 1;
    }
    ifstream in(argv[1]);
    if(!in)
    {
    cout<<"Cannot open input file.\n";
    return 1;
    }
    char str [255];
    while(in.peek()!=EOF)
    {
    in.getline(str,255);
    if(in)
    count = count +1;
    cout<<count<<":"str<<endl;// this prints even the line number u are reading
    }
    in.close();
    return 0;
    }


    This will help u a lot .Happy coding.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    64
    is this not easier than all of these, sorry if its not, yeah i'm not sure you can read in doubles though.

    #include <iostream>
    #include <fstream>

    void main()
    {
    ifstream inFile;
    double var;

    inFile.open("filename.txt");

    while (inFile)
    {
    inFile >> var;
    cout << var;
    }

    inFile.close();
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >you cannot read a double from a file.
    Why not? I've never had any trouble reading doubles with full precision from a file.

    -Prelude
    My best code is written with the delete key.

  7. #7
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Sorry... I just usually recommend against it because I found too many times that the bottom drops out of .eof() when words or improper data is inputted (endless loop). I have traditionally always read characters and type cast from there so that I can verify that I have number values.

    But... I tried your advice of using

    in >> doubl;

    as a condition for the while loop.

    That worked like a champ. It was very very solid and didn't matter what values were in the file (letter, number, or otherwise).

    Good stuff.... as usual.
    Blue

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Good stuff.... as usual.
    I try

    -Prelude
    My best code is written with the delete key.

  9. #9
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Code:
    return EXIT_SUCCESS;
    What the heck is this? (It was the return value for int main).

  10. #10
    Unregistered
    Guest
    anyone know the function for going back to the beginning of the file? I forgot.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    EXIT_SUCCESS is a macro constant that is defined in iostream and stdlib. It's the same thing as saying 0 except you know precisely what I meant instead of trying to figure out a magic number.

    I use the EXIT_SUCCESS and EXIT_FAILURE constants as a matter of style. Better to be explicit than to assume the reader will know what you meant, even with return values

    -Prelude
    My best code is written with the delete key.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >anyone know the function for going back to the beginning of the file?
    fseek ( FILE, 0, SEEK_SET );
    or
    FILE.seekg ( 0, ios::beg );

    Depending on whether you use a FILE * or an ifstream object

    -Prelude
    Last edited by Prelude; 02-12-2002 at 12:07 AM.
    My best code is written with the delete key.

  13. #13
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    But how would return EXIT_FAILURE; accomplish anything? Isn't the program over anyway, so if it fails it closes or if it succeeds it closes?

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Isn't the program over anyway
    Yes, but the system calls main and main returns a value to the system telling it if the program complete successfully or failed at some point. Sometimes the system will act on that value, so you need to be sure what you sent back to it. The macros are defined as such:
    #define EXIT_SUCCESS 0
    #define EXIT_FAILURE 1

    So there's really no difference except for the reader of the program.

    -Prelude
    My best code is written with the delete key.

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