user3483718
user3483718

Reputation: 15

Reading integers from file in C

So I want to read a file containing integers. I can read the first number but how can I read all of them? String is easy but this is different. I also want to read them in a way I can then later add them together. I edited my code and I was able to output them all but my other question is how can I select each number alone so I can add whatever I want. For example, if I want to select the first column only and add it together.

Data:

54 250 19  
62 525 38  
71 123 6 
85 1322 86  
97 235 14

Code:

#include <stdio.h>
#include <conio.h>

int main()
{
    // pointer file
    FILE *pFile;
    char line[128];

    // opening name of file with mode
    pFile = fopen("Carpgm.txt","r");

    //checking if file is real and got right path
    if (pFile != NULL)
    {

        while (fgets(line, sizeof(line), pFile) != NULL)
        {
            int a, b, c;

            if (sscanf(line, "%d %d %d", &a, &b, &c) == 3)
            {
                /* Values read, do something with them */
                printf("%d %d %d\n",a, b, c);
            }
        }

        //using fgets to read with spaces 
        //fgets(line,81, pFile);

        //printing the array that got the pfile info

        //closing file
        fclose(pFile);        
    }
    else 
    {
        printf("Could not open file\n");     
    }

    getch();
    return 0;
}

Upvotes: 0

Views: 2343

Answers (4)

ravibhuva9955
ravibhuva9955

Reputation: 199

Read whole line using fscanf and then add all of them.

You can do it as below:

//using fscanf to read 
while(EOF != fscanf(pFile,"%d %d %d", &a, &b, &c))
{
   //Add all of them
   line = a+b+c;

   //printing the array that got the file info
   printf("%d",line);

   //So it will help to remove trailing "\n" and at the end it will help to come out of loop.
   if (EOF == fgetc(fp))
   {
      break;
   }
}

Upvotes: 0

Mahonri Moriancumer
Mahonri Moriancumer

Reputation: 6013

#include <stdio.h>
#include <conio.h>

int main()
   {
   FILE *pFile;

Instead of reading only one integer on the line, read all three at once.

// int line;
   int v1, v2, v3;

   // opening name of file with mode
   pFile = fopen("Carpgm.txt","r");
   if(NULL == pFile)
      {
      fprintf(stderr, "Could not open file\n");
      goto CLEANUP;
      }

Added a loop to read all lines.

   for(;;)
      {

Use 'elementsRead' to know when the 'End-Of-File' or 'EOF' has been reached.

      int elementsRead;
      //using fscanf to read

Now reading all three numbers (at the same time).

      elementsRead=fscanf(pFile,"%d %d %d", &v1, &v2, &v3);
      if(EOF == elementsRead)
         break;

      //printing the array that got the pfile info

Now printing all three numbers (at the same time).

      printf("%d %d %d\n", v1, v2, v3);
      }

CLEANUP:

   if(pFile)
      fclose(pFile);

   getch();

   return 0;
   }

Upvotes: 0

unwind
unwind

Reputation: 400139

The best way is probably to either read a single number at a time with fscanf(), and let it skip the whitespace (and newlines) as needed.

Or, you could read a whole line with fgets() and then parse/tokenize that line, I would use strtol() for that in this case since it makes it trivial to continue to the next nmumber. This approach will limit you to a maximum line length though, which the fscanf() approach will not.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409482

Read a complete line using fgets then "parse" the line using sscanf.

Something like

char line[128];
while (fgets(line, sizeof(line), pFile) != NULL)
{
    int a, b, c;

    if (sscanf(line, "%d %d %d", &a, &b, &c) == 3)
    {
        /* Values read, do something with them */
    }
}

Upvotes: 2

Related Questions