【问题标题】:Advice needed with grabbing names and numbers from a txt file从 txt 文件中获取姓名和数字所需的建议
【发布时间】:2013-03-23 01:19:20
【问题描述】:

我正在编写一个程序,它从 txt 中获取名称和数字,并将它们添加到数组中,具体取决于它是字符串还是数字。

我目前的程序是这样的:

#include <stdio.h>

int main() {
    FILE * ifp = fopen("input.txt","r");
    FILE * ofp = fopen ("output.txt", "w");
    int participants = 0, i , j;
    char name [10];
    int grade [26];

    fscanf(ifp, "%d", &participants);

    for (i = 1; i < participants; i++) {
        fscanf(ifp, "%s", name);          
        for (j = 0; j < 8; j++) {
            fscanf(ifp, "%d", &grade[j]);  
        }
    }

    printf( "%d\n", participants);
    printf( "%s\n", name);
    printf( "%d\n", grade);

    fclose(ifp);
    fclose(ofp);

    return 0;
}

我的输出是这样的:

2
Optimus
2686616

我的txt文件是这样的:

2 
Optimus 
45 90 
30 60 
25 30 
50 70 
Megatron 
5 6 
7 9 
3 4 
8 10 

关于我如何制作它的任何想法,以便它显示如下:

2 
Optimus 
Megatron 
45 
90 
30 
60 
25 
30 
50 
70 
5 
6 
7 
9 
3 
4 
8 
10 

【问题讨论】:

  • 如果数据具有特定结构,请考虑使用能够捕获该结构的格式。 - 同样在这种情况下,请在您的问题中提供输入的结构,以避免猜测。比如第一个数字是2,条目数?
  • 我只需要找到一种方法将数字放入数组中。一旦我弄清楚了,我就可以继续。我只是遇到了障碍。
  • 只需创建一个数组并将数字加载到其中?
  • 我必须从文件中读取数字,这就是我尝试的原因:fscanf(ifp, "%s", name) 和 fscanf(ifp, "%d", &grade[j] )
  • 好吧,我会选择strtokatoi,但结果应该是一样的?

标签: c


【解决方案1】:

要以请求的格式存储数据,您需要一个二维数组来存储您的值。由于参与者的数量是先验未知的,因此您必须动态分配所需的空间并稍后释放它。请找到您的代码的修改版本,如下所示

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

int main() 
{
    FILE * ifp = fopen("input.txt","r");
    FILE * ofp = fopen ("output.txt", "w");
    int participants = 0, i , j;
    char **name; // Changed the name to a 2D array
    int **grade; // Changed the grade to a 2D array
    char *curname; // Temp pointer to current name
    int  *curgrade; // Temp pointer to current array of grades



    fscanf(ifp, "%d", &participants);

    // Allocate memory
    name = malloc((sizeof(char *) * participants)); // Allocate pointers to hold strings for the number of participants
    grade = malloc((sizeof(int *) * participants)); // Allocate pointers to hold grades for the number of participants

    for(i = 0; i < participants; i++) {
        name[i] = malloc((sizeof(char) * 10)); //Assumption: Name doesn't exceed 10 characters
        grade[i] = malloc((sizeof(int) * 8)); // Assumption: Only 8 grades
   }

    for (i = 0; i < participants; i++) { /* Fixed: index i should start from 0 */
        curname = name[i]; // Get pointer to current name
        curgrade = &grade[i][0]; // Get pointer to array of current grades
        fscanf(ifp, "%s", curname); // Read name into current name pointer
        for (j = 0; j < 8; j++) {
            fscanf(ifp, "%d", &curgrade[j]); // Read grades into current array
        }
    }


    printf("%d\n", participants);
    for(i = 0; i < participants; i++) {
        printf("%s\n", name[i]);
    }
    for(i = 0; i < participants; i++) {
        curgrade = &grade[i][0];
        for (j = 0; j < 8; j++) {
            printf("%d\n", curgrade[j]);
        }
    }

    fclose(ifp);
    fclose(ofp);

    for(i = 0; i < participants; i++) {
        free(name[i]); // Free the array of individual names
        free(grade[i]); // Free the array of grades for every participant
    }

    free(grade); // Free the grades pointer
    free(name); // Free the names pointer

return 0;

}

【讨论】:

    【解决方案2】:

    使用strtok从文件中生成标记,在换行符和空格处分割,然后检查每个字符串是否是一个数字,通过尝试解析它,使用itoa或类似的东西,如果可解析添加到数字否则给名字。

    【讨论】:

    • 看起来他们知道输入格式是什么,只是在正确输出(和保存数据)方面遇到了麻烦
    猜你喜欢
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 2015-04-30
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2014-05-20
    相关资源
    最近更新 更多