Thread: reading strings from .txt and entering in an array

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It looks fine to me.

    Also, I created a file with 500000 "words" [a, b, c .. z, aa, ab, ac... ba, bb, bc,... zz, etc]

    the last ten are
    Code:
    qqkba
    rqkba
    sqkba
    tqkba
    uqkba
    vqkba
    wqkba
    xqkba
    yqkba
    zqkba
    I then instrumented the code to measure the time it takes to read the words in, and it toook about 0.05 seconds the first time round, and 0.16 s the second time round. So although I agree that it's inefficient to read the file twice, it's not as bad as it may seem.

    This of course assumes you're not reading the data from a network, slow CDROM or some such - in that case, I'd recommend copying the file to some local storage first.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    15
    Quote Originally Posted by Elysia View Post
    Haha, must be getting tired. Missed that part.
    You're right obviously. You outsmarted your teacher
    However... scanf("%s", ...) is unsafe. There's still one thing to fix
    And you can still do dynamic memory allocation (though it's a bit more advanced). So there's plenty more work to do if you want it.
    Thanks. I appreciate all of the advice. This was only the first part of the assignment, the real part is to set all this into a binary tree (I am using an AVL tree) and a hash table with options for the user to spell check and or delete words from a supplies .txt. Once I can finish the more difficult parts, if I still have time I will come back and fix the allocation and scanf.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So yes, it's an optional optimization the OP may do.
    Also noticed fgets(wordString, 30, file) <--- it's better to do fgets(wordString, sizeof(wordString), file).
    I don't know if I can think of anything else.

  4. #19
    Registered User
    Join Date
    Sep 2007
    Posts
    15
    Quote Originally Posted by Elysia View Post
    So yes, it's an optional optimization the OP may do.
    Also noticed fgets(wordString, 30, file) <--- it's better to do fgets(wordString, sizeof(wordString), file).
    I don't know if I can think of anything else.
    Ah! good idea!

    Updated that as well.

    Edit: hey I may have been a pain in the ass but at least I didn't post my assignment and ask someone to finish it all for me, yah? haha. Thansk again Mats and Elysia

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not a pain at all.

    I expanded my test-input to 5M words, to make the time a bit more measurable. Using realloc and reading the file once takes 1.69s.

    Reading the file twice, takes 0.61 + 1.67s. So there's a little bit of time to be gained by not reading it twice - but I think it's too small a gain to really worry about it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. entering numbers to array in a row whithout length input
    By transgalactic2 in forum C Programming
    Replies: 55
    Last Post: 01-02-2009, 04:02 AM
  2. Entering data into array of char pointers
    By richdb in forum C Programming
    Replies: 13
    Last Post: 04-09-2006, 07:30 AM
  3. Entering strings into a list?
    By chadsxe in forum C++ Programming
    Replies: 10
    Last Post: 06-02-2005, 12:40 PM
  4. Entering strings with spaces
    By spudtheimpaler in forum C Programming
    Replies: 6
    Last Post: 12-08-2003, 01:23 PM
  5. entering into 2d array
    By gammacad in forum C Programming
    Replies: 7
    Last Post: 06-09-2002, 03:21 PM