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:
- you must include
b
infopen()
mode (as infout = fopen("fileout.bin", "wb");
) because encrypted data binary data, not text data. (theb
) safe both unix , windows, critical on windows , immaterial on unix. - you must not use
fputs()
write data; there 0 bytes ('\0'
) amongst encrypted values ,fputs()
stop @ first of encounters. need usefwrite()
instead, telling how many bytes write each time. - you must not insert newlines anywhere; encrypted data might contain newlines, must preserved, , no extraneous 1 can added.
- when read file in, must open binary file
"rb"
, read usingfread()
.
if base-64 encoding encrypted data, can go treating output text; that's point of base-64 encoding.
Comments
Post a Comment