Writing line to a file using C -


i'm doing this:

file *fout; fout = fopen("fileout.txt", "w"); char line[255]; ... strcat(line, "\n"); fputs(line, fout); 

but find when open file in text editor

line 1  line 2 

if remove strcat(line, "\n"); get.

line 1line2 

how fout be

line 1 line 2 

the puts() function appends newline string given write stdout; fputs() function not that.

since you've not shown code, can hypothesize you've done. but:

strcpy(line, "line1"); fputs(line, fout); putc('\n', fout); strcpy(line, "line2\n"); fputs(line, fout); 

would produce result require, in 2 different ways each used twice achieve consistency (and code should consistent — leave 'elegant variation' literature writing, not programming).


in comment, say:

i'm looping through file encrypting each line , writing line new file.

oh boy! base-64 encoding encrypted data? if not, then:

  1. you must include b in fopen() mode (as in fout = fopen("fileout.bin", "wb");) because encrypted data binary data, not text data. (the b) safe both unix , windows, critical on windows , immaterial on unix.
  2. you must not use fputs() write data; there 0 bytes ('\0') amongst encrypted values , fputs() stop @ first of encounters. need use fwrite() instead, telling how many bytes write each time.
  3. you must not insert newlines anywhere; encrypted data might contain newlines, must preserved, , no extraneous 1 can added.
  4. when read file in, must open binary file "rb" , read using fread().

if base-64 encoding encrypted data, can go treating output text; that's point of base-64 encoding.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -