reading a file and writing it into an array in c

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lilkal
    New Member
    • Sep 2008
    • 1

    #1

    reading a file and writing it into an array in c

    hiii everyone..
    im new to the concept of files in c
    i need to open a file which contains something like

    push a
    push b
    mul
    pop c

    things to do:
    1.open the file
    2.read each line in the file and place it in an array named instructions
    3.parse each line and place constants(a,b,c etc) in another array named constants
    4.close the file when the file ends.

    if u can help me out how to do this i will be really grateful...
    thank u guyzz...
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    FILE* f = fopen(...);
    Then in loop do fgets() for each line ( until eof is reached ) and for each read line allocate place in storage and copy it there.
    In the same loop, for each string loop until the first space ( check optionally if the first part is valid keyword ) and consider the rest a constant, convert to number and store.
    After all the stuff do fclose(f);

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      This is a Homework Question and, as such, is not an acceptable question to post on this site (Please refer to Posting Homework or Coursework Questions and Answers).

      This thread is now locked to further posting.

      Having now been officially warned, further posts of this nature will lead (progressively) to a temporary ban and then a full (permanent) ban from the site.

      Administrator.

      Comment

      Working...