簡體   English   中英

視覺c ++ fstream錯誤

[英]visual c++ fstream error

首先,我是一名硬件程序員(HDL,uC編程),幾天前我的老板要求我在Visual C ++中創建一個接口來控制uC。 我以前從未使用過Visual C ++,而且我的軟件編程技能最多不過是中級。 但是,我只有到星期二才可以使用該界面,因此,由於時間限制,我必須查找示例並進行相同的操作。 因此,如果我提出任何明顯和愚蠢的問題,請原諒。

在我的代碼中,我必須將存儲在數組中的值移動到CSV文件。 因此,我必須使用逗號分隔值。但是,為了創建CSV文件,我必須使用fstream(據目前為止所了解的內容)。 每當我使用

#include <fstream>

我收到大量錯誤,例如:

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(244): error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(244): error C2039: 'YesNoMaybe' : is not a member of '`global namespace''

這是我與fstream相關的其余代碼:

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO::Ports;
#include <iostream>
#include <fstream>

      std::ofstream myfile;
            myfile.open("Data.txt");
            //find available ports
            private: void findports(void){
                         array<Object^>^ objectArray = SerialPort::GetPortNames();
                         this->comboBox4->Items->AddRange( objectArray );
                         array<String ^> ^ h = gcnew array<String ^>(24);
                         for(int i=0; i<=23; i++){
                             h[i]= String::Concat(i.ToString());
                         }
                         this->comboBox1->Items->AddRange( h );
                          array<String ^> ^ m = gcnew array<String ^>(60);
                         for(int i=0; i<=59; i++){
                             m[i]= String::Concat(i.ToString());
                         }
                         this->comboBox3->Items->AddRange( m );
                          array<String ^> ^ s = gcnew array<String ^>(24);
                         for(int i=0; i<=23; i++){
                             s[i]= String::Concat(i.ToString());
                         }
                         this->comboBox5->Items->AddRange( s );
                          array<String ^> ^ d = gcnew array<String ^>(366);
                         for(int i=0; i<=365; i++){
                             d[i]= String::Concat(i.ToString());
                         }
                         this->comboBox2->Items->AddRange( d);
                     }








        private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
                 }
        private: System::Void label2_Click(System::Object^  sender, System::EventArgs^  e) {
                 }
    private: System::Void label4_Click(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void label5_Click(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void label6_Click(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

                 if((this->comboBox4->Text == String::Empty)||(this->textBox1->Text == String::Empty)||(this->textBox2->Text == String::Empty)){
                     this->textBox1->Text="missing port settings";
                     this->textBox2->Text="missing port settings";
                 }
                 else{ // start assigning
                     try{ // first make sure port isn't busy/open
                         if(!this->serialPort1->IsOpen){
                             // select the port whose name is in comboBox4 (select port)
                             this->serialPort1->PortName=this->comboBox4->Text;
                             //open the port
                             this->serialPort1->Open();


                             // sending
                             String^ name_ = this->serialPort1->PortName;
                             String^ sampling_period_ = this->comboBox5->Text;

                             String^ days_ = this->comboBox2->Text;

                             String^ hours_ = this->comboBox3->Text;

                             String^ minutes_ = this->comboBox1->Text;

                             String^ start_ = this->textBox1->Text;

                             String^ end_ = this->textBox2->Text;

                             //send data to setup timer on the microcontroller
                             this->serialPort1->WriteLine(sampling_period_);
                             this->serialPort1->WriteLine(days_);
                             this->serialPort1->WriteLine(hours_);
                             this->serialPort1->WriteLine(minutes_);
                             // send slave addresses
                             this->serialPort1->WriteLine(start_);
                             this->serialPort1->WriteLine(end_);


                             // receiving
                             int rec[100][8];
                             for (int i=0;i<sizeof(rec[0]);i++){
                                 for (int j=0;j<sizeof(rec);j++){
                                     rec[i][j]=int::Parse(this->serialPort1->ReadLine());
                                 }
                             }
                             myfile<<"ADC1"<<","<<"ADC2"<<","<<"ADC3"<<","<<"ADC4"<<","<<"ADC5"<<","<<"ADC6"<<","<<"ADC7"<<","<<"ADC8"<<endl;
                             for (int i=0;i<sizeof(rec[0]);i++){
                                 for (int j=0;j<sizeof(rec);j++){
                                     myfile<<rec[i][j]<<",";
                                 }
                                 myfile<<endl;
                             }



                         }
                         else{
                             this->textBox1->Text="Warning: port is busy or isn't open";
                             this->textBox2->Text="Warning: port is busy or isn't open";
                         }
                     }
                        catch(UnauthorizedAccessException^){
                            this->textBox1->Text="Unauthorized access";
                            this->textBox2->Text="Unauthorized access";
                        }
                     }

             }
    private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void label7_Click(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {

             }
    private: System::Void label8_Click(System::Object^  sender, System::EventArgs^  e) {
             }
    };
    }

我希望在這里獲得幫助或至少獲得任何有用的信息。

提前致謝。

#include <fstream>
std::ofstream myfile;
    myfile.open("Data.txt");

最后一行是一條語句,該語句只能在函數內部發生,而不能在編寫時出現在名稱空間范圍內(即,在任何函數體外部)。

44): error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(244): error C2039: 'YesNoMaybe' : is not a member of '`global namespace''

此錯誤可能無法由您顯示的代碼引起,該代碼未提及vc_attribtuesYesNoMaybe 如果將代碼簡化到顯示問題所需的最低限度並發布與代碼匹配的錯誤,則將獲得更好的答案。

我不確定您希望從sizeof(rec)獲得什么,但可能不是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM