0

Don't know if it is duplicate, if so, mark it ( I havennot found answers anywhere).

Having this:

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

int main (void) {

    FILE *fp = fopen("txt2", "r+"); // does not help a+ or w+
    fprintf(fp,"20");
    fseek(fp,1,SEEK_CUR);
    fprintf(fp,"19");

    for(char c;(c=getc(fp))!=EOF;)
        putchar(c);

    fclose(fp);
    return 0;
}

Cannot write to the file, and at the end view the file via putchar. how to open file to read as well as read? (I have tried r+,w+,a+, non helped). Still confused with their differencies(r+/w+ - both are positioned at the beginning of file, so in which differ? only a+ makes sense, as only it writes at the end (after everything else) in the file).

I don't know it the (r/w)+ mode makes some effect, since noone has yet explained the differences and usecases, but I have changed the mode from r+ to w+ and the offset of fseek:

FILE *fp = fopen("txt2", "w+");
...
fseek(fp,10,SEEK_CUR);

but, the output is 2019 instead of 20 19. So does that mean fseek trims white spaces? or why is not the desire output?

and it

autistic456
  • 183
  • 1
  • 10
  • `c` must be `int` – pmg May 28 '20 at 17:54
  • Why so? There is no char->int conversion? Anyway, still dont get the output – autistic456 May 28 '20 at 17:55
  • Because a `char` is not able to handle `EOF` – David Ranieri May 28 '20 at 17:56
  • Read the documentation. It explains what getc returns. – klutt May 28 '20 at 17:57
  • Well, without writing to the file `fprintf(fp,"20");fseek(fp,1,SEEK_SET);fprintf(fp,"19");` - (make it comment), I can still use `char` in for loop and it works. So your statement is not true for my case (or compiler is making char->int conversion for me) – autistic456 May 28 '20 at 17:58
  • The compiler makes a `int`->`char`conversion when you assign return value to variable of wrong type. Even if a program seems to work it can be still wrong. This is clearly wrong as you cannot distinguish 255 from -1 with a `char`. – Gerhardh May 28 '20 at 18:07
  • @Gerhardh 255 is not possible in `char` since it is not `unsigned char` but it *IS* signed, so the value would be `-1` – autistic456 May 30 '20 at 12:44
  • It is not `signed char` but only plain `char` which can be signed or unsigned depending of the compiler. – Gerhardh May 30 '20 at 16:46
  • @Gerhardh but the twos complement is still preserved for math operations, so I assume it as `signed` (even thought not said explicitly). – autistic456 May 30 '20 at 16:48
  • How is this relevant? If the function returns 255 or -1 both will have same representation and you cannot distinguish. – Gerhardh May 30 '20 at 18:11

1 Answers1

1

You need to add a call to fflush() or fseek() ... between reading and writing.

See https://port70.net/~nsz/c/c11/n1570.html#7.21.5.3p7

When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end- of-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations.

    fprintf(fp,"19");              // write

    rewind(fp);                    // follow rhe rules

    for(int c;(c=getc(fp))!=EOF;)  // read, make sure c is int
pmg
  • 106,608
  • 13
  • 126
  • 198
  • 1
    `getc` wants an `int` – David Ranieri May 28 '20 at 18:02
  • Can you also elaborate the second question of explanation of the `fopen` modes? like the differences between `w+/r+/a+` and when use one over the other. The man page say nothing other the the position it will get at, but I am still confused with them – autistic456 May 28 '20 at 18:02
  • @DavidRanieri that is right. But it sill works, maybe compiler helps here? – autistic456 May 28 '20 at 18:03
  • 2
    @autistic456 you need to change the chip: "it sill works" it's bad reasoning, follow the standard rules. – David Ranieri May 28 '20 at 18:04
  • @pmg, why did you change the function `fflush` to `rewind`? what is different, as I know, both sets file position at the beginning of the file. So why did you do that? – autistic456 May 28 '20 at 18:06
  • @DavidRanieri of course. But that does not explain why it works. As I suspect the compiler does help here. – autistic456 May 28 '20 at 18:06
  • `fflush()` does not set to the beginning: the program would read and get the `EOF` signal – pmg May 28 '20 at 18:07
  • I guess on your platform `EOF` is representable as a `char` but don't rely on this. – David Ranieri May 28 '20 at 18:08
  • @pmg then why did you used it previously? (just want to know its usecase) – autistic456 May 28 '20 at 18:08
  • @DavidRanieri you meant, that macro EOF is less or equal to `255`? – autistic456 May 28 '20 at 18:08
  • Any of the functions mentioned in the Standard will alow reading and writing for `"r+"` files. However the function should be chosen according to the program needs (I first used `fflush()` just to follow the rules, disregarding the program needs) – pmg May 28 '20 at 18:10
  • @autistic456 or -128 to 127 depending on the sign of plain `char`, take a look to [What is value of EOF and '\0' in C](https://stackoverflow.com/questions/4705968/what-is-value-of-eof-and-0-in-c) – David Ranieri May 28 '20 at 18:12
  • @pmg but I do not know if your solutions makes it work for other offset of fseek. Take a look at my edit, I cannot see whitespaces in the output. Is it because of the mode of the `rewind`? – autistic456 May 28 '20 at 18:31