【问题标题】:C (Basic stuff) - Write User Input to a FileC (Basic stuff) - 将用户输入写入文件
【发布时间】:2017-04-01 15:13:00
【问题描述】:

基本上,我想让用户在一个文本文件(2 列)中输入四个姓名和四个等级;举个例子:

98 年 3 月

迈克 60

约翰 78

贝丝 89

我希望能够读取文件(可能使用 fscanf)并显示最高/最低等级,以及它们的平均值。所以我需要为所有输入创建变量,对吧?今天在课堂上,我们学习了结构,所以我假设可能需要实现一个。我没有真正有用的代码,但这是它的开始:

.
.
.

int main()
{

int i;

FILE *Fpointer;
char input[100];
int input2[4];


Fpointer = fopen("file.txt", "w");

for(i = 0; i < 4; i++)
{
    printf("Enter a name>");
    scanf("%s", input);
    printf("Enter a grade>");
    // User Josh Bressers told me to change the below %d to %s
    scanf("%s", input2);
    // And the second %d to %s here
    // The program prints 4 names and 4 numbers perfectly now
    fprintf(Fpointer, "%s %s", input, input2);
    fprintf(Fpointer, "\n");

}

fclose(Fpointer);

return 0;


}

既然程序接受了用户输入的四个姓名和数字,我将如何读取数字并进行第一段中列出的计算?任何帮助表示赞赏!

谢谢,

TP

编辑:我自己搞定了(丑陋,但它有效)

【问题讨论】:

    标签: c file pointers input


    【解决方案1】:

    将 input2 读取为字符串,而不是整数。您可以通过 atoi() 字符串来获取整数,也可以通过将 %d 更改为 %s 将其写为字符串。

    【讨论】:

    • 好的!我将第二个 scanf 更改为 %s 并将 fprintf 中的 %d 更改为 %s ...并且它可以工作。你能解释一下为什么会这样吗?我将 input2 指定为一个整数数组,所以将它作为一个字符串对我来说似乎有点奇怪。
    • 您将 input2 定义为整数数组,编译器应该对此发出警告,您可能会遇到一些内存溢出问题,但除此之外...
    • 如果您需要读取输入或写入输出,您将需要使用字符串,因为这就是您将要得到的。在 C 中转换字符串和整数可能会很痛苦,尤其是如果您是新手。在需要整数之前,将数据作为字符串处理会更容易,然后再进行转换。
    • 好的,有道理。就一件事。我将如何读取文件并找到并显示文件中的最大整数?
    • 现在您正在谈论排序和搜索,这是一个比这里可以回答的更大的问题。您应该阅读数据结构以及如何对它们进行排序。祝你好运。
    【解决方案2】:

    我自己弄的……

    #include <stdio.h>
    #include <stdlib.h>
    
    
    // Name of file to be created and data saved in
    #define FILENAME "students_and_grades.txt"
    // Size of arrays for names
    #define NAME_ARRAY_SIZE 25
    
    
    // Declare arrays
    char arrcName1[NAME_ARRAY_SIZE];
    char arrcName2[NAME_ARRAY_SIZE];
    char arrcName3[NAME_ARRAY_SIZE];
    char arrcName4[NAME_ARRAY_SIZE];
    char arrcHighestName[NAME_ARRAY_SIZE];
    char arrcLowestName[NAME_ARRAY_SIZE];
    
    
    int main()
    {
    printf("----Tyler Procko----\n\n\n");
    
    //Declare
    int iScore1, iScore2, iScore3, iScore4;
    int iHighestScore, iLowestScore;
    double dScoreAverage;
    
    // Declare file pointer
    FILE *Fpointer;
    
    // Open file for writing
    Fpointer = fopen(FILENAME, "w");
    
    
    // Ask user for first student name and score
    printf("What is the first student's name?   ");
    scanf("%s", arrcName1);
    
    // Check range (0-100)
    do
    {
        printf("What is the first student's grade?   ");
        scanf("%d", &iScore1);
    
    } while ((iScore1 < 0) || (iScore1 > 100));
    
    
    // Ask user for second student name and score
    printf("What is the second student's name?   ");
    scanf("%s", arrcName2);
    
    // Check range (0-100)
    do
    {
        printf("What is the second student's grade?   ");
        scanf("%d", &iScore2);
    
    } while ((iScore2 < 0) || (iScore2 > 100));
    
    
    // Ask user for third student name and score
    printf("What is the third student's name?   ");
    scanf("%s", arrcName3);
    
    // Check range (0-100)
    do
    {
        printf("What is the third student's grade?   ");
        scanf("%d", &iScore3);
    
    } while ((iScore3 < 0) || (iScore3 > 100));
    
    
    // Ask user for fourth student name and score
    printf("What is the fourth student's name?   ");
    scanf("%s", arrcName4);
    
    // Check range (0-100)
    do
    {
        printf("What is the fourth student's grade?   ");
        scanf("%d", &iScore4);
    
    } while ((iScore4 < 0) || (iScore4 > 100));
    
    
    // Print student name's and grades to file
    fprintf(Fpointer, "%s %d\n", arrcName1, iScore1);
    fprintf(Fpointer, "%s %d\n", arrcName2, iScore2);
    fprintf(Fpointer, "%s %d\n", arrcName3, iScore3);
    fprintf(Fpointer, "%s %d\n", arrcName4, iScore4);
    
    // Close file
    fclose(Fpointer);
    
    
    // Open file for reading
    Fpointer = fopen(FILENAME, "r");
    
    
    // If file is there and working properly, scan it
    if (Fpointer != NULL)
    {
    // Scan file for data
    fscanf(Fpointer, "%s %d", arrcName1, &iScore1);
    fscanf(Fpointer, "%s %d", arrcName2, &iScore2);
    fscanf(Fpointer, "%s %d", arrcName3, &iScore3);
    fscanf(Fpointer, "%s %d", arrcName4, &iScore4);
    
    }
    
    // Otherwise, ERROR
    else
    {
        printf("ERROR: File not found!");
    
    }
    
    // Display it all, just for the visual
    printf("\nStudent one, %s, scored a %d.\n", arrcName1, iScore1);
    printf("Student two, %s, scored a %d.\n", arrcName2, iScore2);
    printf("Student three, %s, scored a %d.\n", arrcName3, iScore3);
    printf("Student four, %s, scored a %d.\n\n", arrcName4, iScore4);
    
    
    // Assume the first is the highest
    iHighestScore = iScore1;
    strcpy(arrcHighestName, arrcName1);
    
    // Assume the first is the lowest
    iLowestScore = iScore1;
    strcpy(arrcLowestName, arrcName1);
    
    // Compare score 2 to score 1
    // Highest
    if (iScore2 > iScore1)
    {
        iHighestScore = iScore2;
        strcpy(arrcHighestName, arrcName2);
    
    }
    
    // Lowest
    if (iScore2 < iScore1)
    {
        iLowestScore = iScore2;
        strcpy(arrcLowestName, arrcName2);
    
    }
    
    // Compare score 3 to score 2 and 1
    // Highest
    if ((iScore3 > iScore2) && (iScore3 > iScore1))
    {
        iHighestScore = iScore3;
        strcpy(arrcHighestName, arrcName3);
    
    }
    
    // Lowest
     if ((iScore3 < iScore2) && (iScore3 < iScore1))
    {
        iLowestScore = iScore3;
        strcpy(arrcLowestName, arrcName3);
    
    }
    
    // Compare score 4 to score 3, 2 and 1
    // Highest
    if ((iScore4 > iScore3) && (iScore4 > iScore2) && (iScore4 > iScore1))
    {
        iHighestScore = iScore4;
        strcpy(arrcHighestName, arrcName4);
    
    }
    
    // Lowest
     if ((iScore4 < iScore3) && (iScore4 < iScore2) && (iScore4 < iScore1))
    {
        iLowestScore = iScore4;
        strcpy(arrcLowestName, arrcName4);
    
    }
    
    // Print highest and lowest scores and their respective students
    printf("\nThe highest score was a %d from %s.\n", iHighestScore,               
    arrcHighestName);
    printf("The lowest score was a %d from %s.\n", iLowestScore, 
    arrcLowestName);
    
    
    // Perform average calculation and print result
    dScoreAverage = (iScore1 + iScore2 + iScore3 + iScore4) / 4.0;
    printf("\n\nThe test average is %.2f\n\n", dScoreAverage);
    
    
    // Close file
    fclose(Fpointer);
    
    return 0;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 2021-03-02
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多