Thread: fstream

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    fstream

    When using
    Code:
    ofstream a_file("example.txt")
    , how could you get that to save as a text file to your desktop? Do you have a specific place where that goes?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    To get the path to the desktop, you can use the SHGetSpecialFolderPath http://msdn.microsoft.com/library/de...folderpath.asp function

    Code:
    	char b[MAX_PATH];
    
    	BOOL ret = ::SHGetSpecialFolderPath
    		(0, b, CSIDL_DESKTOPDIRECTORY, FALSE);
    
    	if(ret)
    	{
    		std::string s(b);
    		s += "\\yrfilename.ext";
    
    		std::cout << s;
    	}
    	else
    	{
    		std::cout << "err";
    	}

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    From the tutorial: why is it that it only outputs "this", why is it that its not displaying the whole text. How can you get it to read and display the whole file?
    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 ( "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 ( "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
    }

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Thats because the >> operator is whitespace delimited, so when it hits the
    space after the word "This", it stops reading from the file. To read in a whole
    line, look up getline ()
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    Will the
    Code:
    ofstream a_file("example.txt");
    automatically create a text file in the same folder?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In the same folder as what? It will create (or open) a file in the current working directory. Sometimes that is the folder the executable is in, sometimes it is the folder that you run the program from, sometimes it is the project folder if you run your program from an IDE. Do some testing to find out where it is when you test your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM