我正在尝试将我的程序从能够读取硬编码的name.txt改为任何名称或可读类型。我无法正确传递argc和argv,并且出现了错误,例如"主要"中的参数太少我试图在使用的函数中初始化两个,但是未确定。我如何正确使用这两个工具? (箭头指向arg线) 这是我的代码:
#include <stdio.h>
#include <stdlib.h>
// Pointer to store numbers
int *pointerNUM;
// Read in the parts file and returns the length
int readFile(int argc, char *argv[]) //<-----------------------------
{
// File pointer
FILE *fptr;
// numberOfNUM for number of numbers
// cntVAR for counter variable
int numberOfNUM, cntVAR;
// Open the file for reading
fptr = fopen(argv[1], "r"); //<-----------------------------------------
// Check that it opened properly
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}// End of if condition
// Reads number of numbers in the file
fscanf(fptr, "%d", &numberOfNUM);
// Dynamically allocates memory to pointer pointerNUM
pointerNUM = (int *) calloc(numberOfNUM, sizeof(int));
// Loops numberOfNUM times
for(cntVAR = 0; cntVAR < numberOfNUM; cntVAR++)
// Reads each number and stores it in array
fscanf(fptr, "%d", &pointerNUM[cntVAR]);
// Returns the length of the numbers
return numberOfNUM;
}// End of function
// Function to display numbers
void display(int numberOfNUM)
{
int cntVAR;
// Loops numberOfNUM times
for(cntVAR = 0; cntVAR < numberOfNUM; cntVAR++)
// Displays each number
printf("%4d, ", pointerNUM[cntVAR]);
}// End of function
// Function for insertion sort
void insertionSort(int numberOfNUM)
{
int x, key, y;
// Loops numberOfNUM times
for (x = 1; x < numberOfNUM; x++)
{
// Stores i index position data in key
key = pointerNUM[x];
// Stores x minus one as y value
y = x - 1;
/*
Move elements of pointerNUM[0..x - 1], that are greater than key,
to one position ahead of their current position
*/
while (y >= 0 && pointerNUM[y] > key)
{
// Stores pointerNUM y index position value at pointerNUM y next index position
pointerNUM[y + 1] = pointerNUM[y];
// Decrease the y value by one
y = y - 1;
}// End of while
// Stores the key value at pointerNUM y plus one index position
pointerNUM[y + 1] = key;
}// End of for loop
}// End of function
// main function
int main()
{
// To store the numbers of number in file
int numberOfNUM;
// Calls the function to read numbers and stores the length
numberOfNUM = readFile(); <-----------------------------------------
// Calls the function to displays the numbers before sorting
printf("\n Before sort : ");
display(numberOfNUM);
// Calls the function for sorting
insertionSort(numberOfNUM);
// Calls the function to displays the numbers after sorting
printf("\n After sort: ");
display(numberOfNUM);
}
答案 0 :(得分:2)
a
函数的原型是错误的。如果你想在C中使用argc和argv,你需要使用支持它们的main版本。
main()
然后运行它:
int main(int argc, char *argv[])
{
// Check argc, and argv for correctness
numberOfNum = readFile(argc, argv);
...
}
答案 1 :(得分:1)
我注意到有四个即时错误:
int readFile(int argc, char *argv[])
...
numberOfNUM = readFile();
你的函数readFile
有两个参数,但是当你调用它时你根本不传递任何参数:
第二,你似乎误解了argc
和argv
。这些只是我们给main()
的参数的通用名称。实际上,你可以将它们命名为任何你想要的东西,即:
int main(int snapple, char **horses)
导致这一点:
int main()
您实际上并未接受argc
或argv
,您需要将这些内容添加到main
。这样做可以解决您的主问题,但请确保将其提供给readFile
来电。
第三,您永远不会fclose
您打开的文件。你需要这样做。
我注意到的第四件事有点迂腐,但你将main
定义为返回int
,但你没有回归。