[英]Read file - line by line to create permutations in c
So I'm having issues attempting to read a text file consisting of three lines. 因此,尝试读取由三行组成的文本文件时遇到问题。 I am trying to read each line and create permutations per line.
我正在尝试阅读每一行并为每行创建排列。 I want to continue looping through doing this to every line until I hit EOF.
我想继续遍历每一行,直到遇到EOF。 I'm trying to implement fgets within the while loop in my main.
我正在尝试在main的while循环内实现fgets。 Am I on the right track?
我在正确的轨道上吗? The main issue I am having is passing a char to my permutation method in my main.
我遇到的主要问题是将char传递给我的main的排列方法。
#include <stdio.h>
void swap(char *x, char *y)
{
char tempVariable;
tempVariable = *x;
*x = *y;
*y = tempVariable;
}
void stringPermutation(char *a, int zeroVariable, int factorial)
{
int i;
if(zeroVariable == factorial)
{
printf("%s\n", a);
}
else
{
for(i = zeroVariable; i <= factorial; i++)
{
swap((a + zeroVariable), (a + i));
stringPermutation(a, zeroVariable + 1, factorial);
swap((a + zeroVariable), (a + i));
}
}
}
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("ERROR - WRONG AMOUNT OF ARGUMENTS\n");
}
else
{
FILE *file = fopen(argv[1], "r");
int x;
printf("PERMUTATIONS:\n");
while((x = fgetc(file)) != EOF)
{
stringPermutation(x, 0, x - 1); //I'm messing up here
}
fclose(file);
}
return 0;
}
stringPermutation()
is expecting a char*
not a char
or an int
. stringPermutation()
期望使用char*
而不是char
或int
。 You are passing a char
. 您正在传递一个
char
。 It looks like stringPermutation
is using the parameter as a char*
too and so in this case the caller is wrong. 看起来
stringPermutation
将参数用作char*
,因此在这种情况下,调用者是错误的。
Looks like you need to read a few chars into a buffer and pass the buffer to stringPermutation()
. 看起来您需要将一些字符读入缓冲区,并将缓冲区传递给
stringPermutation()
。 More simply use fgets
or getline
etc to read a string in one call instead of building it char by char yourself. 更简单地使用
fgets
或getline
等在一个调用中读取字符串,而不是自己一个字符地构造char。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.