When using, how could you get that to save as a text file to your desktop? Do you have a specific place where that goes?Code:ofstream a_file("example.txt")
When using, how could you get that to save as a text file to your desktop? Do you have a specific place where that goes?Code:ofstream a_file("example.txt")
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"; }
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 }
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
Will the
automatically create a text file in the same folder?Code:ofstream a_file("example.txt");
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.