Reading a file into an array for fast access later

This is a discussion on Reading a file into an array for fast access later within the C Programming forums, part of the General Programming Boards category; I am fairly new to C and would like to read a file into an array so that I can ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    Reading a file into an array for fast access later

    I am fairly new to C and would like to read a file into an array so that I can access the data quickly. I think that I should be using pointers (or an array of pointers) to make effecient code. I get the basic premise of pointers (variables that hold the address of another variable in memory) but have obviously missed something when it comes to working with char (string), pointers and or arrays.

    To make this easy for myself I have created a text file to input as shown below:

    One
    Two
    ...
    ...
    ....
    Ten

    I am using fgets to get the line and then trying to save the information in the buffer to an address in an array of pointers.

    I have included the code for you to ridicule but would appreciate someone letting me know what I am missing.

    Thanks in advance

    Code:
    vuser_init()
    
    
    
    {
    
    
    	long file;//used for file 
    	char *filename="c:\\arraytest.txt";//text file containing 10 lines of text (ONE - TEN)
    	int counter=0;//loop counter
    	char inputline[100];//line in text file
    	char *aop[10];//array of pointers (10)
    
    	file=fopen(filename,"r");//assuming its there for this test
    	
    	while(!feof(file)){//while not end of file
    		fget(inputline,100,file);//get text line
    		aop[counter]=inputline;//assign the value in inputline to the array - I think it starts to go wrong at this point!
    		counter++;				
    	}
    	
    	/*
    	
    	I think that the problem is that the pointer holds the address to inputline and not the value.
    	So my output from each element in the array is always the contents of the last line retrieved by the fget.
    	If I try to use *aop[counter]=inputline I get an illegal types error.
    
    	*/
    
    
    	return 0;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    You're right. inputline is a constant value throughout the program (the address of the first element of the array). So you're just setting each pointer in the aop array to the same value.

    I would suggest changing your loop to:
    Code:
    	while(!feof(file)){//while not end of file
    		fget(inputline,100,file);//get text line
                                    aop[counter] = malloc(strlen(inputline) + 1);
    		strcpy(aop[counter], inputline);//assign the value
    		counter++;				
    	}
    Don't forget to free the memory when you're done!
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > while(!feof(file))
    Shame on you itsme86, I would have figured you would have read the FAQ on this one by now.

    while ( fget(inputline,100,file) != NULL )

    Or a bit better
    while ( fget(inputline,sizeof inputline,file) != NULL )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Sorry, I was just concentrating on the OP's question. I got distracted by work (at work no less, the nerve!) or I would have pointed out the feof() as well
    If you understand what you're doing, you're not learning anything.

  5. #5
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by matsharp
    I
    Code:
    	long file;//used for file 
                        .
                        .
                        .
    	file=fopen(filename,"r");//assuming its there for this test
    	
    }

    Perhaps it is a minor nit, but the type of the fopen return value is not long, but FILE *.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    Thanks, its sorted

    All,

    Thanks for your help. Although the strcpy didn't work, it did help me understand where I was going wrong. As the code is inside the Mercury LoadRunner product I used the lr_eval_string command to assign the contents of the string to the pointer array.

    I also managed to get my head around array of pointers. Thanks again.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You do know you actually have to have some space allocated to copy into, right? You can't just strcpy into any old pointer and expect it to work right.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    > while(!feof(file))
    Shame on you itsme86, I would have figured you would have read the FAQ on this one by now.

    while ( fget(inputline,100,file) != NULL )

    Or a bit better
    while ( fget(inputline,sizeof inputline,file) != NULL )
    I'd have thought you would have noticed that the OP typed fget, not fgets . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sometimes when you're being pedantic, you leave something for the other Old Timers to catch, so they have something to do. It's a professional courtesy.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    Quzah - Remember I am new to this....

    ....so how do I make the space available for the strcpy to work? The lr_eval_string command appears to be working fine but if there is a something that I have still misunderstood then I need to get my head around it. Go on help the newbie...

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Go on, help yourself: Visit malloc and free at your local man page (work safe!)


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. Replies: 9
    Last Post: 12-08-2008, 09:27 AM
  4. Reading a list of ints from file into an array
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-10-2008, 05:45 AM
  5. Reading in an array of text from a file?
    By suzakugaiden in forum C++ Programming
    Replies: 6
    Last Post: 01-04-2006, 02:17 PM

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