Write to Text file using fprintf
-
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
-
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
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.
-
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
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 withfprintf()
. A more strict compiler like g++ would warn you about passing a non-pod object to a vararg function. -
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
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
-
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.
-
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
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.
-
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.