how to read integers from a text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rohit

    how to read integers from a text file

    Hi All,
    I am new to C language.I want to read integers from a text file and
    want to do some operation in the main program.To be more specific I
    need to multiply each of these integers with another set of integers
    stored in an array.It would be a great help if you could provide some
    code for it.I tried the function fscanf but by that I am able to read
    only the first integer of the text file.Please help me.
  • nembo kid

    #2
    Re: how to read integers from a text file

    rohit ha scritto:
    I tried the function fscanf but by that I am able to read
    only the first integer of the text file.
    Place each integer in a line and popolate an array v[] with these
    values. Could be as follows:

    /* ... */

    #define DIM 100 /* max 100 integers */

    int v[DIM];

    /* 'n' is the number of integers (lines) max DIM values */
    /* It is specified in the first line */
    /* fpin is the pointer to FILE */

    for (j=0;j<n;j++)
    {
    if(fscanf(fpin, "%d\n", &v[j])!=1)
    {
    fprintf(stderr, "\nData corrupted");
    return 1;
    }
    }

    /* ... */

    Comment

    • Jens Thoms Toerring

      #3
      Re: how to read integers from a text file

      nembo kid <nembo@kidwrote :
      rohit ha scritto:
      I tried the function fscanf but by that I am able to read
      only the first integer of the text file.
      Place each integer in a line and popolate an array v[] with these
      For fscanf() to work they don't have to be on separate lines.
      fscanf() skips all white-space characters, i.e. spaces, tabs
      and line end characters (or character combinations) etc.
      values. Could be as follows:
      /* ... */
      #define DIM 100 /* max 100 integers */
      int v[DIM];
      /* 'n' is the number of integers (lines) max DIM values */
      /* It is specified in the first line */
      /* fpin is the pointer to FILE */
      for (j=0;j<n;j++)
      {
      if(fscanf(fpin, "%d\n", &v[j])!=1)
      {
      fprintf(stderr, "\nData corrupted");
      return 1;
      }
      }
      Or, if you don't know how many integers there are in the file
      (but also assuming that you know an upper limit 'DIM'):

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

      (...)

      int count = 0;

      (...)

      while ( count < DIM && fscanf( fpin, "%d", v + count ) == 1 )
      count++;

      if ( ferror( fpin ) )
      {
      fprintf( stderr, "Read failure\n" );
      exit( EXIT_FAILURE );
      }

      But it would be much better if you would show what you did
      try yourself. In that case you would get an explanation of
      why it didn't work and how to improve it. And that, in the
      long run, will help you much more than just blindly using
      other peoples code...
      Regards, Jens
      --
      \ Jens Thoms Toerring ___ jt@toerring.de
      \______________ ____________ http://toerring.de

      Comment

      • Kenny McCormack

        #4
        Re: how to read integers from a text file

        In article <3daa36ae-1163-426f-a8a5-275435ffedcd@x1 9g2000prg.googl egroups.com>,
        rohit <rohit.kgpiit@g mail.comwrote:
        >Hi All,
        >I am new to C language.I want to read integers from a text file and
        >want to do some operation in the main program.To be more specific I
        >need to multiply each of these integers with another set of integers
        >stored in an array.It would be a great help if you could provide some
        >code for it.I tried the function fscanf but by that I am able to read
        >only the first integer of the text file.Please help me.
        <OT>I think you'll find this a lot easier to do it in AWK, than
        bothering with C.

        It sounds to me like what you are doing is a perfect fit for AWK.

        Comment

        • Barry Schwarz

          #5
          Re: how to read integers from a text file

          On Sun, 1 Jun 2008 03:56:06 -0700 (PDT), rohit
          <rohit.kgpiit@g mail.comwrote:
          >Hi All,
          >I am new to C language.I want to read integers from a text file and
          >want to do some operation in the main program.To be more specific I
          >need to multiply each of these integers with another set of integers
          >stored in an array.It would be a great help if you could provide some
          >code for it.I tried the function fscanf but by that I am able to read
          >only the first integer of the text file.Please help me.

          Remove del for email

          Comment

          • santosh

            #6
            Re: how to read integers from a text file

            rohit wrote:
            Hi All,
            I am new to C language.I want to read integers from a text file and
            want to do some operation in the main program.To be more specific I
            need to multiply each of these integers with another set of integers
            stored in an array.It would be a great help if you could provide some
            code for it.I tried the function fscanf but by that I am able to read
            only the first integer of the text file.Please help me.
            Since the integer values are represented in text you need to, in
            essence, read each value as a sequence of characters and then convert
            the sequence to the value it represents, before storing away your
            value. You will need to do this for every integer represented in your
            file.

            The fscanf function will do the reading and the conversion in one call
            for you, but can be a bit difficult to use. In particular the sequence
            of characters that fscanf reads from the file must match what it is
            expecting from it's format string. The function can fail if it
            encounters unexpected characters (like say an alphabet). You can (and
            should) check the return value of fscanf after every call to it, before
            proceeding.

            Another method is to read in a line of text (one or more characters
            terminated by a '\n' character) with the fgets function and then supply
            the line (or a part of that line if multiple numbers are represented in
            each line) to a conversion function like strtol. If you use atoi be
            aware that it produces undefined behaviour with certain values and does
            not indicate failure. The functions strtol and strtoul are really much
            more robust.

            Also you could read the file character by character with a function like
            getc and do the conversion to a int value yourself, but it's usually
            better to use standardised functions unless you're learning or have
            special requirements.

            Please provide us with the code for your attempted solution and an exact
            description of the format of your text file to help you further.

            Comment

            • Barry Schwarz

              #7
              Re: how to read integers from a text file

              On Sun, 1 Jun 2008 03:56:06 -0700 (PDT), rohit
              <rohit.kgpiit@g mail.comwrote:
              >Hi All,
              >I am new to C language.I want to read integers from a text file and
              >want to do some operation in the main program.To be more specific I
              >need to multiply each of these integers with another set of integers
              >stored in an array.It would be a great help if you could provide some
              >code for it.I tried the function fscanf but by that I am able to read
              >only the first integer of the text file.Please help me.
              Show your code.


              Remove del for email

              Comment

              Working...