visual c++ fstream error
visual c++ fstream error
首先,我是一名硬件程序员(HDL,uC编程),几天前我的老板要求我创建一个可视化C++界面来控制uC。我以前从未使用过视觉C++,我的软件编程技能充其量是中级的。但是,我只需要到星期二才能使界面正常工作,因此由于我的时间框架,我不得不寻找示例并做同样的事情。因此,如果我问任何明显和愚蠢的问题,请原谅我。
在我的代码中,我必须将存储在数组中的值移动到 CSV 文件。所以我必须用逗号分隔我的值......但是,为了创建CSV文件,我必须使用fstream(据我目前所了解的内容)。每当我使用
#include <fstream>
我收到大量错误,例如:
1>c:program files (x86)microsoft visual studio 10.0vcincludecodeanalysissourceannotations.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.0vcincludecodeanalysissourceannotations.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.0vcincludecodeanalysissourceannotations.h(244): error C2039: 'YesNoMaybe' : is not a member of '`global namespace''
此错误不可能来自您显示的代码,其中没有提到vc_attribtues
或YesNoMaybe
。 如果将代码简化到显示问题所需的最低限度,并发布与代码匹配的错误,您将获得更好的答案。
我不确定你期望从sizeof(rec)
得到什么,但这可能不是你想要的。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 在fstream期间忽略空行
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 使用fstream对txt文件中的数字进行循环
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 使 std::fstream 写入文件末尾,但从头开始读取
- 从不同目录执行时"symbol lookup error:",否则从构建目录执行时按预期工作
- 对象声明中的模板"error: expected expression"
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- visual c++ fstream error