我遇到了以下问题。C中的文件处理和函数
我有一个程序,它允许用户创建一个.txt
文件并添加最多10个ASCII值。 我然后关闭文件,并在读取模式下重新打开。这是因为我使用ATOI
将输入的ASCII转换为整数。
为此的工作代码在下面提供。
我的问题是:我想创建一些存储这些输入的ASCII值的数组。通过这样做,这将使我能够调用函数来检查哪些ASCII值是最小的。
fp = fopen("c:\\CTEMP\\1.txt", "w+");
{
for (x = 0; x < 10; x++)
{
printf("\nType the word you want to add. Type exit to terminate: ");
scanf("%s", word); // word is declared as char word[10]
if (strcmp(word, "exit") == 0)
{
break;
}
fprintf(fp, "%s\n", word);
words++;
}
fclose(fp);
}
fp = fopen("C:\\CTEMP\\1.txt", "r");
while (!feof(fp))
{
fscanf(fp, "%s", &word);
number = atoi(word);
printf("\nstring is \t %s\n", word);
printf("integer is \t %d\n", number);
// location = find_minimum(array,number);
// minimum = array[location];
// printf("Minimum element location = %d and value = %d.\n", location + 1, minimum);
}
scanf_s("%d");
}
- 上午我解决找到正确的最小ASCII值的问题?
- 有没有其他方式没有创建另一个数组来存储ASCII值?
[why?while(!feof(fp))'wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar
你不需要阵列。使用一个变量来保存当前的最小值。将字符串转换为数字后,检查它是否小于变量。如果是,则将新值写入变量。 – Barmar
您可以在从文件中读取的循环中执行此操作,不需要先将它们放入数组中,除非您有其他原因需要所有输入。 – Barmar