C - 在 while 循环的开头和结尾附加字符串
C - Appending strings at beginning and end of while loop
我正在尝试构造一个字符串 - 对于循环中的每 80 个字符,在该行的开头添加 7 个制表符,并在末尾添加一个新行。
它应该打印出 7 个制表符,然后是 80 个字符,然后是 1 个新行等等。
然而,奇怪的事情正在发生。它在前 2 个字符之后直接打印一个新行,然后所有内容都从那时起倾斜。
我也不确定为什么我需要 % 40 而不是 % 80 - 是因为有 2 个字节吗?
我想我通常会对 2 个字节感到困惑。
void do_file(FILE *in, FILE *out, OPTIONS *options)
{
char ch;
int loop = 0;
int sz1,sz2,sz3;
int seeker = offsetof(struct myStruct, contents.datas);
//find total length of file
fseek(in, 0L, SEEK_END);
sz1 = ftell(in);
//find length from beggining to struct beginning and minus that from total length
fseek(in, seeker, SEEK_SET);
sz2 = sz1 - ftell(in);
int tabs = (sz2 / 80) * 8;// Total size / size of chunk * 8 - 7 tabs and 1 new line char
sz3 = ((sz2 + 1 + tabs) * 2); //Total size + nuls + tabs * 2 for 2 bytes
char buffer[sz3];
char *p = buffer;
buffer[0] = '[=10=]';
while (loop < sz2)
{
if(loop % 40 == 0){
//Add 7 tabs to the beginning of each new line
p += sprintf(p, "%s", "\t\t\t\t\t\t\t");
}
fread(&ch, 1, 1, in);
//print hex char
p += sprintf(p, "%02X", (ch & 0x00FF));
if(loop % 40 == 0){
//Add a new line every 80 chars
p += sprintf(p, "%s", "\n");
}
strcat(buffer, p);
loop++;
}
printf("%s", buffer);
}
However, something strange is happening. It's printing a new line straight after the first 2 chars and then everything is skewed from then on.
因为初始值为loop
,用int loop = 1;
试试
I'm also not sure why I need % 40 rather than % 80 - is it because there are 2 bytes?
I think generally im getting confused by 2 bytes.
重点是,对于您在输入文件中读取的每个字符,您在 buffer
中写入两个字符,因为您决定将字符打印为两个字节 (%02X
)。
现在你的需求是什么:
- 在 input 文件中每 80 个字符插入 LF 和制表符? (这是你的期待吗?)
- 在 output 的每个 80 个字符中插入 LF 和制表符? (这就是你编码的)
我正在尝试构造一个字符串 - 对于循环中的每 80 个字符,在该行的开头添加 7 个制表符,并在末尾添加一个新行。
它应该打印出 7 个制表符,然后是 80 个字符,然后是 1 个新行等等。
然而,奇怪的事情正在发生。它在前 2 个字符之后直接打印一个新行,然后所有内容都从那时起倾斜。
我也不确定为什么我需要 % 40 而不是 % 80 - 是因为有 2 个字节吗?
我想我通常会对 2 个字节感到困惑。
void do_file(FILE *in, FILE *out, OPTIONS *options)
{
char ch;
int loop = 0;
int sz1,sz2,sz3;
int seeker = offsetof(struct myStruct, contents.datas);
//find total length of file
fseek(in, 0L, SEEK_END);
sz1 = ftell(in);
//find length from beggining to struct beginning and minus that from total length
fseek(in, seeker, SEEK_SET);
sz2 = sz1 - ftell(in);
int tabs = (sz2 / 80) * 8;// Total size / size of chunk * 8 - 7 tabs and 1 new line char
sz3 = ((sz2 + 1 + tabs) * 2); //Total size + nuls + tabs * 2 for 2 bytes
char buffer[sz3];
char *p = buffer;
buffer[0] = '[=10=]';
while (loop < sz2)
{
if(loop % 40 == 0){
//Add 7 tabs to the beginning of each new line
p += sprintf(p, "%s", "\t\t\t\t\t\t\t");
}
fread(&ch, 1, 1, in);
//print hex char
p += sprintf(p, "%02X", (ch & 0x00FF));
if(loop % 40 == 0){
//Add a new line every 80 chars
p += sprintf(p, "%s", "\n");
}
strcat(buffer, p);
loop++;
}
printf("%s", buffer);
}
However, something strange is happening. It's printing a new line straight after the first 2 chars and then everything is skewed from then on.
因为初始值为loop
,用int loop = 1;
I'm also not sure why I need % 40 rather than % 80 - is it because there are 2 bytes?
I think generally im getting confused by 2 bytes.
重点是,对于您在输入文件中读取的每个字符,您在 buffer
中写入两个字符,因为您决定将字符打印为两个字节 (%02X
)。
现在你的需求是什么:
- 在 input 文件中每 80 个字符插入 LF 和制表符? (这是你的期待吗?)
- 在 output 的每个 80 个字符中插入 LF 和制表符? (这就是你编码的)