【问题标题】:How to read both from user input and txt file - C Console如何从用户输入和 txt 文件中读取 - C 控制台
【发布时间】:2020-06-10 23:19:44
【问题描述】:

我有一个程序,用户输入一个实数对列表,用逗号分隔,然后程序将计算它的平均值、中位数、众数、计算数据数量等...

到目前为止,它可以很好地读取用户输入。我只是想知道程序是否也可以从 .txt 文件中读取。所以是一个 txt 文件,里面是用逗号分隔的实数对。

代码:

#include <stdio.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS

void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

int partition(int arr[], int low, int high)
{
    int pivot = arr[high];    // pivot 
    int i = (low - 1);  // Index of smaller element 

    for (int j = low; j <= high - 1; j++)
    {
        // If current element is smaller than the pivot 
        if (arr[j] < pivot)
        {
            i++;    // increment index of smaller element 
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}

void quickSort(int arr[], int low, int high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
           at right place */
        int pi = partition(arr, low, high);

        // Separately sort elements before 
        // partition and after partition 
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

void printArray(int arr[], int size)
{
    int i;
    for (i = 0; i < size; ++i)
        printf("%d ", arr[i]);

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

    int n1, n2;
    int x[10] = { 0 };
    int y[10] = { 0 };

    int capacity = 0;

    size_t n = sizeof(x) / sizeof(x[0]);
    int ch = 0;
    for (size_t i = 0; i < n; ++i)
    {

        scanf_s(" %d , %d ", &n1, &n2);
            x[i] = n1;
            y[i] = n2;

    }

    for (size_t j = 0; j < n; ++j)
    {
        printf("%d , %d\n", x[j], y[j]);

    }
    quickSort(x, 0, n-1);
    printArray(x, n);

    printf("Minimum is: %d", x[0]);
    printf("Maximum is: %d", x[n-1]);
    return 0;
}

当我尝试使用 CMD 从我的程序中读取 txt 文件时,它给了我一堆数字。

-858993460 , -858993460
-858993460 , -858993460
-858993460 , -858993460
-858993460 , -858993460
-858993460 , -858993460
-858993460 , -858993460
-858993460 , -858993460
-858993460 , -858993460
-858993460 , -858993460

我想我必须使用 FILE* 流或类似的东西,但是我不确定如何使用 fscan_s 来实现它...

【问题讨论】:

  • 您可以从文件中重定向输入:./program_name &lt; filename
  • 嗯,这似乎有效!但是为什么这行得通,但是当我的证明只使用类似:“program_name filename.txt”并且它会起作用时?
  • 因为&lt; 自动更改程序的标准输入以从文件中读取。在没有&lt; 的情况下执行此操作需要您在程序中编写代码以使用fopen() 打开文件并从中读取而不是标准输入。

标签: c file input console scanf


【解决方案1】:

scanf_sfscanf_s 之间的区别只是它们从哪里获取输入,正如您所说的那样。传递给fscanf_s 的第一个标识符是FILE * - 指向文件流的指针。通过调用fopen 或通过返回值的变体来初始化此文件流并获得指针。一旦你有了这个指针,你只需要把它传递给fscanf_s。它应该看起来像这样:

FILE *text_file = fopen("C:\Path\To\File.txt", "r"); //"r" == open for reading
if (text_file == NULL) //error in fopen() e.g. you don't have read permission or the file doesn't exist
{
    puts("error");
    return 1;
}
fscanf_s(text_file, format_string, varargs);

根据您的评论附注。只需将fopen() 中的文件路径替换为argv 参数,就可以从命令行中指定的文件中提取输入

您的评论:
抱歉回复慢。文档是关于fopen_s() 的,这是fopen() 的“更安全”版本。

errno_t fopen_s(
   FILE** pFile,
   const char *filename,
   const char *mode
);

基本上,语法有点不同。与其将FILE 指针设置为返回值,不如将指向该指针的指针作为第一个参数传递。用法如下:

FILE *file_ptr;
errno_t check_this = fopen_s(&file_ptr, filename, mode);

如果您需要有关传递指针到指针的工作原理的更多信息,请询问。

【讨论】:

  • 它给了我一个错误并要求我使用 fopen_s 并且需要一个额外的参数。知道我必须给它什么参数吗? FILE* text_file = fopen(argv[1], "rb"); ... while(fscanf_s(text_file," %d , %d ", &amp;n1, &amp;n2) == 2)
猜你喜欢
  • 1970-01-01
  • 2011-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 2014-06-30
相关资源
最近更新 更多