?
Solved

Reading lines of integers from a file

Posted on 2003-03-11
8
Medium Priority
?
236 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:atariq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8117150
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...
0
 
LVL 6

Expert Comment

by:gj62
ID: 8117156
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.  
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 8117371
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:atariq
ID: 8117489
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()?
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8117539

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!
0
 

Author Comment

by:atariq
ID: 8117548
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()?
0
 
LVL 8

Accepted Solution

by:
akshayxx earned 80 total points
ID: 8117583
since u have decided that u'll have one number per line

here is how u can fill up ur array

int num,temp;
int *data=NULL;
FILE *fp;
fp=fopen("numbers.txt","r");
check if file opened successfully
num=0;
while(EOF!=fscanf(fp,"%d",&temp)){
num++;
data=(int*)realloc(data,num*sizeof(int));
data[num-1]=temp;
}

now u can access all the numbers  ( total numbers read = value of 'num')

for(i=0;i<num;i++) printf("%d",data[i]);
number on nth line will be data[n-1]  
where n starts from 1st,2nd...

NOTE::realloc is used very heavily here .. but for small program it wont matter much
0
 

Author Comment

by:atariq
ID: 8117882
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question