0

I have File ,that contains strings with maximum 80 symbols,I need to center them in current file,I have this code now ,but i can't handle fseek,I can't use another file as buffer,struggling on this task for 3 days ,and I would be happy if someone help me

     #include <stdio.h>
     #include <string.h>
     #define SIZE 80
     int ToInt(size_t  size){
     return static_cast <int>(size);
     }
    int main()
    {
        FILE *SourceFile;
        char String[SIZE];
        SourceFile = fopen("D:/Study/Labs/C/LAb4/LAb4.2/test.txt","r+");
        if(!SourceFile){
            printf("Can't open source file");
        }
        else{
            printf("File's opened\n ");
            int Counter = 0 ;
            while(!feof(SourceFile)){
                fseek(SourceFile,Counter*SIZE,SEEK_SET);
                fgets(String,SIZE,SourceFile);
                printf("String %d is readen\n ",Counter+1);
                char NewString[SIZE];
                int size = (80 - ToInt(strlen(String)))/2;
                for(int i = 0;i<size    ;i++){
                    NewString[i]= ' ';
                }
                for(int i = 0;i<ToInt(strlen(String));i++ ){
                    NewString[i+size]=String[i];
                }
                fseek(SourceFile,Counter * SIZE,SEEK_SET);
                fwrite(NewString,1,SIZE,SourceFile);
                printf("String %d is Writen\n ",Counter+1);
                Counter++;
            }
            fclose(SourceFile);
            printf("\n Source File Closed");
        }

    }
  • 2
    What does "center string in a file" mean? Please provide example input, expected result and actual result. – kaylum Apr 17 '20 at 23:35
  • 1
    `static_cast` is C++. – anastaciu Apr 17 '20 at 23:36
  • 1
    See [Why is `while(!feof(fp))` always wrong?](https://stackoverflow.com/questions/5431941) – user3386109 Apr 17 '20 at 23:40
  • @kaylum For example we have world "Kaylum",so first 37 symbols will be filled with spaces,then "Kaylum", and again 37 spaces.My programm reads first string properly then writes it also properly,in second step file is filling by trash. – Kalacey412 Apr 17 '20 at 23:57
  • What is second step? Do you mean you want to take each word from the original file and then put them each centered on a single line with fixed width of 80 characters? Are you allowed to read all the words into memory first? – kaylum Apr 18 '20 at 00:03

0 Answers0