Reputation: 2757
hi there i'm wondering that if is it possible to update a textfile which i mean add some text to all rows without changing the content. Suppose a textfile like this.
A
B
C
D
E
F
G
and i want to update like this way(adding another characters near to the first character off all rows)
A H
B I
C J
D K
E L
F M
G N
when i open a textfile in append mode and use fseek()
function in various ways even it can add some data in rows, some data from the end is going to missing everytime.
i hope its clear to understand the issue and i will appreciated if you can help. thanks anyway.
Upvotes: 0
Views: 958
Reputation: 6018
Your best bet is to simply read in all the data into memory, and write the file out with the desired format. Sometimes it's better to sacrifice a bit of efficiency for readability and maintainability.
Upvotes: 1
Reputation: 222714
The file model accessed through C and POSIX file operations presents files as a sequence of bytes, and there is no insert operation. You can only write over existing bytes (replacing them), add new bytes to the end, or truncate the file.
To create the output you desire, you must write to a new file. After you are done reading the input file, you could move the new file to the path of the old file, thus replacing the old file with the new output.
Upvotes: 2
Reputation: 58657
The concept of a 'row' in a text file really just means that you have a newline ('\n') character in the file.
To add data after the first element of a row in your text file, find the character you want to append after, then put your data between that character and the next newline character.
Upvotes: 0