filling an array with a simple file

This is a discussion on filling an array with a simple file within the C Programming forums, part of the General Programming Boards category; Hello I have a file with these contents: 7.25 8.00 10.00 11.00 12.00 15.75 25.00 40.00 85.00 125.00 How can ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    48

    filling an array with a simple file

    Hello

    I have a file with these contents:

    7.25
    8.00
    10.00
    11.00
    12.00
    15.75
    25.00
    40.00
    85.00
    125.00

    How can I put them in an array of 10?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    You want to store them in an integral array?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    An array of 10 doubles, perhaps?

    arya6000, you might want to read:
    Lesson 8: Arrays

    Rather unfortunately, it appears that the tutorial on C file I/O is not correctly linked, or non-existent. Do you know how to read from a file?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    Quote Originally Posted by laserlight View Post
    An array of 10 doubles, perhaps?

    arya6000, you might want to read:
    Lesson 8: Arrays

    Rather unfortunately, it appears that the tutorial on C file I/O is not correctly linked, or non-existent. Do you know how to read from a file?

    I know how to open and read from the file, but I don't understand how I can store each line in a different array.

    say if I have an array called:

    double rates[10];

    not sure how to put each line in the array.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    Use a loop to read the floating point number on each line into an element in the array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
    	
            while ( fscanf( fpointer, "%lf", &floatArray[i]) > 0 )
    	{
    		printf( "%.2f\n", floatArray[i]);
    		++i;
    	}
    Or you could use fgets to read from file and sscanf to extract the value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21