0

Say I have a simple text file.txt: 1234567890. I need to edit it inserting a + between each character in file. Simpliest would be to use fseek yet

For streams open in text mode, offset shall either be zero or a value returned by a previous call to ftell, and origin shall necessarily be SEEK_SET.

So I can only stay where I currently am with it or at the beggining of file stream?

so the question is how to fseek 1 character left or right from current position when editing a text file in C?

DuckQueen
  • 772
  • 10
  • 62
  • 134
  • 3
    You can move around with `fseek()`, but you can't 'insert' easily -- you can only overwrite, not insert or delete. Oh, and `fseek(fp, -1L, SEEK_CUR);` seeks back one character on civilized systems (meaning Unix-like systems). On systems where text and binary files are different (Windows), then there are a whole raft of issues related to the way CRLF line endings are managed and the constraint you quote is technically applicable (Standard C says so). I think POSIX partially or wholly lifts those restrictions (if only because it doesn't recognize text files as different from binary files). – Jonathan Leffler May 07 '14 at 17:38
  • What `L` stands for? long? – DuckQueen May 07 '14 at 17:41
  • 1
    Yes, it is a suffix that indicates the number is a `long`, not an `int` that it would be otherwise. You can have `LL` and `UL` and `ULL` too. You can use either case, but you should never use lower-case (because `l` and `1` are too easily confused). A prototype forces the conversion, so you should in fact be OK without the suffix. – Jonathan Leffler May 07 '14 at 17:43
  • 1
    Are you constrained to using SEEK etc.?, or are you able to consider other approaches? Using an `fgetc()`, `fputc()` approach would allow you to open the read file, and a new write file, loop through the read file, char by char, and write each char to write file, but inserting the `+` char every other write. – ryyker May 07 '14 at 18:48
  • Or read the [size of the file](http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c), allocate a buffer at least twice this size, copy one char into the new buffer, add a '+', copy, add, etc. Once the buffer is filled, write it out to disk. – rdtsc Jul 17 '15 at 11:41
  • @JonathanLeffler Hello can you please expound what you mean by overwrite, not insert or delete. Does it mean that if I call fseek (fp, 3, 0) and fprintf (fp, "Hi") and the resulting output of the file will be 123Hi or 123Hi67890 – Janjan Apr 28 '20 at 19:48
  • 1
    @Janjan — If the file initially contains `"1234567890\n"` and you do `fseek(fp, 3, 0)`, and then `fprintf(fp, "Hi")`, and then you close the file (`fclose(fp)`), then the file will contain `"123Hi67890\n"`. The printing doesn't write a null byte. The file is not truncated by writing. It will be extended if need be, so if instead of `"Hi"` you write `"Hello World!\n"` to the file, then it will be longer than before — containing `123Hello World!\n"`. If the file was opened in append mode, then the rules would change — writing `"Hi"` would give you `"1234567890\nHi"` (no trailing newline). – Jonathan Leffler Apr 29 '20 at 02:13

0 Answers0