Форум программистов «Весельчак У»
  *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

  • Рекомендуем проверить настройки временной зоны в вашем профиле (страница "Внешний вид форума", пункт "Часовой пояс:").
  • У нас больше нет рассылок. Если вам приходят письма от наших бывших рассылок mail.ru и subscribe.ru, то знайте, что это не мы рассылаем.
   Начало  
Наши сайты
Помощь Поиск Календарь Почта Войти Регистрация  
 
Страниц: [1]   Вниз
  Печать  
Автор Тема: косяк с fstream  (Прочитано 9564 раз)
0 Пользователей и 1 Гость смотрят эту тему.
Casy
Гость
« : 30-11-2003 09:14 » 

если я пишу вот так, то все работвет:
#include <iostream.h>
#include <fstream.h>

int main()
{
   fstream out("test.bbb",ios::out|ios::binary|ios::ate);
   char str[10] = {1,2,3,4,5,6,7,8,9,10};
   
   out.seekp(-2,ios::end);
   out.write(str, 10);
   return 0;
}

содержимое файла не затираеться, и указатель устанавливаеться в конец файла -2 байта.

а вот если делаю так:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   fstream out("test.bbb",ios::out|ios::binary|ios::ate);
   char str[10] = {1,2,3,4,5,6,7,8,9,10};
   
   out.seekp(-2,ios::end);
   out.write(str, 10);
   return 0;
}

то старый файл затираеться:((
подскажите, что с этим можно сделать?

ЗЫЖ мне нужен работающий второй вариант для длступа к типу string.
Записан
sss
Специалист

ru
Offline Offline

« Ответ #1 : 01-12-2003 09:32 » 

Черт его знает, попробуй поставить ios::fstream out ....
Записан

while (8==8)
ysv_
Помогающий

ua
Offline Offline

« Ответ #2 : 01-12-2003 11:19 » new

В VC (а MinGW использует VC Run Time DLL) вызов basic_fstream::open транслируется в вызов fopen(). Дальше вырезка из MSDN:
====================================================
ios_base::in becomes "r" (open existing file for reading).
ios_base::out or ios_base::out | ios_base::trunc becomes "w" (truncate existing file or create for writing).
ios_base::out | app becomes "a" (open existing file for appending all writes).
ios_base::in | ios_base::out becomes "r+" (open existing file for reading and writing).
ios_base::in | ios_base::out | ios_base::trunc becomes "w+" (truncate existing file or create for reading and writing).
ios_base::in | ios_base::out | ios_base::app becomes "a+" (open existing file for reading and for appending all writes).
If mode & ios_base::binary is nonzero, the function appends b to strmode to open a binary stream instead of a text stream. It then stores the value returned by fopen in the file pointer fp. If mode & ios_base::ate is nonzero and the file pointer is not a null pointer, the function calls fseek(fp, 0, SEEK_END) to position the stream at end of file. If that positioning operation fails, the function calls close(fp) and stores a null pointer in the file pointer.
===================================================
Вывод использовать fstream out("test.bbb",ios::in|ios::out|ios::binary);
Записан
Anonymous
Гость
« Ответ #3 : 02-12-2003 06:46 » 

Цитата: ysv_
В VC (а MinGW использует VC Run Time DLL) вызов basic_fstream::open транслируется в вызов fopen(). Дальше вырезка из MSDN:
====================================================
ios_base::in becomes "r" (open existing file for reading).
ios_base::out or ios_base::out | ios_base::trunc becomes "w" (truncate existing file or create for writing).
ios_base::out | app becomes "a" (open existing file for appending all writes).
ios_base::in | ios_base::out becomes "r+" (open existing file for reading and writing).
ios_base::in | ios_base::out | ios_base::trunc becomes "w+" (truncate existing file or create for reading and writing).
ios_base::in | ios_base::out | ios_base::app becomes "a+" (open existing file for reading and for appending all writes).
If mode & ios_base::binary is nonzero, the function appends b to strmode to open a binary stream instead of a text stream. It then stores the value returned by fopen in the file pointer fp. If mode & ios_base::ate is nonzero and the file pointer is not a null pointer, the function calls fseek(fp, 0, SEEK_END) to position the stream at end of file. If that positioning operation fails, the function calls close(fp) and stores a null pointer in the file pointer.
===================================================
Вывод использовать fstream out("test.bbb",ios::in|ios::out|ios::binary);


спасибо, так все работает.
Записан
Страниц: [1]   Вверх
  Печать  
 

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines