gpt4 book ai didi

C for循环迭代数组与putc不同

转载 作者:行者123 更新时间:2023-11-30 17:43:51 24 4
gpt4 key购买 nike

我正在尝试从文本文件中读取,对于非打印 ascii 字符,我想打印出“^”+“G”作为 BELL 字符的示例。很像unix 的cat -v 命令。问题发生在 for 循环中,我应该存储字符,直到遇到换行符,然后将它们打印出来。 for 循环为 ctrl+G 打印“G”,为测试打印“t”“e”“s”“t”。

int readFile(FILE* inputFile) {

char input[5];
char *arrayEnd = &input[5]+1;

int anyChanges = 1;
int iochar = 0;
int i = 0;
//get index of new line
//substring of position until new line
//print substring position to end.
int printedColumns = 0;
//credit Foster Chapter 2
while (( iochar = getc(inputFile) ) != EOF )
{ //Returns 1 if no changes made, return 0 if any changes have been made.
//printf("character --> %c\n",iochar);
if(iochar != '\n') {
//This if statement checks for normal ascii characters.
//If the output is less than 72 it prints it and increments printedColumns.
if (( ' ' <= iochar ) && ( iochar <= 126 ) ) {
if(*(input + i) == *arrayEnd)
{
i = 0;
}
*(input +i) = iochar;
//printf("input array ---> %c\n",input[i]);
//printf("i:%d\n",i);
//printf("iochar:%d\n",iochar);
//putc(*(input+i), stdout);
i++;
}
//This if statement checks for the non-printing characters.
//New line is not included because it is a special case that is accounted for below
if (iochar <= 31) {

if (*(input + i) == *arrayEnd)
{
i = 0;
}
*(input + i) =94;
putc(*(input+i), stdout);
i++;

if(*(input+i)== *arrayEnd)
{
i = 0;
}
*(input + i) = iochar + 64;
putc(*(input+i), stdout);
printf("\n");
i++;

}
int b = 0;
for (b = 0;b<6;b++){
putc(*(input+b),stdout);
}
}//end if != '\n'
}//end while
return anyChanges;
}//end function

最佳答案

这似乎可以满足您正在寻找的很多功能:

#include <stdio.h>

static
int readFile(FILE *inputFile)
{
int numChanged = 0;
int iochar = 0;

while ((iochar = getc(inputFile) ) != EOF)
{
if ((' ' <= iochar && iochar <= 126) || iochar == '\n')
putc(iochar, stdout);
else if (iochar < ' ')
{
putc('^', stdout);
putc(iochar + 'A' - 1, stdout);
numChanged++;
}
else
numChanged++;
}
return numChanged;
}

int main(void)
{
printf("Number of changed characters: %d\n", readFile(stdin));
return 0;
}

它在自己的源代码上正常工作(无需更改字符 - 源代码中没有制表符),并且似乎在自己的二进制文件上也正常工作。这两种输出都不够令人兴奋,无法在此引用。请注意,它删除代码 0x7F 到 0xFF 中的字符。您可以通过针对您选择指定的任何规则适当调整 else 子句来修改它。这个问题没有提及如何对待这些角色。

如果你需要一行的最后72个字符,那么你需要读取整行;加入fgets():

#include <stdio.h>
#include <string.h>

static
int readFile(FILE *fp)
{
int numChanged = 0;
char line[4096];

while (fgets(line, sizeof(line), fp) != 0)
{
size_t len = strlen(line);
if (line[len-1] != '\n')
break; // Damn! Line to long
size_t start = 0;
if (len > 72)
start = len - 72;
for (size_t i = start; i < len; i++)
{
/* The next line is only 70 characters long, but this comment should be truncated */
if ((' ' <= line[i] && line[i] <= 126) || line[i] == '\n')
putc(line[i], stdout);
else if (line[i] < ' ')
{
putc('^', stdout);
putc(line[i] + 'A' - 1, stdout);
numChanged++;
}
else
numChanged++;
}
}
return numChanged;
}

int main(void)
{
printf("Number of changed characters: %d\n", readFile(stdin));
return 0;
}

这段代码单独运行,仔细地拼凑了源代码,产生了有趣的部分:

            start = len - 72;
for (size_t i = start; i < len; i++)
{
ine is only 70 characters long, but this comment should be truncated */
if ((' ' <= line[i] && line[i] <= 126) || line[i] == '\n')
putc(line[i], stdout);
else if (line[i] < ' ')
{

你可以决定我在计算长度时是否出现了相差一的错误。

关于C for循环迭代数组与putc不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20168971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com