Thread: Reading text files

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    Reading text files

    Hi everyone! I'm just getting into C++ but I have a some experience in programming (if JavaScript counts...)
    anyways I was wondering how you would check to see if there is text in a file (as opposed to none at all). For example in JavaScript I would do something like:

    Code:
    if (text_in_file!="") { ... }
    but it doesnt seem to work in C++...heres what i was doing in C++ (trimmed down to the problem):

    Code:
    char *str = new char[50];
    ifstream read_file("example.txt");
    read_file>>str;
    read_file.close(); 
    if (str!="") //<--heres where im stuck
     {
      //code goes here
     }
    else
     {
     //code goes here
     }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    20
    Heres a better way

    [code]
    #include <iostream>
    #include <fstream>

    int main()
    {
    char *Buffer;
    ifstream fin;

    Buffer= new char[80];
    fin.open("Example.txt");

    fin >> Buffer;
    fin.close();

    if( !Buffer )
    {
    cout << "Theres nothing in the text file!!"; << endl;
    }

    delete [] Buffer;
    cin.get();
    return 0;
    }

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Hm that doesn't seem to be working...are you sure the ( !Buffer ) is the right way to check if the file is empty?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    20
    or

    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
     char Buffer[80]
     fstream file("Example.txt", ios::in);
    
     file >> Buffer;
     
     if( !Buffer )    // Checks the contents of the buffer. If its empty then it proceeds the following code.
    {
     cout << "No text detected!!" << endl;
    }
    
     else
    {
     cout << Buffer << endl;
    }
    
     file.close();
     cin.get();
     return 0;
    }

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Cool

    Newbie helping a newbie!! There is an easier way!!
    Mr. C: Author and Instructor

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Heh still no luck. What's the cin.get(); for?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    20
    my bad, replace if( !Buffer ) with if( (strlen(Buffer))==0)

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    ah ok thank you...im guessing the cin.get is to keep it from closing instead of using system("PAUSE")?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Yes, because system("PAUSE") is OS dependent.
    Last edited by alpha; 02-17-2003 at 12:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 07:13 PM
  2. reading text files
    By stimpyzu in forum C++ Programming
    Replies: 11
    Last Post: 04-17-2004, 07:45 AM
  3. Reading spaces, carrage returns, eof from text files
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 05:18 AM
  4. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM