I have the following code:
#include <iostream>
#include <conio.h>
using namespace std;
long filesize(FILE *stream);
int main(void)
{
FILE *stream;
char c;
int i=0,n;
long lSize;
stream = fopen("tanc.TXT", "r");
fseek (stream , 0 , SEEK_END);
lSize = ftell (stream);
rewind (stream);
cout<<"Dati numarul de caractere:";
cin>>n;
while(!feof(stream) && i<lSize)
{
fseek(stream,i,0);
for (int j=i; j<i+n; j++)
{
c=getc(stream);
printf("%c",c);
}
i+=n;
cout<<"#";
}
fclose(stream);
getch();
}
It works well for the following example:
File content(single line):
bread horse meat salt
Output for k=2:
br#ea#d #ho#rs#e #me#at# s#al#t #
but if i have
File content(multiple lines):
bread
horse
meat
salt
Output for k=2:
br#ea#d
#
h#or#se#
m#me#at#
s#sa#lt#
Why are "s" and "m" letters printed 2 times? I guess "\n" is responsible, but i don't get how and because of that, don't know how to fix it. Any ideas? Thanks.