Question

I have this text:

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

I want to read the first 5 numbers from each line and then use them as inputs in a function.

I'm new to c and have only accomplished this code that doesn't do much, if anything really.

    int v,o;
    FILE *mydata;

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


    fclose(mydata);

How do i complete it?

Thank you.

Was it helpful?

Solution 2

Here is a small code to read character by character and store only the wanted numbers:

C Code:

#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);
}

And here is the output:

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

Now you can use your 2D array, for example if you want a variable a to have the 2nd line, 3rd number, you'd do : a = data[1][2] Don't forget arrays start at index 0

Hope this helps...

OTHER TIPS

Well, assuming your file is called "input.txt", then this is all you need to do:

#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;
}

With this input.txt file:

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

This gives us the following output:

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

That should be more than enough to get you started

It might help you:

#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;
}

The above program passes integer by integer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top