Thread: Reading values from a file and storing them in an array

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    5

    Reading values from a file and storing them in an array

    Code:
     #include<stdio.h>
    int main()
    {
    int i,j,k,l;
        char in;
        FILE*fp;
        float array[500];
        fp=fopen("input.txt","r");
         if(fp==NULL)
         {
         printf("Error: can't open file.\n");
         }
          else
          {
          printf("File opened successfully.\n");
          }
           do
            {
                in = getc(input);
            printf("%c",in);
           }
           while(in != EOF);
           for(i=0;i<500;i++)
           {
           }
           float max=array[0],min=array[0];
           if (array[i] > max)
            {
              max = array[i];
            }
          else if (array[i] < min)
            {
              min = array[i];
            }
    
      printf ("Maximum element in an array : %d\n", max);
    }
    /* I need help storing the 100 values into an array and finding the maximum value from the array*/
    [/Code]
    Last edited by vunwe; 04-24-2012 at 09:19 AM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by vunwe View Post
    I have writen a code which reads 100 floats numbers from a (100*1)matrix in a data.txt.
    No you haven't.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    It does read. 100 values form data.txt,my problem is assigning them into an array

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Your code does not even compile for obvious reasons. Fix it and FIX YOUR INDENTATION, as a first step.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    ive fixed it
    Last edited by vunwe; 04-24-2012 at 09:20 AM.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, this is getting somewhere.

    A few things to fix:

    1) Indentation is still not perfect and could be improved.

    2) main() returns 0 at the end and should really be int main(void). This is not C++ and the absence of void does not mean no parameters.

    3) getc() returns an int not a char. Google getc() usage.

    4) What is in your input file? If you are just reading float numbers you can do so directly with fscanf(input,"%f",array[i]). No need to read character by character.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    hello

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    thanx for help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from File and Storing into Array
    By Magic staple in forum C Programming
    Replies: 13
    Last Post: 12-05-2011, 12:00 AM
  2. Replies: 12
    Last Post: 09-23-2010, 11:49 AM
  3. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  4. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 03:01 PM
  5. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 03:04 AM

Tags for this Thread