Another Question Dealing With I/O.

This is a discussion on Another Question Dealing With I/O. within the C++ Programming forums, part of the General Programming Boards category; For some reason my ofstream command isn't working, I can't run my program Code: #include <iostream> #include <fstream> int Main() ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20

    Another Question Dealing With I/O.

    For some reason my ofstream command isn't working, I can't run my program
    Code:
    #include <iostream>
    #include <fstream>
    
    int Main()
    {
    ofstream a_file ("C:\users\jason\excess\testfile.txt");
    
          }

    I keep getting these errors, which I don't understand

    Line 6: 'ofsteam' undeclared (first use this function)

    Line 6: expected ';' before 'a_file'

    Lines 6:18 incomplete universal character name \u


    and one warning

    6:18 unknown escape sequence '\i'
    Last edited by The7thCrest; 02-04-2009 at 05:24 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    is it "ofstream" or "ofsteam" (the latter missing the "r")?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    Quote Originally Posted by matsp View Post
    is it "ofstream" or "ofsteam" (the latter missing the "r")?

    --
    Mats
    I didn't misspell, trust me that was the first thing I checked, the tutorial reference that I am using has ofstream, i'll go and check to make sure that is the right command

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Fine, but in your error message it is missing the r, but in your code-sample it is not. And it is the typical thing we see very often here - someone is typing in part new code into the site, and quoting errors that belong to some OTHER code ...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    Quote Originally Posted by matsp View Post
    Fine, but in your error message it is missing the r, but in your code-sample it is not. And it is the typical thing we see very often here - someone is typing in part new code into the site, and quoting errors that belong to some OTHER code ...

    --
    Mats
    That means I misspelled the error, but in the program my command is ofstream, spelled correctly

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    MY PROBLEM IS STILL UNSOLVED... If anyone can help I would appreciate it

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So. There's not really an ostream. There is an std::ostream, though.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    Quote Originally Posted by tabstop View Post
    So. There's not really an ostream. There is an std::ostream, though.
    I'm sorry, but I have no idea what you just said, however I did just discover that even if I slightly alter the tutorial, it still does not work.
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char str[10];
    
      //Creates an instance of ofstream, and opens example.txt
      ofstream a_file ( "c:\users\Jason\Excess\example.txt" );
      // Outputs to example.txt through a_file
      a_file<<"This text will now be inside of example.txt";
      // Close the file stream explicitly
      a_file.close();
      //Opens for reading the file
      ifstream b_file ( "c:\users\Jason\Excess\example.txt" );
      //Reads one string from the file
      b_file>> str;
      //Should output 'this'
      cout<< str <<"\n";
      cin.get();    // wait for a keypress
      // b_file is closed implicitly here
    }

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    1. You need to include <fstream>
    2. You need to escape your backslashes.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    Quote Originally Posted by brewbuck View Post
    1. You need to include <fstream>
    2. You need to escape your backslashes.
    I did include <fstream>, but I don't understand what you mean by escape your backlashes.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by The7thCrest View Post
    I did include <fstream>, but I don't understand what you mean by escape your backlashes.
    Instead of:

    Code:
    "C:\users\jason\excess\testfile.txt"
    You need:

    Code:
    "C:\\users\\jason\\excess\\testfile.txt"
    Remember the string escapes. '\n' is newline. '\t' is tab -- you have a '\t' in your string. You need to escape the backslashes.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    I understand that I need double slashes now, but I still don't understand the explanation of how or why I do.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by The7thCrest View Post
    I understand that I need double slashes now, but I still don't understand the explanation of how or why I do.
    In your own example you use '\n' -- are you saying you don't know what that is?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    I didn't use /n anywhere, in my text or my code, however I do know that '/n' is a new line tag. Like
    Code:
    cout<<("Hello World \n")
    says output to the screen and then move to the nest line

  15. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by The7thCrest View Post
    I didn't use /n anywhere, in my text or my code, however I do know that '/n' is a new line tag. Like
    Code:
    cout<<("Hello World \n")
    says output to the screen and then move to the nest line
    And "\t" means a tab character. Look carefully at your string.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about file I/O.
    By The7thCrest in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2009, 04:15 PM
  2. C++ File I/O question
    By zero_cool in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2005, 10:43 AM
  3. basic i/o question cout<<str;
    By Noobie in forum C++ Programming
    Replies: 9
    Last Post: 02-08-2003, 11:11 PM
  4. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 04:14 PM
  5. Dealing with i/o streams in a class
    By ender in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2002, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21