0

here is my code:

//..
CString strWrite;                    // "VLF,3,30,100.000000,10.000000"
strWrite.Format(_T("%s,%d,%d,%f,%f\n"), str_aName[i], anFreq[i],anFreq[i+1], afWvLen[i], afWvLen[i+1]);
        
int n = strWrite.GetLength();        // 30
m_file.Write(strWrite.GetBuffer(), n);

by doing .Write(..\n) I want to save the next data (LF,30,300..) to the new line. But it's not working and saving like this:

enter image description here

any advices??

Zrn-dev
  • 99
  • 5
  • What do you learn about the contents of `strWrite` when you step through this program in your debugger? Does it contain a `V` as the first character? If so, then look at `str_aName[i]` and go from there. Perhaps you have undefined behavior somewhere for indexing an array out of range. If it doesn't have a `V` as the first character, then go looking for places where you _previously_ wrote to this file. – paddy Jul 12 '22 at 01:24
  • All questions here should have all relevant information ***in the question itself as plain text***. Links can stop working at any time making questions meaningless. Code, data, or errors shown as images cannot be copy/pasted; or edited or compiled for further research and investigation. Can you [edit] this question, removing and replacing all links and images with all relevant information as plain text? All code must meet all requirements of a [mre]. You'll find many other questions here, with a [mre], in plain text. Please use them as an example for how your question should look. – Sam Varshavchik Jul 12 '22 at 01:29
  • 2
    The `CFile::Write()` function takes the number of bytes as a parameter, while the `CString::GetLength()` one returns the number of characters, so most probably it writes half the # of bytes. Multiply the number of characters by `sizeof(TCHAR)`. Take a look into `CFile`'s open modes and types (text/binary/unicode). Also be careful about the end of line character(s), it can be `\n` or `\r\n`. You may also want to add a BOM header to your file. If you want to use UTF-8 you need to convert the string first. Finally, consider using the `TAB` character as a separator instead of `,`, if allowed. – Constantine Georgiou Jul 12 '22 at 06:11
  • My educated guess is your problem has to do with the format of the line change you are writing. Follow @ConstantineGeorgiou's advice about it. Unfortunately, there is no standard way of writing them; some platforms use '\n', others '\r', others '\n\r', and others '\r\n'. As Andrew Tanenbaum once famously said: [“The good thing about standards is that there are so many to choose from.”](https://www.goodreads.com/quotes/589703-the-good-thing-about-standards-is-that-there-are-so) – sergiol Jul 12 '22 at 13:26
  • @ConstantineGeorgiou Thank you for response!!! yeah, that was the problem which you said and I changed second parameter of ```CFile::Write()``` function to ```strWrite.GetLength()*sizeof(TCHAR)``` and it's now working well. But, now I've got new question here: what I know is: 1byte == 8bits, and also 1char == 8bits,so, what is the difference here that write function is distinguishing?? – Zrn-dev Jul 23 '22 at 12:52
  • One character equals one byte only for the [ANSI Codepages](https://en.wikipedia.org/wiki/Windows_code_page#ANSI_code_page), eg 1252. Unicode ([UTF-16LE](https://en.wikipedia.org/wiki/UTF-16#UTF-16LE) on Windows) uses 2 bytes per character (for the most part), while [UTF-8](https://en.wikipedia.org/wiki/UTF-8) uses variable-length characters, ie 1 byte for the most commonly used chars, and then there are 2-, 3- and 4-byte long sequences. – Constantine Georgiou Jul 25 '22 at 12:34

0 Answers0