-1

I am debugging my program using gdb, fgets(line, sizeof(line), fp2) reads nothing from a text file. so the program loops infinity ins side while(!feof(fp2)) and the EOF is never met i dont know why?

I'm putting part of the code for context,

here is the inputfile:

  COPY   START  1000
  FIRST  STL    RETADR
  CLOOP  JSUB   RDREC
         LDA    LENGTH
         COMP   ZERO
         JEQ    ENDFIL
  ZERO   WORD   0
  RETADR RESW   1
  LENGTH RESW   1
  BUFFER RESB   4096



         RSUB
         END    FIRST

here is the main program:

int main(int argc, char *argv[])
{

    FILE *fp, *fp2, *fphex;
    char line[1000] = "" ;


    if (argc != 2)
    {
        printf("Usage: %s filename\n", argv[0]);
        exit(EXIT_FAILURE);
    }


    if ((fp = fopen(argv[1], "r")) == NULL)
    {
        printf("Can't open %s\n", argv[1]);
        exit(EXIT_FAILURE);
    }

    fp2 = fopen("intermediate.asm", "w");
    fp2 = removecomment(fp,fp2);
    rewind(fp2);

    while (!feof(fp2))
    {
        fgets(line, sizeof(line), fp2);   /*this fgets reads only 4 bytes of empty spaces*/ 
        parse(line);

    }
    struct node *print = head;
    fphex = fopen("Hex_code", "w");

    while(print == NULL)
    {
        fprintf(fphex, "%s", print->instruction);
        print = print->next;
    }

    return(0);
}

EDIT:

While(!feof(File*pointer) was not the problem.

i was trying to read from a write only fopen file.

i resolved it by fclose(file) fopen("file","r") or as suggested by others w+ mode. I think closing and opening in read mode is safer.

topcat
  • 177
  • 3
  • 17
  • 2
    Take the [tour], read [Ask] and [MCVE]. Can you strip that code down to the smallest example that demonstrates the problem? – jwdonahue Mar 29 '18 at 03:03
  • What is actually in `line[i] ` at the point the conditional is evaluated? – jwdonahue Mar 29 '18 at 03:09
  • You don't seem to understand what an MCVE is. You just removed all of the relevant code. What asked you to do was strip out everything that is irrelevant until you have a working example that demonstrates the problem without all that extra code. Usually, the act of doing that, helps you find the problem on your own. – jwdonahue Mar 29 '18 at 03:16
  • @jwdonahue is reads character by character from the fgets(line, sizeof(line), fp2) to skip spaces and tabs and only read instructions or operands.i removed the rest of the code its too vague. problem in inside feof loop. – topcat Mar 29 '18 at 03:16
  • Looking at it now ... – jwdonahue Mar 29 '18 at 03:17
  • We still need the content of the input file(s). – jwdonahue Mar 29 '18 at 03:18
  • See https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong. – jwdonahue Mar 29 '18 at 03:23
  • @jwdonahue i added it, i removed the rest of the program because it seemed unnecessary. – topcat Mar 29 '18 at 03:23
  • 1
    Possible duplicate of [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – jwdonahue Mar 29 '18 at 03:24
  • 1
    You are calling `fgets` and `feof` on an output stream, what did you expect to happen – M.M Mar 29 '18 at 03:25
  • 1
    Ya, I was tracking on that with the original post, then all the code changed in front of me. I think the OP has their guzzintas crossed over with their cumzoutas. But while(!feof()) is just wrong anyway. – jwdonahue Mar 29 '18 at 03:26
  • @jwdonahue ok then what should i do instead of feof , what will happen when it reads past the file? – topcat Mar 29 '18 at 03:43
  • You should read the Q&A that I referenced earlier. Have you tried fixing the confusion over inputs and outputs yet? – jwdonahue Mar 29 '18 at 03:48
  • 1
    @topcat you cannot read from a file opened with mode `"w"` – M.M Mar 29 '18 at 03:50
  • Avoid reading past the end of the INPUT file by having the call to `fgets()` be inside the `while()` condition – user3629249 Mar 29 '18 at 04:47

3 Answers3

3

Ok, here is the problem, you have "w" as a file opening mode.

fp2 = fopen("intermediate.asm", "w");

it should be

fp2 = fopen("intermediate.asm", "r");

file opening modes are

  1. w - write (file is deleted if exists)
  2. r - read (file must exist)
  3. a - append

than you have + sign which means:

  1. w+ - write and read (overwrite if file exists)
  2. r+ - read and write (file must exist)
  3. a+ - append and read (create file if it does not exist)
-1

fp2 was opened in write mode "w", So it must be closed then opened in read mode "r" so lines could be read properly, people could have spotted that instead of saying its the While(!feof(fp2)).

topcat
  • 177
  • 3
  • 17
-2

I believe this is well addressed here, it will solve if you replace while(!feof(fp2)) ---> while(!feof(fp2) && !ferror(fp2))

ntshetty
  • 1,293
  • 9
  • 20