Normalt vil ofstream lave en tom fil, men du kan undgå dette ved at tilføje ios::app (i.e. append) når du åbner den. Fil pointeren vil så pege på enden af filen.
Husk i øvrigt at du ikke kan indsætte midt i en fil, du kan kun overskrive, hvis du vil indsætte bliver du nødt til at læse og skrive hele filen.
hmm ikke meningen jeg ville lave et svar :s anyway... det er nemt nok at skrive hele filen forfra, men bare besværligt og tager tid og det er jo kun nogle enkelte byte der skal skrives om..
The character string mode specifies the type of access requested for the file, as follows:
"r" Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"w" Opens an empty file for writing. If the given file exists, its contents are destroyed.
"a" Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn't exist.
"r+" Opens for both reading and writing. (The file must exist.)
"w+" Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+" Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist.
When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.
The "a" mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. The "a+" mode does remove the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.
In addition to the above values, the following characters can be included in mode to specify the translation mode for newline characters:
t Open in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing with "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z, may cause fseek to behave improperly near the end of the file.
Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the mbtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).
b Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.
If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL.
For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.
c Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called.
n Reset the commit flag for the associated filename to "no-commit." This is the default. It also overrides the global commit flag if you link your program with COMMODE.OBJ. The global commit flag default is "no-commit" unless you explicitly link your program with COMMODE.OBJ (see Link Options).
N Specifies that the file is not inherited by child processes.
S Specifies that caching is optimized for, but not restricted to, sequential access from disk.
R Specifies that caching is optimized for, but not restricted to, random access from disk.
T Specifies a file as temporary. If possible, it is not flushed to disk.
D Specifies a file as temporary. It is deleted when the last file pointer is closed.
Jeg tror jeg ved hvordan man gør.. When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.
Men det er en .mp3 fil hvor jeg gerne vil kunne autogenerere informationer om filen, derved skal jeg dels have skrevet noget ny data i starten og i slutningen af filen.
Må indrømme at det jeg har arbejdet med filer er begrænset indtil videre!! Men altså det der kunne gøres er jo at indlæse hele filen først og skrive den bagefter igen med de bytes du vil have ind her og der
Ja det var også min første ide.. men det er jo meget tidskrævende og hvis man nu har 1000 filer tager det lang lang tid så der burde være en nemmere og hurtigere måde at gøre det på
Citat: "Men tjaa så er der jo et andet problem ang. det med at skrive i begyndelsen af filen.. der skal jeg jo have rykket alt dataen"
Man kan ikke indsætte midt i en fil og få "den" til at flytte data, uanset hvordan du åbner filen. Hvis du har behov for at indsætte (og ikke blot overskrive) midt i en fil er du nødt til at læse og skrive hele filen.
Jeg er nok kommet til kort og har valgt at skrive filen forfra med de input der nu skulle være rundt omkring, så fiil123, det var vist din løsning jeg brugte, smider du et svar? og mange tak for hjælpen til jer begge
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.