-1

Here is my code but I don't know why it prints only some part of lines. here is my code:

#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>

int main(int argc,char *argv[])
{
    FILE *fpr,*fpw;
    int cnt=0;
    fpw=fopen(argv[2],"w+");
    char buff[1000];
    while((fpr=fopen(argv[1],"r"))==NULL)
    {
        printf("\nCan't open file %s\n",argv[1]);
        scanf("re-enter file name:%s\n",argv[1]);
    }
    
    while (!feof(fpr))
    {
        
        fgets(buff,2,fpr);

        if(buff[0]=='\n')
        {
            putc(buff[0],fpw);
            fseek(fpw,0,SEEK_SET);
        
        }
        fputs(buff,fpw);
        cnt++;
    }

    
    fclose(fpr);
    fclose(fpw);
    
}

INPUT FILE:

Hrey yhis will print twicw

lets print thrice

@why not

OUTPUT FILE

lets print thrice @why not

Nizar Kadri
  • 321
  • 2
  • 10
  • Reading into `argv[1]` could be fatal. First of all, you don't even know if there is a valid string in `argv[1]`. Secondly that string (if valid) have a fixed size, and it's very easy to go out of bounds of that. – Some programmer dude Feb 07 '21 at 12:54
  • 2
    Also please take some time to read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Feb 07 '21 at 12:56
  • Lastly, and probably your problem, please take some time to think about `fseek(fpw,0,SEEK_SET);` and what it does. – Some programmer dude Feb 07 '21 at 12:57
  • This code is not meant to be industrial and I am learning things so I am not taking handling possible wrong inputs by user. That's why I asked this question to have clear understanding – Nizar Kadri Feb 07 '21 at 19:28
  • Updated:This code is not meant to be industrial and I am learning things so I am not taking handling possible wrong inputs by user. That's why I asked this question to have clear understanding. Also as per my understanding fseek(fpw,0,SEEK_SET) will set the pointer at the starting of file. – Nizar Kadri Feb 07 '21 at 19:42
  • Yes the seek call will set the "file pointer" to the beginning of the file, causing next write to ***ovwrwrite*** What have already been written. Making you "loose" some of the data. But since each input line is sorter than the previous, the newline from the previous will not be overwritten, making it appear as different (partial}) lines. – Some programmer dude Feb 07 '21 at 21:39
  • To possibly make you understand it better, I suggest you do the "write" and "seek" using pencil and paper. – Some programmer dude Feb 07 '21 at 21:40

1 Answers1

0

So.. basically what I was trying to do is actually impossible using lseek/fseek. So I found another approach , as shown in the code below:

#include <stdio.h> 
#include <string.h> 
#include<stdlib.h>

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

    FILE* fr;
    FILE* fw;
    int l=0;
    char lines[10000][100];
    if( ( (fr=fopen(argv[1],"r+"))==NULL || (fw=fopen(argv[2],"w+"))==NULL )  )
    {
        printf("Error reading or opening files %s,%s",argv[1],argv[2]);
    }

    while(fgets(lines[l++], sizeof(lines[l]), fr)!=NULL);
        

    while(l>=0)
        fputs(lines[l--],fw);
        

    fclose(fr);
    fclose(fw);
}
Nizar Kadri
  • 321
  • 2
  • 10