C:读取文本文件并操纵数据

时间:2016-09-10 11:16:40

标签: c arrays dynamic-programming

我是c编程的新手。我正在使用以下格式的文本文件

1 2009 james smith 2 18

2 2010年bob davies 5 18

3 2010 Allan Thomson 15 26

2010 2010 Brad Haye 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;
}

3 个答案:

答案 0 :(得分:0)

您应该逐行读取文件,解析字符串,直到遇到文件结尾(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

答案 1 :(得分:0)

请尝试使用此代码,它几乎可以满足您的所有需求。

#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

答案 2 :(得分:0)

你可以让它变得更简单。首先你不需要变量&#34; Line&#34;当c读取文件时他可以告诉他是否到达它的末尾,我们也不会在c中使用字符串类型,我们使用char数组,但在你的代码中,字符串数组没用,它只是占用空间,因为上面的注释是正确的,可以让你知道如何读取文件