【问题标题】:C: read text file and manipulate dataC:读取文本文件并操作数据
【发布时间】:2016-09-10 11:16:39
【问题描述】:

我是 C 编程新手。我正在处理具有以下格式的文本文件

1 2009 詹姆斯史密斯 2 18

2 2010 年鲍勃·戴维斯 5 18

3 2010 艾伦汤姆森 15 26

4 2010 布拉德海耶 15 26

我想要做的是读取每一行并以有意义的方式打印到控制台

詹姆斯·史密斯于 2009 年首次亮相,年收入 200 万。他一生赚了1800万,平均每年xyzM

然后

最高收入者:?
平均收入:?
玩家总数:

这是我目前所拥有的:

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

typedef char *string;

int main() {

    int i = 0, line = 5;
    char ch[100];
    string array[4];

    FILE *myfile;
    myfile = fopen("C:/Data/Players.txt","r");
    if (myfile== NULL)
    {
        printf("File Does Not Exist \n");
        return 1;
    }

    while(line--)
    {
        fscanf(myfile,"%s",&ch[i]);
        printf("\n%s", &ch[i]);
        array[0] = array[i];
        i++;

        if(i=5)
        {
            printf("Yes");
        }


    }

    fclose(myfile);

    return 0;
}

【问题讨论】:

  • 几乎所有的初学者书籍或教程都会通过你了解scanf

标签: c arrays dynamic-programming


【解决方案1】:

您应该逐行读取文件,解析字符串,直到遇到文件结尾 (EOF)。读过之后才知道遇到过。

这是我的解决方案:

#include <stdio.h>

int main()
{
    FILE *file;
    int line_number;
    int year;
    char fname[10];
    char lname[10];
    int m_a_year;
    int m_over_lifetime;

file = fopen("textfile.txt", "r");

while(!feof(file))
{
    fscanf(file, "%d %d %s %s %d %d", &line_number, &year, fname, lname, &m_a_year, &m_over_lifetime);
    printf("%s %s made his debut in %d and earns %dm a year. He has earned %dm over his lifetime with an average of %dM a year\n\n", fname, lname, year, m_a_year, m_over_lifetime, (m_over_lifetime/(2016-year)));
}

fclose(file);

    return 0;
}

输出:

james smith made his debut in 2009 and earns 2m a year. He has earned 18m over h
is lifetime with an average of 2M a year

bob davies made his debut in 2010 and earns 5m a year. He has earned 18m over hi
s lifetime with an average of 3M a year

Allan Thomson made his debut in 2010 and earns 15m a year. He has earned 26m ove
r his lifetime with an average of 4M a year

Brad Haye made his debut in 2010 and earns 15m a year. He has earned 26m over hi
s lifetime with an average of 4M a year

【讨论】:

  • feof(file) 仅在您读取 EOF 字节(位于文件末尾)后才为真。 @BLUEPIXY
  • 这个想法是将循环重写为while( fscanf(...) == 6 ) { printf(...); }。这样做是在检查 EOF 并读取所有字段。
【解决方案2】:

你可以让它变得更简单。首先,您不需要变量“Line”,当c读取文件时,他可以判断他是否到达文件末尾,而且我们在c中不使用字符串类型,我们使用char数组,但是在您的代码中字符串数组没用,只是占用空间,至于上面的 cmets 是正确的,可以让您了解如何读取文件

【讨论】:

  • 欢迎来到 stackOverflow。虽然您在此处的建议可能是正确的,但请考虑添加您认为可行的代码。这样,它可能会帮助未来的读者。还包括您更改 OP 代码的动机。
【解决方案3】:

请试试这段代码,它几乎可以满足您的所有需求。

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

int main(void) {
    int status;
    int number;
    FILE *fp;
    if ((fp = fopen("C:/Data/Players.txt", "r+")) == NULL) {
        printf("No such file\n");
        exit(1);
    }
    int x, y;
    char firstname[10];
    char lastname[20];
     while (fscanf(fp, "%d %d %s %s %d %d", &number, &x, firstname, lastname, &y, &status) == 6) {
        printf("%s %s made his debut in %d and earns %dm a year. He has earned %dm over his lifetime with an average of %dM a year\n\n", firstname, lastname, x, y, status, (status/(2016-x)));
    }
    return 0;
}

输出

james smith made his debut in 2009 and earns 2m a year. He has earned 18m over his lifetime with an average of 2M a year

bob davies made his debut in 2010 and earns 5m a year. He has earned 18m over his lifetime with an average of 3M a year

Allan Thomson made his debut in 2010 and earns 15m a year. He has earned 26m over his lifetime with an average of 4M a year

Brad Haye made his debut in 2010 and earns 15m a year. He has earned 26m over his lifetime with an average of 4M a year

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-03
    • 2013-09-25
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 2023-03-06
    相关资源
    最近更新 更多