Thread: having some trouble with making a simple function,help please

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > the fscanf function gives a value of 30 to "number_of_words"

    You mean this?
    > fscanf(text,"%i",*number_of_words);

    No, it doesn't.

    The one I posted in #6 does however
    fscanf(text,"%i",number_of_words);
    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.

  2. #17
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    okay then can you recomend a code that would do what ive been tryna do

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > the text file is simply a list of 30 ,5 letter words with number 30 at the top.
    Say for example

    30\n
    hello\n
    world\n


    So here are your first couple of fscanf calls.
    Code:
        fscanf(text,"%i",number_of_words);
        for (i=0;i<*number_of_words;i++)
        {
           for(j = 0; j <6 ; j++)
           {
               fscanf(text,"%c",words[i][j]);
    The first call with %i will consume 30
    The first 6 calls with %c will consume \nhello
    The next 6 calls with %c will consume \nworld

    On the whole, it might be better if you just did this, which will skip over all the white-space in the file.
    Code:
        fscanf(text,"%i",number_of_words);
        for (i=0;i<*number_of_words;i++)
            fscanf(text,"%s",words[i]);

    Now recall post #9
    But given that you're using %c to read the file, you need to understand that file format is critical. If you're out by even 1 character, then the program just won't work as expected.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  2. Trouble making a font name list
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 11-10-2006, 04:20 PM
  3. Trouble making an Array equal a Function
    By Ozor in forum C++ Programming
    Replies: 16
    Last Post: 07-11-2003, 10:33 AM
  4. Replies: 2
    Last Post: 01-13-2003, 01:28 PM

Tags for this Thread