gpt4 book ai didi

c - 反转文件中的单词

转载 作者:行者123 更新时间:2023-11-30 19:57:13 24 4
gpt4 key购买 nike

有人可以提示我我做错了什么吗?我尝试编译它,但我不确定这里出了什么问题:

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

int main()
{
int k, i, j, len;
FILE *f;
char aux, str[100];

f = fopen("rev.txt", "r");
len = strlen(str);
if (f == NULL) {
printf("File not found!");
return 0;
}

while (fgets(str, 80, f) != NULL) {
for (j = 0; j < len; j++)
if ((isalpha(str[j]) != 1) || (j == len - 1))
if (j < len - 1)
k = j - 1;
else
k = j;
i = 0;
while (i < k) {
aux = str[i];
str[i] = str[k];
str[k] = aux;
i++;
k--;
}

printf("%s",str);
}
}

因此,在上面的代码中,我尝试反转名为 rev 的文件中的单词,但每当我运行它时,它都会打印奇怪的字符。有什么帮助吗?

最佳答案

正确缩进代码并打开警告可以揭示问题。您正在使用forif没有 block 。虽然这是合法的,但很容易出错。特别是...

    for(j=0;j<len;j++)
if ((isalpha(str[j])!=1) || (j==len-1))
if(j<len-1)
k=j-1;
else
k=j;
i=0;

老实说,我不确定这里合适的支撑是什么。我的编译器警告了 "dangling else"所以有些事情不对劲。

test.c:21:9: warning: add explicit braces to avoid dangling else [-Wdangling-else]
else

我怀疑你是这个意思。

    for(j=0;j<len;j++) {
if((isalpha(str[j])!=1) || (j==len-1)) {
if(j<len-1) {
k=j-1;
}
else {
k=j;
}
}
}

无论哪种方式,始终使用 block 。编译时始终带有警告。我用-Wall -Wshadow -Wwrite-strings -Wextra -Wconversion -std=c99 -pedantic .

<小时/>

另一个问题是:

len = strlen(str);

此时str未初始化,因此它将包含垃圾。 len将是该垃圾的长度。

len也永远不会改变,但str的内容随着每行读取而变化。相反,您需要检查 str 的长度在每个 fgets 之后的循环内打电话。

while(fgets(str,80, f) != NULL) {
len = strlen(str);
...
}
<小时/>

您的str缓冲区为 100,但您只允许 fgets 80 个字符。为了避免这种情况,请使用 sizeof(str)而不是硬编码。注意:这仅适用于堆栈分配的内存。

while(fgets(str, sizeof(str), f) != NULL) {
len = strlen(str);

当您使用行缓冲区时,没有理由吝啬。它只分配一次。对于行缓冲区来说 80 或 100 非常小。给它 4096 字节以允许很长的行。

<小时/>

通过这些修复,您的代码可以工作,但我们可以对其进行改进。特别是整个for循环似乎没有必要。我怀疑它所做的只是试图将换行符保留在反转字符串的末尾。无需查看整个字符串。 fgets逐行读取,如果有换行符,则总是在最后。

// Convert the length to an index
k = strlen(str) - 1;

// Leave the trailing newline alone
if( str[k] == '\n' ) {
k--;
}

这样,加上更好的变量名称,仅根据需要声明变量,并使用正确的类型,我们得到......

while(fgets(str, sizeof(str), f) != NULL) {
// Check if someone snuck a null byte into the file.
if( !*str ) {
continue;
}

// Convert the length to an index
size_t back = strlen(str) - 1;

// Leave the trailing newline alone
if( str[back] == '\n' ) {
back--;
}

// Swap characters
size_t front = 0;
while(front < back) {
char tmp = str[front];
str[front] = str[back];
str[back] = tmp;
front++;
back--;
}

printf("%s",str);
}

使用指针而不是索引可以进一步简化。

while(fgets(str, sizeof(str), f) != NULL) {
// A pointer to the last character of str
char *back = &str[strlen(str) - 1];

// Leave the trailing newline alone
if( *back == '\n' ) {
back--;
}

// Swap characters
for(
char *front = str;
front < back;
front++, back--
) {
char tmp = *front;
*front = *back;
*back = tmp;
}

printf("%s",str);
}

关于c - 反转文件中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51642579/

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