Link to home
Start Free TrialLog in
Avatar of atariq
atariq

asked on

Reading lines of integers from a file

I'm trying to read lines of integers from a file in which there is one integer per line, after which I should be able to read a specific integer by calling its respective line number. I figured I would be able to read the sizeOf the file and mallocate some memory for an array that would store the integers from the file, and that I could use the index of the array to represent the line numbers of the file. While I'm sure this would work, I was wondering if there is another, simpler, way of doing this without the use of an array. Thanks in advance.
Avatar of gj62
gj62

Not quite sure I understand exactly what you are getting at, but you can resize the array as you go along using realloc()

for example:

int *arr=0;
  /* initial size of 100 ints */
arr = (int*)realloc(NULL,sizeof(int)*100);

/* oops, need 100 more ints */
arr = (int*)realloc(arr,sizeof(int)*200);

realloc() might move the memory, but it will keep the contents.  In other words, the arr address may be different, but the contents will be retained.

Hope this helps...
Now that I reread, I think you are asking if an array (whether dynamic or static) is the best thing to use.  Depending upon the size, it probably is the simplest and most straightforward.  
Instead of allocating a a memory buffer, try to index the file itself. Since you know the sizeof an integer it wont' be a problem. Try to store the integers in binary format, if possible. Because with binary data, deriving a formula wont' be a problem. I suggested to use the file itself because if the number of integer increases the memory constraint will not be a problem.

Give further comments.
Avatar of atariq

ASKER

I appreciate the quick responses.
I'll try to make myself clearer:
say I have a file with the integers 20, 3, 51, 13 with each one on a separate line. I want to be able to read the integer in, say, the 3rd line (which in this case would be 51) and change it if I wish. Now, I think allocating a memory buffer for an array that would store these numbers (read in using fscanf) is a good idea (with help from q162). The realloc() was actually helpful, thanks. But in your code, you say,
   int *arr=0;
   /* initial size of 100 ints */
   arr = (int*)realloc(NULL,sizeof(int)*100);
I don't see how int *arr represents an array and what's the role of the NULL here? Was this actually supposed to be a malloc()?

as long as u know the format of the file .. it doesnt matter if u have binary or text file .. u can always do the calculation stuff .. good thing about binary is that .. u dont have to do text-to-integer convertions.. u can directly read the binary data .. and with proper offset u can directly type-cast the data to appropriate data-type...

memmap the file and start playing with offsets and type-casts

Cheers!
Avatar of atariq

ASKER

I appreciate the quick responses.
I'll try to make myself clearer:
say I have a file with the integers 20, 3, 51, 13 with each one on a separate line. I want to be able to read the integer in, say, the 3rd line (which in this case would be 51) and change it if I wish. Now, I think allocating a memory buffer for an array that would store these numbers (read in using fscanf) is a good idea (with help from q162). The realloc() was actually helpful, thanks. But in your code, you say,
   int *arr=0;
   /* initial size of 100 ints */
   arr = (int*)realloc(NULL,sizeof(int)*100);
I don't see how int *arr represents an array and what's the role of the NULL here? Was this actually supposed to be a malloc()?
ASKER CERTIFIED SOLUTION
Avatar of akshayxx
akshayxx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of atariq

ASKER

Thanks very much for that... I had written almost exactly what you just wrote, without the addition of realloc(). Now, with it, it works just fine. Thanks everyone for your much appreciated help.