Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to write CString to a .txt file

How to write CString to a .txt file

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutoriallearning
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ewighell
    wrote on last edited by
    #1

    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

    V Z A H 4 Replies Last reply
    0
    • E ewighell

      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

      V Offline
      V Offline
      V_shr
      wrote on last edited by
      #2

      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 .

      E T 2 Replies Last reply
      0
      • V V_shr

        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 .

        E Offline
        E Offline
        ewighell
        wrote on last edited by
        #3

        this is enough ,thank you very much

        1 Reply Last reply
        0
        • V V_shr

          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 .

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          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 1 Reply Last reply
          0
          • E ewighell

            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

            Z Offline
            Z Offline
            Zac Howland
            wrote on last edited by
            #5

            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 1 Reply Last reply
            0
            • E ewighell

              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

              A Offline
              A Offline
              Andre xxxxxxx
              wrote on last edited by
              #6

              If you are using MFC then use the CStdioFile::WriteString method. CStdioFile file; file.Open (...); file.WriteString (myString); file.Close ();

              E 1 Reply Last reply
              0
              • T toxcct

                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 Offline
                E Offline
                ewighell
                wrote on last edited by
                #7

                Thank you, I'll follow the rule in the future

                1 Reply Last reply
                0
                • Z Zac Howland

                  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 Offline
                  E Offline
                  ewighell
                  wrote on last edited by
                  #8

                  Thank you very much!!! I am happy that I have never planned to become a software engineering.

                  1 Reply Last reply
                  0
                  • A Andre xxxxxxx

                    If you are using MFC then use the CStdioFile::WriteString method. CStdioFile file; file.Open (...); file.WriteString (myString); file.Close ();

                    E Offline
                    E Offline
                    ewighell
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • E ewighell

                      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

                      H Offline
                      H Offline
                      Hamid Taebi
                      wrote on last edited by
                      #10

                      You can use CFilewith CArchive for wrote to txt file

                      _**


                      **_

                      WhiteSky


                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups