Thread: reading a file problem

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Hi I am using ifstream but I am using the fstream.h header file does this make a difference?
    No, it shouldn't make a difference as long as you use ifstream, and open with:
    Code:
    ifstream test;
    test.open(file);
    If it's creating the file anyway, then my guess is a compiler bug, in which case, go with SlyMaelstrom's idea of using the ios::nocreate flag.

  2. #17
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    I will do thanks.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by SlyMaelstrom
    fstream.h is depreciated. It could likely be the reason your functionality is different than 7stud's. You should probably use <fstream> instead.
    That's a good point SlyMaelstrom, I failed to notice the fstream.h. That could be it. And make sure to add:
    Code:
    using namespace std;
    after the includes. So you would change the fstream.h to:
    Code:
    #include <fstream>
    using namespace std;

  4. #19
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    thanks does this mean i should remove the .h from all my header files such as:

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

    I was also wondering if I do it the other way and use ios::nocreate does the ios::noreplace have to be used as well as i don't want any of the files to be replaced?

  5. #20
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>

    ...and with ifstream, no you shouldn't have to use noreplace. The idea is to read from the file it opens... if it replaces the file... there won't be anything to read will there.
    Sent from my iPadŽ

  6. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    thanks does this mean i should remove the .h from all my header files such as:

    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include <stdlib.h>
    Yes, if your compiler has them (you might need to keep the .h for a non-standard header file like conio.h).

    [edit] Sly beat me. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #22
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks again guys, if I were to keep the ifstream.h and use ios::nocreate could this cause any further problems as well?

  8. #23
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    There is a nice article on standard C++ libraries and how they changed. I don't have the link, though.

    Basically, all the C++ dedicated libraries, like <iostream.h> and <iomanip.h> just dropped the .h and became <iostream> and <iomanip> and so on.

    The old C libraries like <stdio.h> and <time.h> and <ctype.h> dropped the .h and gained a 'c' to the front becoming <cstdio>, <ctime>, and <cctype>, respectively.

    As dwks said, non standard libraries still remain their .h extension.
    Sent from my iPadŽ

  9. #24
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by 182
    Thanks again guys, if I were to keep the ifstream.h and use ios::nocreate could this cause any further problems as well?
    No and yes... No because it works for you... and yes because that's a terrible reason to use non standard programming. In reality, you should use <fstream> and that shouldn't require the ios::nocreate. That way if someone else were to use your source code it wouldn't cause problems for them.
    Sent from my iPadŽ

  10. #25
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    thanks guys you have been so much help
    I have just disovered that #include <stdlib.h> still required the .h is there a list somewhere about which headers require the .h and which don't?

    Thanks again.

  11. #26
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No, no, no... you should reread what I said.

    #include <cstdlib>
    Sent from my iPadŽ

  12. #27
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hi sorry about that, is there an advantage to using the #include <cstdlib> instead of #include <stdlib.h> or is it just because the #include <cstdlib> is the standard?

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> fstream.h is depreciated.

    BTW, fstream.h is not deprecated. It is simply old and non-standard. The stdlib.h header is deprecated (in favor of cstdlib). The difference is that standards-compliant compilers must provide stdlib.h, but they can leave out fstream.h entirely.

    One example is the last couple versions of Visual C++. They provide stdlib.h, but not fstream.h. This is another reason to use the standard version (fstream). When you switch to a more modern compiler your code won't work if you use the old fstream.h header.

    The advantage of using cstdlib over stdlib.h is that it is the preferred version, and modern compilers place all the names in the std namespace like they do for the C++ headers. Chances are these reasons might not affect you, but it shouldn't really hurt to use the preferred version if you are programming in C++.

  14. #29
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks again, thats leaves me with one more question how do you know which librarys are C librarys which are C++ and which are non standard and still require the .h?

  15. #30
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by 182
    Hi sorry about that, is there an advantage to using the #include <cstdlib> instead of #include <stdlib.h> or is it just because the #include <cstdlib> is the standard?
    As long as your compiler supports the new headers, I would always use them. Even if you see an example program with the old headers, before compiling change them to the new ones. Some C++ library headers are:
    Code:
    <iostream>
    <fstream>
    <iomanip>
    <string>
    <sstream>
    <vector>
    <iterator>
    <algorithm>
    <bitset>
    <list>
    <deque>
    The C library headers with a 'c' added to the front are:
    Code:
    <cstdlib>
    <cstdio>
    <cmath>
    <cstring>
    <ctime>
    <cctype>
    Here's a decent library reference: www.cppreference.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM