Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

It stops printing after 4th output line in C

+3 votes
asked Oct 2, 2022 by Ias l33t (150 points)
At the end of this code it prints "Hello", but then stops printing ahead, so never prints "World". Does anyone knows what could be wrong? I would be very glad if someone helps me out :)

int main(){
    FILE *fuente = fopen("certamen.txt", "r");
    int a;
    fscanf (fuente, "%d\n", &a);
    tCertamen* certamen = crearCertamen(a);
    printf("Hay %d preguntas en el certamen\n", a);
    for(int i = 0; i < a; i ++){
        char b[64];
        printf("%d\n", i);
        fscanf(fuente, "%s\n", b);
        if (strcmp(b, "AlternativaSimple") == 0){
            tEnunciadoAlternativa* enunciado = malloc(sizeof(tEnunciadoAlternativa));
            printf("%s\n", b);
            char c[128];
            fscanf(fuente, "%[^\n]s\n", c);
            printf("%s Hello\nWorld", c);

1 Answer

0 votes
answered Oct 2, 2022 by Peter Minarik (101,360 points)

If I just grab the last 3 lines of your shared code and replace fuente by stdin (i.e., reading from the standard input, instead of a file) then I see both "Hello" and "World" on the console.

So maybe your file is causing the issue. Have you reached the end of the file?

Can you share a link to the whole project (that includes the input file as well, not just the program so I could test what's going on)?

Note: it is useful to write your program in English (identifiers, comments, etc) so others could better understand what's going on there. :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...