Thread: Reading int's from file to array HELP plz

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Lisbon
    Posts
    2

    Reading int's from file to array HELP plz

    Hey there guys.
    Don't worry, i won't ask you for making my homework, lol..
    My question is simple. I want to read a file, with integers inside, and pass those values to an array of int's.
    The question is, the program has to work with an undefined number of values, like, i can't initialize an array like this: int array[10].
    I'm going to put my code here so you can understand:

    Code:
    void main(){
    
    	int line_temp;
    	int aux;
    	int c;
            int number;
    	FILE * fp;
            int array[];
    	char name[20];
    	printf("File name:\n");
    	scanf("%s",name);
    	fp = fopen(name,"r");
    	c = getc(fp);
    	while (c != EOF){
    
    	fscanf(fp,"%s",linha_temp);
            sscanf(linha_temp,"%d",&number);
    	
    	}
    }
    Question 1: is the syntax "int array[];" correct? it has a undefined size, right?
    Question 2: how do i avoid creating N int's for the storage of the file numbers? (if there are 500 numbers, i guess it's stupid to initialize 500 ints)


    I guess this is only saving the first number on the file..
    I need some help please, i appreciate it in advance
    Thank you, peace

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by GARiMTO View Post
    Question 1: is the syntax "int array[];" correct? it has a undefined size, right?
    Where do you propose an undefined array be stored? How much space should be allocated for it in computer memory? How would it be resized should there not be enough space to contain as many elements as you want to give it?

    OK, so an undefined size of array doesn't exactly exist. You have a few options, however. You can dynamically allocate memory and do some fancy swapping of data as you keep enlarging your dynamic block of memory. Otherwise, you can just guess and make an array as large as you need it to be, but if you underguess you can't complete the program and if you overguess you waste memory (or blow the stack).

    You could also leave off the array and use a linked list or more advanced data type. I suspect this might be overkill for your type of project, though.

    Quote Originally Posted by GARiMTO View Post
    Question 2: how do i avoid creating N int's for the storage of the file numbers? (if there are 500 numbers, i guess it's stupid to initialize 500 ints)
    500 ints isn't bad. 500 * sizeof(int) on most modern machines will be 2,000 bytes worth of data. Hardly something you should worry about on machines with 1 or 2 GBs worth of RAM and climbing. If you still wish to lower the overhead, then you'd have to explain exactly what you want to do with those ints. Overall, I think it would be best to read in all 500 at once if you need to operate on them. If you're just trying to read the average, let's say, of all 500, then don't bother storing them at all.

    On a side note, void main() is not standard C. There are numerous posts on this subject as well as a FAQ entry on it. Try int main(void) instead.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    while (c != EOF)

    and do you see the modification of c in the loop?

    your loop should look like
    Code:
    while (fgets(linha_temp, sizeof linha_temp, fp) != NULL)
    {
            sscanf(linha_temp,"%d",&number);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do proper indenting, please.
    You can also leave some blank new lines to separate your code into blocks and make it even more readable.
    And as explained, you need a dynamic array, which basically involved allocating a known size with malloc, keeping check of the size, and if you need more space later, allocate a bigger memory block and copy the old data over and freeing the old memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 12-08-2008, 10:27 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Reading a file into an array for fast access later
    By matsharp in forum C Programming
    Replies: 10
    Last Post: 08-03-2006, 02:42 AM
  4. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM