gpt4 book ai didi

c - 需要就地反转文件,但它只适用于一行文件

转载 作者:太空宇宙 更新时间:2023-11-04 04:52:34 25 4
gpt4 key购买 nike

所以我认为我离这里更近了,但在原地打印反转字符串时我仍然得到有趣的结果。我会尽量详细。

这里是输入:

Writing code in c
is fun

这是我想要的:

c in code Writing
fun is

这是实际输出:

C
in code Writing
fun
is

这是我的代码:

char str[1000];  /*making array large. I picked 1000 beacuse it'll never be written over.  A line will never hole 1000 characters so fgets won't write into memory where it doesnt belong*/

int reverse(int pos)
{
int strl = strlen(str)-1,i;
int substrstart = 0,substrend = 0;
char temp;

for(;;)
{
if( pos <= strl/2){ /*This will allow the for loop to iterate to the middle of the string. Once the middle is reached you no longer need to swap*/
temp = str[pos]; /*Classic swap algorithm where you move the value of the first into a temp variable*/
str[pos]= str[strl-pos]; /*Move the value of last index into the first*/
str[strl-pos] = temp; /*move the value of the first into the last*/
}

else
break;
pos++; /*Increment your position so that you are now swaping the next two indicies inside the last two*/
} /* If you just swapped index 5 with 0 now you're swapping index 4 with 1*/


for(;substrend-1 <= strl;)
{
if(str[substrend] == ' ' || str[substrend] == '\0' ) /*in this second part of reverse we take the now completely reversed*/
{
for(i = 0; i <= ((substrend-1) - substrstart)/2; i++) /*Once we find a word delimiter we go into the word and apply the same swap algorthim*/
{
temp = str[substrstart+i]; /*This time we are only swapping the characters in the word so it looks as if the string was reversed in place*/
str[substrstart+i] = str[(substrend-1)-i];
str[(substrend-1)-i] = temp;
}
if(str[substrend] == '\t' || str[substrend] == '\n')
{
str[substrend] = ' ';
for(i = 0; i <= ((substrend-1) - substrstart)/2; i++) /*Once we find a word delimiter we go into the word and apply the same swap algorthim*/
{
temp = str[substrstart+i]; /*This time we are only swapping the characters in the word so it looks as if the string was reversed in place*/
str[substrstart+i] = str[(substrend-1)-i];
str[(substrend-1)-i] = temp;
}
}
if(str[substrend] == '\0')
{
break;
}
substrstart=substrend+1;
}
substrend++; /*Keep increasing the substrend until we hit a word delimiter*/
}
printf("%s\n", str); /*Print the reversed line and then jump down a line*/
return 0;
}


int main(int argc, char *argv[])
{

char *filename; /*creating a pointer to a filename*/
FILE *file20; /*creating FIlE pointer to a file to open*/
int n;
int i;

if (argc==1) /*If there is no line parameter*/
{
printf("Please use line parameter!\n");
return(5); /*a return of 5 should mean that now line parameter was given*/
}

if(argc>1){
for(i=1; i < argc; i++)
{
filename = argv[i]; //get first line parameter
file20 = fopen(filename, "r"); //read text file, use rb for binary

if (file20 == NULL){
printf("Cannot open empty file!\n");
}

while(fgets(str, 1000, file20) != NULL) {
reverse(0);
}

fclose(file20);

}
return(0); /*return a value of 0 if all the line parameters were opened reveresed and closed successfully*/

}
}

任何人都可以指出我的反向函数逻辑中的错误吗?

最佳答案

您所写的内容将整个文件读出到一个缓冲区中,并立即对整个文件运行反向函数。

如果您想要反转第一行然后反转下一行,等等,您需要使用类似 fgets 的方法一次读取一行。反向运行每一行,一次一个,你应该得到你想要的。

http://www.cplusplus.com/reference/cstdio/fgets/

关于c - 需要就地反转文件,但它只适用于一行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13718624/

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