How to write CString to a .txt file
-
e.g. how to write a string "sample string" to a txt file whose full name is "C:\a.txt"? this must be a very simple question. so could anybody be so kind as to provide a little sample code? by the way, if writing to a MS Excel file is as simple, I would rather learn how to write to a Excel file.
Thank you very much!!! ------------------- I am learning C++ and English
-
e.g. how to write a string "sample string" to a txt file whose full name is "C:\a.txt"? this must be a very simple question. so could anybody be so kind as to provide a little sample code? by the way, if writing to a MS Excel file is as simple, I would rather learn how to write to a Excel file.
Thank you very much!!! ------------------- I am learning C++ and English
-
char string[100]="hello"; FILE *p; p=fopen("C:\a.txt","w"); fprintf(p,"sample string is : %s",string); ------------------------------------------ also you can see fprintf and it's examples in help .
-
char string[100]="hello"; FILE *p; p=fopen("C:\a.txt","w"); fprintf(p,"sample string is : %s",string); ------------------------------------------ also you can see fprintf and it's examples in help .
as you use C++, use C++ 'til the end... prefer
std::fstream
to manage files that the old C functions like fopen() and fprintf().std::ofstream myFile("C:\\a.txt");
myFile << (LPCSTR)myCString;
myFile.close();also, ,be careful when writing file paths... use the double
\\
!!!
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
e.g. how to write a string "sample string" to a txt file whose full name is "C:\a.txt"? this must be a very simple question. so could anybody be so kind as to provide a little sample code? by the way, if writing to a MS Excel file is as simple, I would rather learn how to write to a Excel file.
Thank you very much!!! ------------------- I am learning C++ and English
ewighell wrote:
if writing to a MS Excel file is as simple, I would rather learn how to write to a Excel file.
Its not as simple as writing to a text file, but you can find plenty of examples and documentation by googling for Excel Automation. I recommend being at least vaguely familiar with COM before trying to jump through the Office-Automation hoops, though.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
e.g. how to write a string "sample string" to a txt file whose full name is "C:\a.txt"? this must be a very simple question. so could anybody be so kind as to provide a little sample code? by the way, if writing to a MS Excel file is as simple, I would rather learn how to write to a Excel file.
Thank you very much!!! ------------------- I am learning C++ and English
If you are using MFC then use the CStdioFile::WriteString method.
CStdioFile file; file.Open (...); file.WriteString (myString); file.Close ();
-
as you use C++, use C++ 'til the end... prefer
std::fstream
to manage files that the old C functions like fopen() and fprintf().std::ofstream myFile("C:\\a.txt");
myFile << (LPCSTR)myCString;
myFile.close();also, ,be careful when writing file paths... use the double
\\
!!!
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
ewighell wrote:
if writing to a MS Excel file is as simple, I would rather learn how to write to a Excel file.
Its not as simple as writing to a text file, but you can find plenty of examples and documentation by googling for Excel Automation. I recommend being at least vaguely familiar with COM before trying to jump through the Office-Automation hoops, though.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
If you are using MFC then use the CStdioFile::WriteString method.
CStdioFile file; file.Open (...); file.WriteString (myString); file.Close ();
but it seems that I can only write strings in English into the file. it fails when it comes to Chinese characters. by the way, UNICODE has been defined in my program. does any body know why and how to fix it?
Thank you very much!!! ------------------- I am learning C++ and English
-
e.g. how to write a string "sample string" to a txt file whose full name is "C:\a.txt"? this must be a very simple question. so could anybody be so kind as to provide a little sample code? by the way, if writing to a MS Excel file is as simple, I would rather learn how to write to a Excel file.
Thank you very much!!! ------------------- I am learning C++ and English
You can use
CFile
withCArchive
for wrote to txt file_**
**_
WhiteSky