【问题标题】:Trouble with writing and reading using fstream (windows forms, C++/CLI)使用 fstream(Windows 窗体、C++/CLI)写入和读取时出现问题
【发布时间】:2013-03-24 17:26:27
【问题描述】:

我正在创建一个小程序来从串行端口接收(可能)多路复用的串行数据,但在涉及读取和写入我的输出文件的功能时遇到问题。 一切都可以使用 Windows 窗体编译。

这是应该发生的事情:[下面的代码 sn-ps]

在程序开始时,会创建一个文件并将其插入:

DC1
DC2
DC3
DC4
JUNK:

~blah blah blah,COMport 的事情发生了,我们得到一个包含一条数据的字符串~

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

我遇到的问题是在第一次运行“WriteToFile”之后,文件行什么也没有出现,所以标记永远不会找到它的位置。 此外,即使在刷新文本文件后也没有任何变化的迹象(这是实现流的正确方法吗?)

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

//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();
}

【问题讨论】:

    标签: c++ winforms file c++-cli fstream


    【解决方案1】:

    为什么不使用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;
    }
    

    【讨论】:

    • 我必须构造我正在搜索的字符串。如果我使用带有不同第三位的“string::find”,它会变得更简单吗?
    • 您已经构建了字符串以正确搜索。我编辑了答案。
    • 如果有帮助,我不明白为什么不:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多