Thread: Newbie File input question

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    Newbie File input question

    Ok i've just started using files, and I can't get this bit of code to work.

    Code:
    #include<String.h> 
    #include<fstream.h> 
    
    int main( )
    {
    string str(“Hello”);
    ofstream outFile(“C:\\My Documents\\C++\\test.txt”, ios::out);
    outFile << str;
    outFile.close( );
    return 0;
    }
    When I run this code it comes up saying "fatal error C1083: Cannot open include file: 'fstream.h': No such file or directory"

    I've tried numorous things, but none of them to seem to work. Anyone help?

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Shall we play a game of "guess which OS and compiler you're using" or are you going to tell us?

    My guess it's the free visual studio express, and you haven't downloaded the platform SDK.

    Also, the first include should not begin with a capital letter.
    You should be using
    #include <fstream>
    if you're using a modern C++ compiler, and finally

    string str(“Hello”);
    Backward sloping double quotes are not the same as "Hello"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It should be <string>, not <String.h> (or <string.h>). Only the first one declares the C++ string class that you are using. When you fix that and <fstream>, you will need to specify the std namespace. The best solution is to prefix standard library names with std:: (like std::string, std::ofstream and std::ios::out). You can also just add using namespace std; under your includes, which is acceptable for small beginner programs.

    >> My guess it's the free visual studio express, and you haven't downloaded the platform SDK.
    VC++ 7.1 (2003) and later don't accept the non-standard <fstream.h> header, so that error will occur with or without the platform SDK being downloaded.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    Cheers guys!

    That's it working now, just had to remove the ".h" and add namespace like you said. And yes i am using the free version

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie question reguarding safe practice of file io
    By Ashii in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2008, 03:47 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM