help on assignment (file i/o)

This is a discussion on help on assignment (file i/o) within the C Programming forums, part of the General Programming Boards category; Hey guys. I'm new here. I have an assignment to read from a file and emulate a computer (moving around ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    help on assignment (file i/o)

    Hey guys. I'm new here. I have an assignment to read from a file and emulate a computer (moving around imaginary registers, moving RAM around, etc.)

    I have two file pointers. one is in my main function and one is in a different function.

    here is the code up to the first error:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define INPUT_FILE "interpreter.txt"
    
        FILE *fp, *rp;
    int registers[10], ram[1000], freq[10], instruction, lines;
    
    void decMack0();
    void decMack1();
    void decMack2();
    void decMack3();
    void decMack4();
    void decMack5();
    void decMack6();
    void decMack7();
    void decMack8();
    void decMack9();
    void output();
    int s, a, d, n, l;
    
    int main()
    {
    fp = fopen(INPUT_FILE, "r");
        if (fp == NULL)
        {
         printf("Can't open %s\n", INPUT_FILE);
         getchar();
         exit(1);
        }
       fscanf(fp, "%d", &lines);
       printf("%d", fp);
       reader(); 
     		while(!feof(fp))
    here is the code for my second function:

    Code:
    reader()
    {
    int i=0;
    int f=0;
    while(!feof(rp)){
       rp = fopen(INPUT_FILE, "r");
       fscanf(rp, "%d", &f);
       f = ram[i];
    	i++;
    }
    fclose(rp);
    return;
    }
    for some reason, when I debug it, it doesn't seem to like the while loops. it won't even load the file once it sees that. when i print the pointers, fp and rp, it has a huge number (435557) something like that. can anyone see what I'm doing wrong? any help would be appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can never get a meaningful number from printing the value of a FILE* variable.

    You can't meaningfully check a file pointer (such as rp) for end-of-file if you have not yet opened it. Also, should you somehow get into that while loop, opening a file that is already open is a Bad Thing. (IOW: move that fopen outside the while loop.)

    Also, you should remember that assignment statements in C move from right to left (i.e., the value on the right side is assigned to the variable on the left), not the other way around.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    You need better indenting. See http://cpwiki.sf.net/Indentation and conform to the example.
    Code:
    reader()
    {
    This does not do what you think it does.
    You are returning nothing, but you declared reader to return int.
    So it should be:
    Code:
    void reader()
    {
    Always specify a return type for functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    While on the subject of good function declarations, you should also always declare the types of the parameters, and never leave the list empty. That is:
    Code:
    void f(); /* bad */
    void f(void); /* good */
    This is so the compiler can tell you if you call the function with incorrect arguments:
    Code:
    void f();
    void g(void);
    int main(void)
    {
      f(1, 2, 3); /* no diagnostic required! */
      g(1, 2, 3); /* diagnostic required */
    }
    The empty parameter list meaning "unspecified arguments" is leftover garbage from the pre-ANSI/ISO days of C, and was kept for compatibility purposes. New code should avoid it.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    16
    what do you want to do in reader() function , it is not clear
    first, you are opening the file which is already opend than you are taking integer value in local variable int f , and then updating this value with ram[i]; value after completing the loop you are returning no value to the main..i think it is looking like some dummy code

    and in the same section first you are using the' rp' value before assign it to any file ..so it is looking like a wild pointer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21