-1

I'm trying to print a string inside a file but in reverse. But the fprintf doesn't print it into the file.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iso646.h>
#include <errno.h>
#include <stddef.h>
#define dim 50

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

    FILE *fin;
    FILE *fout;
    char str[dim];
    char nomefilein[dim];
    char nomefileout[dim];
    int i;

    printf("Inserisci il nome del file da leggere:\n");
    scanf("%s",nomefilein);
    printf("Inserisci il nome del file da scrivere:\n");
    scanf("%s",nomefileout);

    fin=fopen(nomefilein, "r");
    fout=fopen(nomefileout, "w");

    while (fgets(str, dim, fin)!=NULL) {

        printf("%s",str);
        
        for (i = 49; i > 0; i--) {

            fprintf(fout, "%s", str[i]);
            
        }
        
        
    }

    fclose(fin);

    return 0;
    
}

Can you help me?

AntonelloP
  • 29
  • 4
  • 4
    `str[i]` is `char`, so passing that to `%s` invokes *undefined behavior* and typically lead to Segmentation Fault. What do you want to do? – MikeCAT Apr 23 '21 at 12:07
  • 1
    Also note that if the input doesn't fill all of `str` then part of it will be *indeterminate*. Not to mention that you also attempt to write the string ending null-terminator to the file. – Some programmer dude Apr 23 '21 at 12:09
  • I wanna print the string taken in the first file and then print it in a second flie but in reverse – AntonelloP Apr 23 '21 at 12:12
  • You need to `fclose()` the both files , you need to verify if `fopen()` worked with success ... https://stackoverflow.com/questions/14680232/how-to-detect-a-file-is-opened-or-not-in-c – Saifeddine Ben Salem Apr 23 '21 at 12:13
  • You won't need 2D array to reverse the whole file. It can be done by just reading all of the contents ti one normal array of `char` and simply printing its contents from back after finished reading. – MikeCAT Apr 23 '21 at 12:27

2 Answers2

1
  • str[i] is char, so passing that to %s invokes undefined behavior and typically leads to Segmentation Fault because a typical valid address will take more than 1 byte.
  • You should calculate the length of the string read and use that instead of fixed start point 49.
  • You forgot to print str[0]. Also you may not want the newline character to be reversed (brought to top).

Instead of the for (i = 49; i > 0; i--) loop, try this:

i = strlen(str); /* get the length of string */
if (i > 0) {
    i--;
    if (i > 0 && str[i] == '\n') i--; /* ignore the last newline character */
    for (; i >= 0; i--) { /* use >=, not > */
        fputc(str[i], fout); /* you won't need fprintf() to print single character */
    }
    fputc('\n', fout); /* print newline character at end of line */
}

#include <string.h> should be added to use strlen().

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • This (sort of) fixes the printing in reverse, but not reading the file into the array. And the str declaration still needs to be the correct one. – Robert Harvey Apr 23 '21 at 12:14
  • 1
    @RobertHarvey It is unclear that "print a string inside a file but in reverse" means to reverse each line (I implemented this) or reverse the whole file. I don't see any conditions or constraints that is saying to read file into arrays here. I don't see any problem with the declaration of `str` (if the length is enough compared to the contents of input files). – MikeCAT Apr 23 '21 at 12:18
  • @MikeCAT I added to my program your answer, but still the 'fputc' doesn't print into the file! – AntonelloP Apr 23 '21 at 12:29
0

Assuming you simply want to reverse the string and then print it to wherever, that can be easily done with the following code, assuming you know the string's length:

for(int i=0, k=len-1; i<(len/2); i++, k--)
{
    temp = str[k];
    str[k] = str[i];
    str[i] = temp;
}

You can then just fprintf the string in the usual way.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501