Peršokti į turinį

Išvestis į failą windows formoje c++ [Išsiaiškinau galima rakinti tema]


Tuk1s

Ši tema yra neaktyvi. Paskutinis pranešimas šioje temoje buvo prieš 2807 dienas (-ų). Patariame sukurti naują temą, o ne rašyti naują pranešimą.

Už neaktyvių temų prikėlimą galite sulaukti įspėjimo ir pranešimo pašalinimo!

Recommended Posts

Sveiki, taigi iškilo toks klausimas, programuoju c++ ir gavau namų darbą windows formoje padaryti užduotėlę, tačiau man ji problemų nesukelia, didžiausia problema ta, kad niekaip nepavyksta išvesti į failą rezultatų. Nors viršuje prirašau #include <fstream> tačiau neleidžia naudoti ofstream funkcijos. tiesiog rašo: identifier "ofstream" is undefined. Galbūt žinote kaip būtų galima išvesti į failą windows formoje?

Redaguota , nario Tukas14
Nuoroda į komentarą
Dalintis per kitą puslapį

pataisysiu nes nusišnekėjau

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream fr("file.txt");
    fr << "ofstream yay";
    return 0;
}

pabandyk, gal kažkur klaida yra pas tave, jei šitas kodas neveiks tai gal neturi fstream include?

Redaguota , nario Dr. Arnas
Nuoroda į komentarą
Dalintis per kitą puslapį

(redaguota)

pataisysiu nes nusišnekėjau

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream fr("file.txt");
    fr << "ofstream yay";
    return 0;
}

pabandyk, gal kažkur klaida yra pas tave, jei šitas kodas neveiks tai gal neturi fstream include?

Čia yra paprastam console code tai taip, bet pvz windows formoje yra kitaip neveikia tas ofstream. Tarkim numesiu koda:

#pragma once
#include <fstream>
#include <cmath>

namespace Namai {
	void fun (double b, double xp, double xg, double hx);
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		double	a,
				xp, 
				xg, 
				hx;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::TextBox^  textBox3;
	private: System::Windows::Forms::TextBox^  textBox4;
	private: System::Windows::Forms::Button^  button1;
			 System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->textBox4 = (gcnew System::Windows::Forms::TextBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(61, 30);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(25, 17);
			this->label1->TabIndex = 0;
			this->label1->Text = L"Xp";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(357, 30);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(25, 17);
			this->label2->TabIndex = 1;
			this->label2->Text = L"Xg";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(61, 134);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(24, 17);
			this->label3->TabIndex = 2;
			this->label3->Text = L"Hx";
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(357, 134);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(16, 17);
			this->label4->TabIndex = 3;
			this->label4->Text = L"a";
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(33, 54);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(100, 22);
			this->textBox1->TabIndex = 4;
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(333, 50);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(100, 22);
			this->textBox2->TabIndex = 5;
			// 
			// textBox3
			// 
			this->textBox3->Location = System::Drawing::Point(33, 158);
			this->textBox3->Name = L"textBox3";
			this->textBox3->Size = System::Drawing::Size(100, 22);
			this->textBox3->TabIndex = 6;
			// 
			// textBox4
			// 
			this->textBox4->Location = System::Drawing::Point(333, 158);
			this->textBox4->Name = L"textBox4";
			this->textBox4->Size = System::Drawing::Size(100, 22);
			this->textBox4->TabIndex = 7;
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(194, 208);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(88, 35);
			this->button1->TabIndex = 8;
			this->button1->Text = L"Vygdyti";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
			// 
			// MyForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::Color::DarkKhaki;
			this->ClientSize = System::Drawing::Size(482, 255);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->textBox4);
			this->Controls->Add(this->textBox3);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Name = L"MyForm";
			this->Text = L"MyForm";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
				 xp = double::Parse(textBox1->Text); // gaunamas xp
				 xg = double::Parse(textBox2->Text); // gaunamas xg
				 hx = double::Parse(textBox3->Text); // gaunamas hx
				 a = double::Parse(textBox4->Text); // gaunamas a
				 fun (a, xp, xg, hx);
			 }
			 void fun (double a, double xp, double xg, double hx)
			 {
				double y;
				for (double x = xp; x <= xg; x+=hx) 
				{
					if(x >= 0) 
					{
						y = (sin(x)-sqrt(x))*a;
					}
					
				}
			 }
};
}


Ir štai čia viršuje fstream įvedu, bet ofstream vistiek neveikia. čia yra ne console application čia rimčiau per visual studio windows forma. 

Redaguota , nario Tukas14
Nuoroda į komentarą
Dalintis per kitą puslapį

Negalėtų būti tai, kad naudoji ofstream o ne std::ofstream? Nes niekur kode nematau 

using namespace std;

Todėl jei nori išvesti į failą reikėtų naudoti std::ofstream

 

Jei ne tas tai nepyk, neesu dirbęs su C++ ir Windows vienu metu

Redaguota , nario InSaneOxT
  • Teigiamai 1
Nuoroda į komentarą
Dalintis per kitą puslapį

Negalėtų būti tai, kad naudoji ofstream o ne std::ofstream? Nes niekur kode nematau 

using namespace std;

Todėl jei nori išvesti į failą reikėtų naudoti std::ofstream

 

Jei ne tas tai nepyk, neesu dirbęs su C++ ir Windows vienu metu

Hmm įdomus variantas, tiesą sakant nežinau ar taip galima, bet pabandysiu gryžes patikrinti.

Nuoroda į komentarą
Dalintis per kitą puslapį

Tai vat išsiaiškinau kodėl man neidavo išvesti į failą. Tagi parašysiu, kad tokiems kaip man kitą kartą nekiltų tiek daug problemų ieškant atsakymų, nes pats žinau kaip sunku rasti kažką panašaus. Tai tiesiog paprastas dalykas naudojau (visual studio) ir manau kitiems naudojant jį ir programuojant windows formoje norint išvesti į failą ar gauti iš failo viršuje(po visais include) tiesiog pakanka vienos eilutės kodo:

using namespace std;

Tai tiek.

Nuoroda į komentarą
Dalintis per kitą puslapį

 tai kad ir ne windows forma programuojant to reikia :) nebent pries kiekviena fstream includo funkcija rasysi std::

Aš turiu omenyje tai, jog paprastame tu code blocks jau rasi parašyta o visual studio žinoma ir taip reikia prirašyti, bet yra kitas dalykas kai per windows forma darai ten jau viskas irgi būna surašyta tik to using namespace std; nebūna myform.h failiuke. Tiesiog neužkliūna toks dalykas nes myform.cpp yra bent pas mane taip.

Nuoroda į komentarą
Dalintis per kitą puslapį

Ši tema yra neaktyvi. Paskutinis pranešimas šioje temoje buvo prieš 2807 dienas (-ų). Patariame sukurti naują temą, o ne rašyti naują pranešimą.

Už neaktyvių temų prikėlimą galite sulaukti įspėjimo ir pranešimo pašalinimo!

Svečias
Ši tema yra užrakinta.
  • Šiame puslapyje naršo:   0 nariai

    • Nėra registruotų narių peržiūrinčių šį forumą.

Skelbimai


×
×
  • Sukurti naują...