[英]C++ Trouble reading and writing to text file using the fstream object
[英]Trouble with writing and reading using fstream (windows forms, C++/CLI)
我正在創建一個小程序來從串行端口接收(可能)多路復用的串行數據,但是在涉及讀取和寫入我的輸出文件的功能時遇到了麻煩。 使用 Windows 窗體,一切都可以很好地編譯。
這是應該發生的事情:[下面的代碼片段]
在程序開始時,創建一個文件並插入:
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();
}
為什么不使用string::find
來找出 DC1、DC2、DC3?
看看字符串庫。
順便說一句,我無法對您的問題發表評論。
你應該做這樣的事情:
#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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.