Thread: A question about ifstream

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

    A question about ifstream

    With a background in DOS, I always understood that newlines in text files required TWO characters rather than one like on UNIX. So if I'm reading one character at a time using get(), how can I check for a newline?

    Secondly, how can I check if a file exists? Someone suggested I use access() but I couldn't find that documented anywhere.

    Thanks a lot!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    hi,

    you could check for newline with the '\n'

    then you could check if the file opens successfuly with the
    ifstream::fail() command,

    ie,
    // to check if open successfully
    ifstream fin("data.dat");
    if(fin.fail())
    return false;
    // to check for new line
    while(fin.get(ch) != '\n')


  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yup, in C++ newline is always one character.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    hmm., that's weird, i just tried using ifstream::fail() to check if the file opens successfully, now it won't work, you have similar problems?

    EDIT: alright, it's really weird. I got my c++ book and just to make sure if i'm really makign the right way, well, i am, but it's not working
    Last edited by mickey; 10-10-2002 at 04:25 AM.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    hmm

    So am I to understand that the file stream swallow the CR/LF characters and replaced it with one character?

  6. #6
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Code:
    '\n'
    is one character (hence the use of ' rather than "... if that makes any sense )

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    the slash is used in C++ so that the compiler now that it's not just a letter.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: hmm

    Originally posted by JMB
    So am I to understand that the file stream swallow the CR/LF characters and replaced it with one character?
    Yes, in text mode, these 2 characters are converted into 1. However, open you file in binary mode, and you'll get both. (... this is talking about reading a file)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    One more question. :)

    Hammer,

    Thank you for the clarification. How can I use ifstream in binary mode so that I get the file exactly byte-for-byte? I thought get() would solve that problem but apparantly not...

    -JMB

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <fstream.h>
    
    /* */
    int main(void)
    {
        ifstream f;
        int c;
            
        f.open("junk1.c", ios::in | ios::binary);
        
        if (!f)
        {
            cout <<"open error" <<endl;
            return 1;
        }
        
        while ((c = f.get() ) != EOF)
        {
            switch (c)
            {
                case '\n': cout <<"LF found\n"; break;
                case '\r': cout <<"CR found\n"; break;
                default: break;
            }
        }
        return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Shadow12345
    Guest
    but I couldn't find that documented anywhere.
    msdn.microsoft.com has just about everything, just type in a function name and if it exists you will find a complete documentation of the function, i.e its parameters, description of what it does and even examples. I use it often. It also has functions listed there not related to windows, i.e opengl functions

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    Thanks!

    Thanks a lot!
    Proud to be straightedge.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Simple ifstream Question
    By Paul22000 in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2008, 05:34 PM
  3. question about ifstreams
    By Amyaayaa in forum C++ Programming
    Replies: 6
    Last Post: 05-27-2008, 03:48 PM
  4. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM