我目前正在尝试处理一个逐行读取文件并将内容放在数组中的程序。然后尝试将原始数组反转为第二个数组,以便稍后比较两个数组。我目前的代码是
#include <stdio.h>
#include <string.h>
void reversing(char buffer[], char reverse[], int stringLength);
int main(int argc, char const *argv[])
{
int stringLength = 0;
FILE *fp; //Declaring a FILE type pointer calling it fp
char buffer[64]; //Declaring a char array the size of 64 named buffer
fp = fopen("./test.txt", "r");
if(fp == NULL)
{
printf("File did not open!\n");
}
else
{
while(fgets(buffer, 64, (FILE*)fp) != NULL)
{
printf("\nNormal String: %s", buffer); //Print content in buffer(original array)
stringLength = strlen(buffer); //Storing the length of the readed input
char reverse[stringLength]; //Creating array for reversed comparision(comparision not implemented yet)
printf("String length: %d\n", stringLength); //Print string length(for debugging)
reversing(buffer, reverse, stringLength);
printf("Reversed String: %s\n", reverse);//Print content in reverse(Reverse array)
}
printf("\n\nEND OF FILE Reached!\n");
fclose(fp);
}
return 0;
}
void reversing(char buffer[], char reverse[], int stringLength)
{
int i = 0;
int j = stringLength - 1;
while(i < j)
{
reverse[j] = buffer[i];
i++;
j--;
}
}
我的测试文件只是
A
AB
ABC
ABCD
ABCDE
它应该打印到屏幕
Normal String: A
Reversed String: A
Normal String: AB
Reversed String: BA
Normal String: ABC
Reversed String: CBA
Normal String: ABCD
Reversed String: DCBA
Normal String: ABCDE
Reversed String: EDCBA
当我通过调试器运行程序时,它显示一旦在反转功能内部,字母正确交换;然而,一旦它退出该函数并且打印反向数组的调用是打印垃圾,但在垃圾内部是前两个元素正确交换。任何建议的帮助将不胜感激。
As a side note
我试图让这个变得尽可能简单,因为真正的目标或者这个程序是在ci20机器上装配并实现它。
答案 0 :(得分:2)
stringLength = strlen(buffer); //Storing the length of the readed input
char reverse[stringLength]; //Creating array for reversed comparision(comparision not implemented yet)
您没有足够的空间存储尾随\0
char reverse[stringLength];
应该是
char reverse[stringLength + 1];
并且不要忘记在反向函数的最后添加此traling \0
:
reverse[stringLength] = '\0';
但是请注意(在这种情况下)你不需要临时来反转字符串,只需传递原始字符串并交换直到达到NUL
字符,看看How do you reverse a string in place in C or C++?
答案 1 :(得分:0)
如果您正在寻找一个非常简单的解决方案,可以使用string.h
库中的strrev
。它为你翻转字符串,这意味着你不必自己进行字符串反转功能。你可以改为调用它。
这样的事情:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 64
int
main(void) {
FILE *filename;
char buffer[BUFFSIZE];
int length;
filename = fopen("test.txt", "r");
if (filename == NULL) {
fprintf(stderr, "%s\n", "Error Reading File!");
exit(EXIT_FAILURE);
}
while (fgets(buffer, BUFFSIZE, filename) != NULL) {
length = strlen(buffer)-1;
if (buffer[length] == '\n') {
buffer[length] = '\0';
}
printf("Normal String: %s\n", buffer);
printf("Reversed String: %s\n", strrev(buffer));
}
return 0;
}
答案 2 :(得分:0)
你的问题在于你的逆转功能中的while (i < j)
行。当只有一半的数组被反转时,i
最终会小于j
。相反,它应该是while (i < stringLength)
您还应该将字符数组char reverse[stringLength]
更改为char reverse[stringLength + 1]
,以便为'\0'
终止字符留出空间,您应该在反转功能结束时添加到数组中。
它应该是这样的:
void reversing(char buffer[], char reverse[], int stringLength)
{
int i = 0;
int j = stringLength - 1;
while(i < stringLength)
{
reverse[j] = buffer[i];
i++;
j--;
}
reverse[i] = '\0';
}
另请注意,文件的读取功能也在读取换行符,并且反转功能将其放在反向字符串的开头,因此输出如下所示:
Normal String: A
String length: 2
Reversed String:
A
Normal String: AB
String length: 3
Reversed String:
BA
Normal String: ABC
String length: 4
Reversed String:
CBA
Normal String: ABCD
String length: 5
Reversed String:
DCBA
Normal String: ABCDE
String length: 6
Reversed String:
EDCBA
END OF FILE Reached!
在将换行符发送到反转功能之前,我会让你知道删除换行符。如果您无法弄清楚,只需在此答案下对其进行评论,我就会编辑答案。
答案 3 :(得分:0)
为了解决我扭转数组并忘记'\0'
的问题,没有为'\0'
留出足够的空间,并处理fgets
添加换行符。这是我当前正确运行的代码。
#include <stdio.h>
#include <string.h>
void reversing(char buffer[], char reverse[], int stringLength);
int main(int argc, char const *argv[])
{
int stringLength = 0;
FILE *fp; //Declaring a FILE type pointer calling it fp
char buffer[64]; //Declaring a char array the size of 64 named buffer
fp = fopen("./test.txt", "r");
if (fp == NULL)
{
printf("File did not open\n");
return 0;
}
else
{
while(fgets(buffer, 64, (FILE*)fp) != NULL)
{
printf("\nNormal String: %s", buffer); //Print content in buffer(original array)
stringLength = strlen(buffer); //Storing the length of read input
char reverse[stringLength + 1];
reversing(buffer, reverse, stringLength);
printf("Reversed String: %s\n", reverse);//Print content in reverse(Reverse array)
}
printf("\nEND OF FILE Reached!\n");
}
fclose(fp);
return 0;
}
void reversing(char buffer[], char reverse[], int stringLength)
{
int i = 0;
int j = stringLength - 1;
while(i < stringLength)
{
reverse[j] = buffer[i]; //Swap elements
i++;
j--;
}
reverse[i] = '\0'; //Add null terminating
i = 0;
if(reverse[0] == '\n') //If first element is '\n' swift elements
{
while(i < stringLength)
{
reverse[i] = reverse[i + 1];
i++;
}
}
}
现在打印到屏幕
Normal String: A
Reversed String: A
Normal String: AB
Reversed String: BA
Normal String: ABC
Reversed String: CBA
Normal String: ABCD
Reversed String: DCBA
Normal String: ABCDE
Reversed String: EDCBA
END OF FILE Reached!
感谢@PhantomWhiskers和所有评论并提供原始问题解决方案的人。