使用fstream(windows窗体,C++/CLI)编写和读取时遇到问题

问题描述:

我正在创建一个小程序,以便从串行端口接收(可能)多路复用的串行数据,但遇到与读取和写入输出文件。 一切都使用Windows窗体编译得很好。使用fstream(windows窗体,C++/CLI)编写和读取时遇到问题

这里是什么应该发生:下面的代码片段]

在节目的开始,一个文件被创建,该插入:

DC1 
DC2 
DC3 
DC4 
JUNK: 

〜等等等等,相称的东西发生并且我们得到一个包含一段数据的字符串〜

我们来到“WriteToFile”函数,它有一段数据传递给它。 (比如0.95) “NumberCheck”过滤所有非数字(但包括'。')部分(包含垃圾)的数据。 读取文件的每一行,将其长度加入计数并检查数据将进入哪个“区域”。如果它是正确的,请停止阅读,因为标记应指向该行末尾文件中的位置。 (FS是一个故障安全计数器,用于处理奇怪的事情)。然后我找到从那个位置到最后的文件长度,调整temp的大小以适应,用标记(包括那个位置)之后的内容填充temp,并且在标记处输出我的数据和temp ...理论上将数据插入到正确的地方。

我遇到的问题是在“WriteToFile”第一次运行后,fileline没有任何东西,所以标记从未找到它的位置。 此外,即使冲洗文本文件后没有显示出变化的迹象(是实现数据流的正确方法?)

下面是相关代码: 打开文件:

//opening file with the name in the textbox 
string myfilename = txtboxCONT; 
myfilename.append(".txt"); 
myfilename="test.txt"; //temporary name, DELETE BEFORE PUBLISH <<. 
fstream myfile(myfilename,ios::in | ios::out | ios::trunc); 
if (!myfile.is_open())   
    throw; 

//initial file set-up 
int i; 
for (i=1;i<=4;i++) 
    myfile << "DC" << i << "\n"; 
myfile << "JUNK:"; 
myfile.flush(); 

而且我的写作功能:

void WriteToFile(fstream &myfile, string& data, int zone) 
{ 
    data = NumberCheck(data); //filters out non-numeric data, returns "null" if nothing left. 
    if (data=="null")  //if the filter leaves no valid output, no need to write. 
     return; 

    myfile.seekg(0);  //set initial position at the start? 
    string fileline, srch, out; 
    stringstream ss; //for int to string conversion 
    ss << zone; 
    srch="DC"; 
    srch.append(ss.str()); 
    int fs=1, marker=0; 
    while(fs<=4)  //test each line if it beins with DC(zone) 
    { 
     getline(myfile, fileline); 
     marker = marker + fileline.length() + 1; 
     if (srch==fileline.substr(0,3)) 
      break; 
     fs++; 
    } 

    if (fs!=5) 
    { 
     //can't avoid overwriting so... this is gon' suck... 
     string temp; 
     myfile.seekg(0,ios::end); 
     int length = myfile.tellg(); 
     length = length - marker; 
     temp.resize(length); 
     myfile.seekg(marker); 
     myfile.read(&temp[0],temp.size()); 
     myfile.seekp(marker); 
     myfile << ',' << data << temp; 
    } 
    else 
    { 
     myfile.seekp(0,ios::end); 
     myfile << ',' << data; 
    } 
    myfile.flush(); 
    data.clear(); 
} 

所以,是啊... halp请吗?

如果有人可以发现任何其他危害或事情我搞砸了,请指出。

为什么不用string::find找出DC1,DC2,DC3?

看看string library

顺便说一句,我无法对您的问题发表评论。

你应该做这样的事情:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <sstream> 

using namespace std; 

void WriteToFile(fstream &myfile, string& data, int zone) 
{ 

    string fileline, srch; 
    stringstream ss; //for int to string conversion 
    ss << zone; 
    srch="DC"; 
    srch.append(ss.str()); 

    int offset; 
    while(!myfile.eof()) 
    { 
     getline(myfile, fileline); 
     if ((offset = fileline.find(srch, 0)) != string::npos) 
     { 
      cout << "My search " << srch << endl; 
     } 
    } 

    myfile.clear(); 
    myfile.seekg(0, ios::beg); 

    // Write here 

} 


int main() 
{ 
    string myfilename = "test"; 
    myfilename.append(".txt"); 
    myfilename="test.txt"; //temporary name, DELETE BEFORE PUBLISH <<. 
    fstream myfile(myfilename,ios::in | ios::out | ios::trunc); 
    if (!myfile.is_open())   
    throw; 

    //initial file set-up 
    int i; 
    for (i=1;i<=4;i++) myfile << "DC" << i << "\n"; 
    myfile << "JUNK:"; 
    myfile.flush(); 


    string write = "blalblablab"; 
    WriteToFile(myfile, write, 1); 
    WriteToFile(myfile, write, 2); 
    WriteToFile(myfile, write, 3); 

    WriteToFile(myfile, write, 4); 

    getchar(); 
    return 0; 
} 
+0

我必须构建我搜索的字符串。如果我用不同的第三位数字'string :: find',它会使事情变得更简单吗? – IamPancakeMan 2013-03-24 18:13:05

+0

您已经构建了您的字符串以正确搜索。我编辑了答案。 – billybob 2013-03-24 18:20:05

+0

如果有帮助,我不明白为什么:) – IamPancakeMan 2013-03-24 18:29:43