从文本文件中读取数字并将其用作输入

时间:2014-05-08 12:42:36

标签: c text numbers

我有这样的文字:

0 0 0 0 0 1
0 0 0 0 1 1
0 0 0 1 0 1
0 0 0 1 1 1

我想从每一行读取前5个数字,然后将它们用作函数中的输入。

我是c的新手,并且只完成了这段代码,如果有的话,这些代码并没有那么多。

    int v,o;
    FILE *mydata;

    if ((mydata = fopen("testinputs.txt", "rt"))==NULL)
    {
        printf ("file can't be opened'\n");
        exit(1);}


    fclose(mydata);

我如何完成它?

谢谢。

3 个答案:

答案 0 :(得分:1)

好吧,假设你的文件名为“input.txt”,那么这就是你需要做的:

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

#define LINE_LEN 100
int main ( void )
{
    char line[LINE_LEN];
    int sum, i, read_cnt, numbers[5];//sum and i are there for my example usage
    FILE *in = fopen("input.txt", "r");//open file
    if (in == NULL)
    {
        fprintf(stderr, "File could not be opened\n");
        exit( EXIT_FAILURE);
    }
    while((fgets(line, LINE_LEN, in)) != NULL)
    {//read the line
        //scan 5 numbers, sscanf returns the number of values it managed to extract
        read_cnt = sscanf(
            line,
            "%d %d %d %d %d",
            &numbers[0],
            &numbers[1],
            &numbers[2],
            &numbers[3],
            &numbers[4]
        );
        //check to see if we got all 5 ints
        if (read_cnt != 5)
            printf("Warning: only read %d numbers\n", read_cnt);//whoops
        //just an example, let's add them all up
        for (sum= i=0;i<read_cnt;++i)
            sum += numbers[i];
        printf("Sum of numbers was: %d\n", sum);
    }
    return EXIT_SUCCESS;
}

使用此input.txt文件:

1 2 3 4 5 
2 2 2 2 2
1 23 2 3 4
12 23

这给了我们以下输出:

Sum of numbers was: 15
Sum of numbers was: 10
Sum of numbers was: 33
Warning: only read 2 numbers
Sum of numbers was: 35

那应该更多,足以让你开始

答案 1 :(得分:0)

它可能会对您有所帮助:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
FILE *datafile;
int main()
{
    char line[100],*ch;
    int count;

    datafile = fopen ( "my.txt", "r");

    while ( fgets(line , sizeof line , datafile) != NULL )//fgets reads line by line
    {
        count = 0;
        ch = strtok ( line , " ");
        while ( count < 5 && ch != NULL )
        {


            printf("%d ",*ch - 48 );//*ch gives ascii value
            //pass to any function

            count++;
            ch = strtok ( NULL, " ");
        }

    }
    return 0;
}

上述程序按整数传递整数。

答案 2 :(得分:0)

这是一个逐个字符读取的小代码,只存储想要的数字:

C代码:

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

int main()
{
    int c;                     // Character read from the file
    int cpt;                    // Counter (to get only 5 numbers per line)
    int i,j;                     // Array indexes
    int data[4][5];       // 2D integer array to store the data
    FILE *f;                // File

    if ((f = fopen("file.txt", "r")) == NULL)       // Open the file in "read" mode
    {
        printf ("file can't be opened'\n");
        exit(255);
    }

    // Counter and indexes initialization
    cpt=0;
    i=0;
    j=0;

    // Read the file till the EOF (end of file)
    while ((c = fgetc(f)) != EOF)
    {
        // If 5 numbers read, go to new line, first index in the data array and to the next line in the file
        if(cpt==5)
        {
            i++;
            cpt=0;
            j=0;
            while(c != '\n' && c != EOF)
                c=fgetc(f);
        }

        // If a number is read, store it at the right place in the array
        if(c>='0'&&c<='9')
        {
               // Convert character to integer (see ascii table)
            data[i][j] = c-'0';
            j++;
            cpt++;
        }
    }

        // Display the array
    for(i=0;i<4;i++)
    {
        for(j=0;j<5;j++)
            printf("%d ", data[i][j]);
        printf("\n");
    }

    fclose(f);
}

这是输出:

0 0 0 0 0
0 0 0 0 1
0 0 0 1 0
0 0 0 1 1

现在你可以使用你的2D数组了,例如,如果你想让变量a有第二行,第三个数字,你可以这样做:a = data[1][2] 不要忘记数组从索引0开始

希望这会有所帮助......