Elektroda.pl
Elektroda.pl
X

Search our partners

Find the latest content on electronic components. Datasheets.com
Elektroda.pl
Please add exception to AdBlock for elektroda.pl.
If you watch the ads, you support portal and users.

(Visual c++) fstream - Zapis do pliku

29 Jan 2012 16:39 2297 9
  • Level 11  
    Witam!
    Mam mały problem korzystam z biblioteki fstream (Visual c++) i chce pobrać tekst z textBox i zapisać go do pliku no i właśnie tu pojawia się problem ponieważ nie mogę znaleźć odpowiedniej konwersi. Do zapisu liczb napisałem coś takiego [code]
    Code: cpp
    Log in, to see the code


    ale z tekstem niestety już nie mogę sobie poradzić próbowałem ze String^ i nic :cry: .

    Dodano po 5 [godziny] 49 [minuty]:

    Morze ktoś odpowiedzieć proszę :D
  • Level 12  
    A jak dajesz

    Code: cpp
    Log in, to see the code


    To jaki błąd dostajesz?
  • Level 11  
    >c:\documents and settings\darek\moje dokumenty\visual studio 2010\projects\hfgf\hfgf\Form1.h(278): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion)
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(726): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(764): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(811): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(937): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(944): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(951): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(958): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(968): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>,System::String^>(std::basic_ostream<_Elem,_Traits> &&,_Ty)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Ty=System::String ^
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(1085): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(165): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__clrcall *)(std::basic_ostream<_Elem,_Traits> &))'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(171): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__clrcall *)(std::basic_ios<_Elem,_Traits> &))'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(178): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__clrcall *)(std::ios_base &))'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(186): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(192): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(199): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(206): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(226): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(260): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(280): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(305): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned int)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(325): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(345): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(366): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(386): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(407): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(427): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(447): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(467): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(487): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> while trying to match the argument list '(std::ofstream, System::String ^)'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  • Level 12  
    Może spróbuj :

    Code: cpp
    Log in, to see the code
  • Level 11  
    Kompilator wywala:
    1>c:\documents and settings\darek\moje dokumenty\visual studio 2010\projects\hfgf\hfgf\Form1.h(278): error C2440: 'initializing' : cannot convert from 'System::String ^' to 'std::basic_string<_Elem,_Traits,_Ax>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Ax=std::allocator<char>
    1> ]
    1> No constructor could take the source type, or constructor overload resolution was ambiguous
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    uruchamiam program mimo to i liczby zapisuje normalnie a w literach

    An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

    Additional information: Input string was not in a correct format.

    i otwiera jakiś kod
    [code]
    Code: cpp
    Log in, to see the code

    w sumie nie wiem do czego jest ten kod ale w nim zaznacza [b return 0;
  • Level 12  
    Aha. Musisz sobie skonwertować ten typ System::String na char albo (chyba też zadziała) zwykły std::string. Na tej stronie masz przykłady:
    Code:
    http://support.microsoft.com/kb/311259
    Jak to zrobić.

    Spróbuj może najpierw tak:
    Code: cpp
    Log in, to see the code
  • Level 20  
    Inna sprawa - dlaczego używasz flagi
    Code:
    ios::binary

    skoro chcesz zapisać tekst?
  • Level 11  
    Witam!
    spróbowałem napisać ten przykład który kolega podał ale niestety kompilator nie zna 'Marshal' i 'StringToHGlobalAnsi'. Poczytałem na stronie i spróbowałem dodać biblioteki (nie wiem czy potrzebnie) ale niestety nic to nie dało nie otworzyłem tylko tej <atlstr.h> ponieważ jej nie znalazło. Czy błędy przy kompilacji są winą tego że nie mam jakiejś biblioteki ?


    Co do ios::binary to dodałem to jak sam próbowałem uruchomić program ale teraz już to usunąłem.
  • Helpful post
    Level 12  
    Zrób tak: poszukaj sobie ctrl + f: "using namespace" (na górze pliku są).
    Dopisz tam:

    Code: cpp
    Log in, to see the code


    I użyj metody którą podałem wcześniej.
  • Level 11  
    Bardzo dziękuje udało się.

    Temat można zamknąć.