0

Hi everyone I'm having a nightmare while trying to figure out why the fprintf() function is not writing the string to the file.

I would say that I'm 95% sure that the problem is on the highlighted line down in the code sample because when I'm printing the exact same thing to the console (with printf()) is indeed printing the string that I need.

for (int i = 1; i <= nmappers; i++)
{
    if (fork() == 0) //SI ENTRA A ESTE IF
    {
        sprintf(nombreArchivo, "split%d", i);
        ptrLectura = fopen(nombreArchivo, "r");
        sprintf(nombreArchivo, "buf%d", i);
        ptrEscritura = fopen(nombreArchivo, "w");

        while ((caracter = fgetc(ptrLectura)) != EOF)
        {
            if (caracter != ' ' && caracter != '\n' && !encontroCadena)
            {
                printf("111\n");
                strncat(cadena, &caracter, 1); //Pa que sirva toca con &
                ncol++;
                encontroCadena = true;
            }
            else if (caracter != ' ' && caracter != '\n' && encontroCadena)
            {
                printf("222\n");
                strncat(cadena, &caracter, 1); //Pa que sirva toca con &
            }
            else if (caracter == ' ')
            {
                printf("333\n");
                encontroCadena = false;
            }
            if (caracter == '\n')
            {
                printf("%s\n", cadena);
                printf("444\n");
                fprintf(ptrEscritura, "%s", cadena); //PROBLEM'S PROBABLY HERE
                ncol = 0;
            }
        }
        fflush(ptrEscritura);
        fclose(ptrLectura);
        fclose(ptrEscritura);

        exit(0);
    }
}

PS: The files in wich I want to write the strings are being create succesfully, but the fprintf() function is not working! Also I've tried changing it for fputs(), neither worked.

I will appreciate a lot the help that you could provide to me!

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Dacaramo
  • 75
  • 10
  • The `fork()` immediately has me suspicious. What are the return values of those `fprintf` calls? – tadman Oct 02 '20 at 01:35
  • This could be a byproduct of using `FILE*` across processes. If each process opened their own handles it may work better. – tadman Oct 02 '20 at 01:45
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Oct 02 '20 at 01:48
  • 1
    Are you sure that the memory buffer `cadena` has sufficient space for the `strncat` call? – Andreas Wenzel Oct 02 '20 at 01:50
  • @tadman the fprintf() function is returning only positive values – Dacaramo Oct 02 '20 at 01:53

1 Answers1

1

There are two available reasons.if(caracter == '\n') { ... fprintf(ptrEscritura, "%s", cadena); //PROBLEM'S PROBABLY HERE ... }

  1. The if condition is not matched(caracter is not equal with '\n'), the fprintf function is not called.
  2. cadena is empty.

if you test which occurs error, please do like below:

  • fprintf(ptrEscritura, "%s", "abc"); // if here prints abc, then error exists in number 2).
  • check cadena[0] == 0.

I'm glad if it helps you.