Reading Integer from TXT File

Discussion in 'C' started by DarkChewby, Sep 30, 2010.

  1. DarkChewby

    DarkChewby New Member

    Joined:
    Sep 30, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    I am really new to writing C code. I am generally written Java code all my life, so I am struggling with C code as this is the second program I am writing in C.

    I am trying to read a txt file that has numbers in the format of

    12
    145
    156
    1263
    1342
    1642

    I am trying to read these in and storing them into my array. I tried two different ways, but it's not working. The loop just runs forever and prints 0s and random values.


    while((fscanf(file, "%d\n", &allInstructs[count]) != EOF))
    {
    printf("Running %d\n", allInstructs[count]);
    count++;
    }
    Thanks again!
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    did u try using fgets()?? it is much better than fscanf
     
  3. lp1938

    lp1938 New Member

    Joined:
    Jan 21, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Retired
    Location:
    Chiang Mai Thailand and Melbourne Australia
    The code you have shown looks file to me. Make sure that you initialise count to zero before you start the loop.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice