0

The idea is to create a database for students' grades but I do not know what is wrong in this code

char bas;
char cadena[100];
FILE *arch, *pagC;
int c;
arch=fopen("alumnos.txt","r");
pagC=fopen("calificaciones.html","w");
/*html tags for a table*/
while(!feof(arch))         
{
    fprintf(pagC,"<TR>");
    while(c!='\n')                                  
    {
        fscanf(arch,"%[^:]%c",cadena,&bas);    
        fprintf(pagC,"<TD>%s</TD>",cadena);
    }
    fprintf(pagC,"</TR> \n");
}
fprintf(pagC,"</TABLE> </CENTER> </BODY> </HTML>");
fclose(pagC);
fclose(arch);
anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • *"I do not know what is wrong with this part of the code"* - We're not mind readers. How can we possibly know what is *wrong* with this code when you never told intended behavior is *right*? We could *guess*, but that isn't what this site is about. [Your post needs updating](https://stackoverflow.com/posts/46189198/edit) to include the intended behavior, the actual (wrong) behavior, how they are different, and any additional info required to reproduce the problem (like input data, etc). [minimal, **complete** example](https://stackoverflow.com/help/mcve) demonstrating wrong behavior is ideal. – WhozCraig Sep 13 '17 at 05:17

1 Answers1

1

This means:

while(getchar()!='\n')

you are reading a file char by char BUT you are not interested in the newLine character \n


as aside note this important comment from @WhozCraig

Equally important, A newline will terminate the loop, but no matter what was read, it is lost. Loop continuation only means whatever it was, it wasn't a newline. Further, getchar() is likely reading from stdin, while the statements inside appear only interested in file IO. Needless to say, this loop is flat-out-odd.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 3
    Equally important, A newline will terminate the loop, but no matter what was read, it is *lost*. Loop continuation only means whatever it was, it wasn't a newline. Further, `getchar()` is *likely* reading from `stdin`, while the statements inside appear only interested in file IO. Needless to say, this loop is flat-out-odd. `getchar()` is *likely* wrong, in favor of `fgetc` on one of the files (likely `arch`) – WhozCraig Sep 13 '17 at 05:12
  • 1
    Note that on traditional versions of Unix, or on Linux when the input comes from a file and not a terminal, EOF would cause the `while (getchar() != '\n')` loop to spin its wheels indefinitely. The loop should take care of EOF too: `int c; while ((c = getchar()) != EOF && c != '\n') ...` and the return value from the `fscanf()` call should be be checked too. And [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – Jonathan Leffler Sep 13 '17 at 05:55