繁体   English   中英

如何从用户输入和 txt 文件中读取 - C 控制台

[英]How to read both from user input and txt file - C Console

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

到目前为止,它可以很好地读取用户输入。 我只是想知道有没有办法让程序也从 .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 来实现它...

scanf_sfscanf_s之间的区别只是它们从哪里fscanf_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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM