8 Replies - 237 Views - Last Post: 10 October 2012 - 05:45 AM Rate Topic: -----

#1 esmile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

Scanning in text file 1st line is # of words

Posted 09 October 2012 - 11:33 PM

Hey everyone! For this HW I need to compare permutations to a dictionary file with binary search. I got the permutations down and I stored all the different permutations in an array. Now for binary search I need to know the size of text document, the text document that is given has the 1st line as the number of words so example:

10
cool
hi
test
sweet
coolio
cat
dog
town
car
ear

My only question is how do I get the loop to store the # as the size of the array of dictionary words? Right now I just have it reading in like this:

for(i=0; i<filelength; i++)
{
fscanf(ifp, "%s", words[i]);
i++;
}

It goes till the file ends, for this HW it specifically states we must scan the int in as the array size.

Is This A Good Question/Topic? 0
  • +

Replies To: Scanning in text file 1st line is # of words

#2 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Scanning in text file 1st line is # of words

Posted 09 October 2012 - 11:39 PM

Something like
fscanf(ifp, "%d", &numWords);
for ( i = 0 ; i < numWords ; i++ ) {
    fscanf(ifp, "%s", words[i]);
}


Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,566
  • Joined: 25-December 09

Re: Scanning in text file 1st line is # of words

Posted 09 October 2012 - 11:39 PM

After you retrieve the int then you should use malloc/free combination to create the array to hold the data.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 esmile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

Re: Scanning in text file 1st line is # of words

Posted 09 October 2012 - 11:41 PM

View Postjimblumberg, on 09 October 2012 - 11:39 PM, said:

After you retrieve the int then you should use malloc/free combination to create the array to hold the data.

Jim




Haha that's actually exactly what she wanted to do. I need to read up on malloc more. She said we need to allocate space for the dictionary array. This was weird because every other assignment I just used the simple for loop to scan in all the words. Could you give me any references that would better explain how to use malloc in this situation?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,566
  • Joined: 25-December 09

Re: Scanning in text file 1st line is # of words

Posted 09 October 2012 - 11:48 PM

Try: Malloc.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 esmile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

Re: Scanning in text file 1st line is # of words

Posted 09 October 2012 - 11:51 PM

View Postjimblumberg, on 09 October 2012 - 11:48 PM, said:

Try: Malloc.

Jim



Hey Jim! thanks for all the help. I actually attempted to use Malloc like this:

char *words = malloc(numWords*sizeof(char));


but it crashed my program lol
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,566
  • Joined: 25-December 09

Re: Scanning in text file 1st line is # of words

Posted 09 October 2012 - 11:54 PM

Show your code.

Also since this is a character you don't need the multiplication:
char *words = malloc(numWords);


And don't forget to include <stdlib.h>


Jim
Was This Post Helpful? 0
  • +
  • -

#8 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Scanning in text file 1st line is # of words

Posted 10 October 2012 - 12:58 AM

> char *words = malloc(numWords*sizeof(char));
But this is only sufficient for a single word up to however many characters numWords is.

If you really want an array of words, you need something like

char (*words)[100] = malloc( numWords * sizeof(*words) );

Which is the malloc'ed equivalent of saying
char words[numWords][100];
Was This Post Helpful? 0
  • +
  • -

#9 esmile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-October 12

Re: Scanning in text file 1st line is # of words

Posted 10 October 2012 - 05:45 AM

View PostSalem_c, on 10 October 2012 - 12:58 AM, said:

> char *words = malloc(numWords*sizeof(char));
But this is only sufficient for a single word up to however many characters numWords is.

If you really want an array of words, you need something like

char (*words)[100] = malloc( numWords * sizeof(*words) );

Which is the malloc'ed equivalent of saying
char words[numWords][100];



This helped me so much,so the pointer in (*words) pointed to the (*words). You where right I was just allocating memory for the one word the size of the int. thats what was causing the error. I would post my whole code here but sadly that is not aloud and I would get an F for this assignment.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1