3

I have a strange problem:

I read a file into a buf and tried to run it in ssh (Linux)..

my file contains:

We 
I
a

so this is my buf:

enter image description here

now I create a new file and paste the buf into this new file:

FILE*nem_file_name;
nem_file_name= fopen("email1.clear","wb"); //create the file if not exist.
fwrite (buf, sizeof(char), strlen(buf),nem_file_name); //write the new sensored mail to the file. 

in this case, the file: email1.clear was created, but this is what it contains: We Ia

when I copy it to clipboard and paste it to this topic, it was pasted so:

We 
I
a

why there is no 'end line' in my file? I want it to be like what I have in my clipboard :/

UPDATE I tried to create the buf manually by:

char buf[10];
buf[0] = 'W';
buf[1] = 'e';
buf[2] = 32;
buf[3] = 13;
buf[4] = 10;
buf[5] = 'I';
buf[6] = 13;
buf[7] = 10;
buf[8] = 'a';
buf[9] = 0;

(note that I didn't read a file into buf, but do it manually)

and then:

FILE*nem_file_name;
nem_file_name= fopen("email1.clear","wb"); //create the file if not exist.
fwrite (buf, sizeof(char), strlen(buf),nem_file_name);

and the file email1.clear was created as I want:

We
I
a

I can't understand it!

Maor Cohen
  • 936
  • 2
  • 18
  • 33
  • what program do you use to view the file? Have you checked the encoding in it? – default Jan 15 '13 at 13:04
  • View the result file in a hex editor. – Kerrek SB Jan 15 '13 at 13:05
  • 1
    You don't have a new line (CR-LF) after 'a', "standard" text files end in a new line. – Marc Glisse Jan 15 '13 at 13:05
  • Encodings can give [strange results](http://www.hoax-slayer.com/bush-hid-the-facts-notepad.html) in Notepad, i.e. it doesn't always show what you want it to show – default Jan 15 '13 at 13:06
  • please see my update to this topic :/ – Maor Cohen Jan 15 '13 at 13:11
  • @user1961415: It is a bit unclear (to me) where exactly perceived behaviour differs from your expectation. Right now I'd guess whatever program you used to display the contents of email1.clear before copy/pasting it to your web browser is somewhat broken and not displaying `\r\n` as an actual newline. But again, it is not really clear what you are asking. – DevSolar Jan 15 '13 at 13:35
  • I have a text file of two lines. in the first line I have the letter 'a', and in the second line I have: 'b'. I read the file into the variable: buf. buf contains: buf[0] = 'a', buf[1] = 13, buf[2] = 10, buf[3] = b, buf[4] = 0. when I try to create a new file and entered the buf into the new file, I got a file with one row: 'ab'. this is in the first case. – Maor Cohen Jan 15 '13 at 13:44
  • in the second case, if I create the buf variable by myself (and not create it from reading a file): buf[0] = 'a'; buf[1] = 13; .... buf[4] = 0; the new file has two rows, like what it should be.. – Maor Cohen Jan 15 '13 at 13:46
  • How did you create the file you read the data from on Linux? Could this file contain Linux line endings (`"\n"` instead of `"\r\n"`)? Could you provide the output of `hexdump -C ` run on the Linux machine? – Florian Sowade Jan 15 '13 at 23:45

3 Answers3

1

Is the debugger-screenshot actually from your linux environment? Or did you create it on a windows-debugger?

It depends on how you read the original file. I you're using text mode (r or rt at the fopen call), Linux will convert the CRLF (13,10) into a single LF (10) character during reading. When writing this into a new file in binary mode (wb as in your code), it will stay a single LF.

Notepad cannot handle single LF characters as newlines, however, your webbrowser does obviously.

UPDATE:
End-Of-Line characters are handled differently by different Operating Systems. When opening a file in text mode, the differences are handled during reading/writing and converted to/from the system's mode. In binary mode, the bytes are read and written as is without conversion (fopen documentation).

It depends on where the program should run and what clients should read the output (Linux/Windows). When your code runs on linux, reads text files from linux and generates text files to be used in linux, use text mode (same applies for windows). If you need to mix platforms, you might have to convert line ends by yourself.

king_nak
  • 11,313
  • 33
  • 58
  • thank you, I created it from the windows debugger. I want it to work on Linux. so what should I change in my program? – Maor Cohen Jan 15 '13 at 13:49
  • I read the file into buf by: fp=fopen(name_of_file, "rb"); fread (buf, sizeof(char), BUFFER_SIZE, fp); – Maor Cohen Jan 15 '13 at 14:01
  • I tried to read the file by: fp=fopen(name_of_file, "r"); and enter the buf into the new file by: nem_file_name= fopen(name_of_file,"w"); but it doesn't work.. – Maor Cohen Jan 15 '13 at 15:05
  • First of all, I think the `b` in the `fopen()` call has no effect on Linux (see [man 3 fopen](http://man7.org/linux/man-pages/man3/fopen.3.html)). Second: Could you print the content of `buf` on Linux after reading it from the file? You can do this for example in the following way: `for (size_t i = 0; buf[i]; ++i) { std::cout << i << ": " << unsigned(buf[i]) << std::endl; }` – Florian Sowade Jan 15 '13 at 23:37
1

It's a text, why do you write it to a binary file ("wb")? Just work with text files and everything should be fine (remove b from your file open mode when you read file and when you write file)

Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112
-1

I think strlen(buf) will return the size of buf without the null char that marks end of string. You can try and write to your file like this:

fwrite (buf, sizeof(char), strlen(buf), nem_file_name);
char eos = '\0';
fputc (eos, nem_file_name);

Just my guess. Good luck!

  • 1
    There is no such thing as an "EOF char". You are probably referring to the null byte (string terminator), and yes, `strlen()` does not include it in the character count. – DevSolar Jan 15 '13 at 13:09
  • 1
    You can see the documentation for strlen [here](http://en.cppreference.com/w/cpp/string/byte/strlen) – default Jan 15 '13 at 13:10
  • Yep, the answer was not correct, sorry!. I'm quite new here as a contributor. Is there any way I can remove my answer? Should I? Thanks :) – Óscar Gómez Alcañiz Jan 15 '13 at 13:13
  • thank you but I think that you new answer is not correct also (did you see my update to the topic? I don't think it is matter what editor I use to view this file. both cases I use notepad: the first one didn't give me the desired result and the second gave me what I wanted). – Maor Cohen Jan 15 '13 at 13:16
  • @ÓscarGómezAlcañiz If you have another solution you can click the `edit` button under your answer to modify it. There should also be a `delete` button if you want to remove it completely. When you delete you can still see the answer and edit it, but it will disappear from the "public eye". This is useful if you want to edit the answer without receiving downvotes for example. – default Jan 15 '13 at 13:26
  • The problem seems to be on the null byte not being written to the file. Just to check, you could write a '\0' after the buffer writing and see if that changes anything :P – Óscar Gómez Alcañiz Jan 15 '13 at 13:30
  • Thanks @Default, I edited the answer to say something slightly better :) – Óscar Gómez Alcañiz Jan 15 '13 at 13:34
  • thank you but I got: 'fwrite' : cannot convert parameter 1 from 'char' to 'const void *' 1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast – Maor Cohen Jan 15 '13 at 13:36
  • he probably meant to use fputc (not that that will help...). – Marc Glisse Jan 15 '13 at 14:02
  • and it costed me my first downvote :( sorry for the misleading answer. I edited it anyway. – Óscar Gómez Alcañiz Jan 16 '13 at 15:32