Maor Cohen
Maor Cohen

Reputation: 956

inserting text to a file (end line isn't entered)

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!

Upvotes: 3

Views: 337

Answers (3)

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!

Upvotes: -1

Andriy Tylychko
Andriy Tylychko

Reputation: 16266

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)

Upvotes: 1

king_nak
king_nak

Reputation: 11513

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.

Upvotes: 1

Related Questions