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. Write to Text file using fprintf

Write to Text file using fprintf

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 Posts 5 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.
  • M Offline
    M Offline
    manju 3
    wrote on last edited by
    #1

    Hi all, I am trying to write one string to a file.But i am getting only one character saved in my Text.txt file.Output text file contains only "a" in it. How can i get whole string saved in my text file. Here is the code i am trying:

    FILE *fptr;
    CString Text;
    Text = "asdasdasdasd";
    fptr = fopen("D:\\Test.txt","w+");
    if(fptr== NULL)
    exit(1);
    fprintf(fptr ,"%s",Text);
    fclose(fptr);

    If i write it as ""asdasdasdasd"" instead of passing the CString,it writes in the text file fprintf(fptr ,"%s",""asdasdasdasd""); Can anyone help me,where i am going wrong. Thanks Manju

    L P _ 4 Replies Last reply
    0
    • M manju 3

      Hi all, I am trying to write one string to a file.But i am getting only one character saved in my Text.txt file.Output text file contains only "a" in it. How can i get whole string saved in my text file. Here is the code i am trying:

      FILE *fptr;
      CString Text;
      Text = "asdasdasdasd";
      fptr = fopen("D:\\Test.txt","w+");
      if(fptr== NULL)
      exit(1);
      fprintf(fptr ,"%s",Text);
      fclose(fptr);

      If i write it as ""asdasdasdasd"" instead of passing the CString,it writes in the text file fprintf(fptr ,"%s",""asdasdasdasd""); Can anyone help me,where i am going wrong. Thanks Manju

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I suspect your CString may be defaulting to Unicode (check your project settings), so it appears as though you have only written a single character. Try using CStringA and see what happens.

      One of these days I'm going to think of a really clever signature.

      C 1 Reply Last reply
      0
      • M manju 3

        Hi all, I am trying to write one string to a file.But i am getting only one character saved in my Text.txt file.Output text file contains only "a" in it. How can i get whole string saved in my text file. Here is the code i am trying:

        FILE *fptr;
        CString Text;
        Text = "asdasdasdasd";
        fptr = fopen("D:\\Test.txt","w+");
        if(fptr== NULL)
        exit(1);
        fprintf(fptr ,"%s",Text);
        fclose(fptr);

        If i write it as ""asdasdasdasd"" instead of passing the CString,it writes in the text file fprintf(fptr ,"%s",""asdasdasdasd""); Can anyone help me,where i am going wrong. Thanks Manju

        P Offline
        P Offline
        pasztorpisti
        wrote on last edited by
        #3

        The Text parameter you pass to fprintf() is a CString object, and and the %s format specifier expects a (const char*) or (char*) there. Instead of this you are pushing the whole CString object to the stack and the first 4 bytes of that CString instance are used as a (const char*). You have to convert the CString to either (const char*) or (char*) before using it with fprintf(). A more strict compiler like g++ would warn you about passing a non-pod object to a vararg function.

        1 Reply Last reply
        0
        • M manju 3

          Hi all, I am trying to write one string to a file.But i am getting only one character saved in my Text.txt file.Output text file contains only "a" in it. How can i get whole string saved in my text file. Here is the code i am trying:

          FILE *fptr;
          CString Text;
          Text = "asdasdasdasd";
          fptr = fopen("D:\\Test.txt","w+");
          if(fptr== NULL)
          exit(1);
          fprintf(fptr ,"%s",Text);
          fclose(fptr);

          If i write it as ""asdasdasdasd"" instead of passing the CString,it writes in the text file fprintf(fptr ,"%s",""asdasdasdasd""); Can anyone help me,where i am going wrong. Thanks Manju

          _ Offline
          _ Offline
          _AnsHUMAN_
          wrote on last edited by
          #4

          Are you building your project with "Use Unicode Character Set" or "Use Multi-Byte Character Set" character set. Change it to "Not Set" Other alternative would be to use string instead of CString. or like this with the above options set:

          FILE *fptr;
          CString Text;
          Text = "asdasdasdasd";
          fptr = _wfopen (_T("D:\\Test.txt"),_T("w+"));
          if(fptr== NULL)
          exit(1);
          fwprintf(fptr,_T("%s"),Text);
          fclose(fptr);

          You talk about Being HUMAN. I have it in my name AnsHUMAN

          1 Reply Last reply
          0
          • L Lost User

            I suspect your CString may be defaulting to Unicode (check your project settings), so it appears as though you have only written a single character. Try using CStringA and see what happens.

            One of these days I'm going to think of a really clever signature.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            I suspect you are right.

            Veni, vidi, vici.

            L 1 Reply Last reply
            0
            • M manju 3

              Hi all, I am trying to write one string to a file.But i am getting only one character saved in my Text.txt file.Output text file contains only "a" in it. How can i get whole string saved in my text file. Here is the code i am trying:

              FILE *fptr;
              CString Text;
              Text = "asdasdasdasd";
              fptr = fopen("D:\\Test.txt","w+");
              if(fptr== NULL)
              exit(1);
              fprintf(fptr ,"%s",Text);
              fclose(fptr);

              If i write it as ""asdasdasdasd"" instead of passing the CString,it writes in the text file fprintf(fptr ,"%s",""asdasdasdasd""); Can anyone help me,where i am going wrong. Thanks Manju

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Following on from pasztorpisti's answer[^], this MSDN page[^] explains what you need to do.

              One of these days I'm going to think of a really clever signature.

              1 Reply Last reply
              0
              • C CPallini

                I suspect you are right.

                Veni, vidi, vici.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                No, I think pasztorpisti is right, the string needs to be cast to LPCTSTR to get the content.

                One of these days I'm going to think of a really clever signature.

                C 1 Reply Last reply
                0
                • L Lost User

                  No, I think pasztorpisti is right, the string needs to be cast to LPCTSTR to get the content.

                  One of these days I'm going to think of a really clever signature.

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Now I'm quite confident you are right. :)

                  Veni, vidi, vici.

                  L 1 Reply Last reply
                  0
                  • C CPallini

                    Now I'm quite confident you are right. :)

                    Veni, vidi, vici.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    I hope your confidence is not misplaced.

                    One of these days I'm going to think of a really clever signature.

                    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