I want achieve a function : when I clickt the "start" button,the textBrowser will be cleaned up immediately and then the textBrowser show something for user.But I use QtextBrowser::clear() ,it didn't work immediately.
//constructor
connect(StartBtn,SIGNAL(clicked()),this,SLOT(interfaceStart()));
bool MainWindow::interfaceStart()
{
ui->textBrowser->clear();// this line does not seem to work immediately before `while(fgets...)` loop is finished;
if(interfaceLine->text()!=QString("") && interfaceSpinBox->text()!="0"){
ui->textBrowser->insertPlainText(QString("Starting capture interface %1 \n\n").arg(interfaceLine->text()));//this line also doesn't work
std::string cc = std::string("tshark -i ")
+std::string((interfaceLine->text()).toLocal8Bit().data())
+std::string(" -a duration:")
+std::string(interfaceSpinBox->text().toLocal8Bit().data());
char buf[1024];
char const *command=cc.c_str();
std::cout<<command;
std::cout.flush(); // when i see this line work, console will output command.But textBroser is't cleaned up
FILE *ptr;
if((ptr=popen(command, "r"))!=NULL)
{
while(fgets(buf, 1024, ptr)!=NULL)
{
ui->textBrowser->insertPlainText(buf);
}
pclose(ptr);
ptr = NULL;
}
return true;
}
QMessageBox::warning(this,QString("Error"),QString("Interface %1 doesn't exist or time is 0!").arg(interfaceLine->text()));
return false;
}
I saw console print command first,but testBrowser isn't cleaned up.When popen() isfinished,testBrowser will be cleaned.Why?
How to make clear() work right now?Can i flush the textBrowser?
Related
I have a simple 'brut' big text file (20MB). I would like to show it in a TRichEdit. The problem is that it takes 6 seconds for the showing. I would like to put a progess bar in the bottom of the application to avoid this bad UX design.
My question is how to get the progress of the TRichEdit showing ? With the TRichEdit::LoadFromStream method, it goes from 0 to 100% fast (less than 1 second) but after the application wait 6 secondes during the first show.
I created this class FileStreamProgress with TFileStream inherit. I overide the TFileStream::Read()
int __fastcall FileStreamProgress::Read(void *Buffer, int Count)
{
__int64 previousPosition = this->Position;
int ret = TFileStream::Read(Buffer, Count);
if (this->Position == 0 || this->Position == this->Size || (previousPosition/128000) != (this->Position/128000)) {
ProgressCallBack(ProgressCallBackParam1, this->Position, this->Size);
}
return ret;
}
static void FileStreamProgress::ProgressCallBack(void*thiz, int i, int max)
{
TProgressBar* ProgressBar = (TProgressBar*)thiz;
if (ProgressBar)
{
if (max > 0)
{
ProgressBar->Position = int(i * 100 / max);
}
if (Application)
{
Sleep(1);
Application->ProcessMessages();
}
}
}
This is how I test it :
void MyApp::CreatePage(AnsiString filename)
{
ProgressBar->Visible = true;
FileStreamProgress::ProgressCallBackParam1 = (void*)this->ProgressBar;
TFileStream * stream = new FileStreamProgress(filename.c_str(), fmOpenRead);
TPageMemo* Page = new TPageMemo(this);
Page->Parent = PControl;
Page->PageControl = PControl;
MessageDlg("111",mtError,TMsgDlgButtons()<<mbOK,0);
Page->Texte->Lines->LoadFromStream(stream);
MessageDlg("222",mtError,TMsgDlgButtons()<<mbOK,0);
PControl->ActivePage = Page;
}
There are 7 secondes between the 2 message dialogs "111" and "222". And my progress bar wait 6 secondes at 100% (during the showing)
I tried to go deeper with the win32 API's SendMessage and Handle without the expected result.
At the end, I used a TMemo yesterday cuz it's a brut text. It's very fast (instant open) but some functions are missing like FindTextW(). I rewrote it. Thanks
http://docwiki.embarcadero.com/RADStudio/Rio/en/Memo_and_Rich_Edit_Controls
was curious so I tested the TRichEdit and came up with this:
//---------------------------------------------------------------------------
void load_text(TRichEdit *re,AnsiString filename,TProgressBar *pb)
{
// variables
int hnd,adr,siz,len,i;
const int _buf=128*1024; // buffer size
AnsiString s,s0;
char buf[_buf+1];
// open file and detect size
hnd=FileOpen(filename,fmOpenRead); if (hnd<0) return;
siz=FileSeek(hnd,0,2);
FileSeek(hnd,0,0);
// prepare progress bar
pb->Position=0;
pb->Max=siz;
pb->Visible=true;
// process txt file by chunks
for (s0="",adr=0;adr<siz;)
{
// update progress bar and GUI
pb->Position=adr;
Application->ProcessMessages();
// load chunk
len=FileRead(hnd,buf,_buf);
adr+=len; buf[len]=0;
// handle cutted lines by chunk size
s=s0; s0="";
// ignore last 2 lines for chunks (except last chunk)
if (len==_buf)
{
// skip last line
for (i=len-1;i>=0;i--)
if ((buf[i]==13)||(buf[i]==10))
{
i--;
if (i>=0)
if (buf[i]!=buf[i+1]) // different eol code to ignore empty line
if ((buf[i]==13)||(buf[i]==10)) // eol code
i--;
break;
}
// skip another line to avoid inserting empty line if eol is cutted
for (;i>=0;i--)
if ((buf[i]==13)||(buf[i]==10))
{
s0+=buf+i+1; // copy last 2 lines into s0
i--;
if (i>=0)
if (buf[i]!=buf[i+1]) // different eol code to ignore empty line
if ((buf[i]==13)||(buf[i]==10)) // eol code
i--;
i++; if (i<0) i=0; buf[i]=0; // end of string
break;
}
}
// last chunk ignore last eol
else{
// skip last line
i=len-1;
if ((buf[i]==13)||(buf[i]==10)) // eol code
{
i--;
if (buf[i]!=buf[i+1]) // different eol code to ignore empty line
if ((buf[i]==13)||(buf[i]==10)) // eol code
i--;
i++; if (i<0) i=0; buf[i]=0; // end of string
}
}
// add actual chunk
s+=buf;
re->Lines->Add(s);
}
// tidy up
pb->Visible=false;
FileClose(hnd); hnd=-1;
}
//---------------------------------------------------------------------------
Looks like it works without the ending pause you describe however that might be related to version of IDE/VCL/compiler I am using BDS2006 Turbo C++. When Tested on ~23 MByte STL file the load time is ~10sec (TMemo takes twice of that god know why)...
The saved file (while PlainText=true) is identical to the loaded one so the code should be correct.
Here animated GIF of preview:
while used like this:
void __fastcall TForm1::FormActivate(TObject *Sender)
{
//tbeg();
load_text(re_txt,"in.txt",pb_progress);
//tend();
//Caption=tstr();
re_txt->Lines->SaveToFile("out.txt");
}
where pb_progress is the TProgressBar and and re_txt is the TRichEdit.
As you can see no callback is needed ...
PS. If you want to measure the time like I did (the commented lines) the implementations of tbeg/tend/tstr functions are here:
performance.h
I am using ifstream to open a file and read line by line and print to console.
Now, I also want to make sure that if the file gets updated, it reflects. My code should handle that.
I tried setting fseek to end of the file and then looking for new entries by using peek. However, that did not work.
Here's some code I used
bool ifRead = true;
while (1)
{
if (ifRead)
{
if (!file2read.eof())
{
//valid file. not end of file.
while (getline(file2read, line))
printf("Line: %s \n", line.c_str());
}
else
{
file2read.seekg(0, file2read.end);
ifRead = false;
}
}
else
{
//I thought this would check if new content is added.
//in which case, "peek" will return a non-EOF value. else it will always be EOF.
if (file2read.peek() != EOF)
ifRead = true;
}
}
}
Any suggestions on what could be wrong or how I could do this.
Hi guys i use this code to find the line included seta r_fullscreen "0" and if the value for this line is 0 return MessageBox but my question is if the value of seta r_fullscreenis "0" so how i can replace this value to "1" in this line ?
ifstream cfgm2("players\\config_m2.cfg",ios::in);
string cfgLine;
Process32First(proc_Snap , &pe32);
do{
while (getline(cfgm2,cfgLine)) {
if (string::npos != cfgLine.find("seta r_fullscreen")){
if (cfgLine.at(19) == '0'){
MessageBox(NULL,"run in full Screen mod.","ERROR", MB_OK | MB_ICONERROR);
...
You can use std::string::find() and std::string::replace() to do this. After you have located the line containing the configuration specifier seta r_fullscreen you can do something like the following.
std::string::size_type pos = cfgLine.find("\"0\"");
if(pos != std::string::npos)
{
cfgLine.replace(pos, 3, "\"1\"");
}
You should not assume that the configuration value "0" is at a specific offset as there may be additional spaces between r_fullscreen and "0".
After seeing your additional comments you need to update the configuration file after the changes have been made. The changes you make to the string only apply to the copy in memory and are not automatically saved to the file. You will need to save each line after it has been loaded and changed and then save the updates out to the file. You should also move the process of updating the config file outside of do/while loop. If yo don't you will read/update the file for each process you check.
The example below should get you started.
#include <fstream>
#include <string>
#include <vector>
std::ifstream cfgm2("players\\config_m2.cfg", std::ios::in);
if(cfgm2.is_open())
{
std::string cfgLine;
bool changed = false;
std::vector<std::string> cfgContents;
while (std::getline(cfgm2,cfgLine))
{
// Check if this is a line that can be changed
if (std::string::npos != cfgLine.find("seta r_fullscreen"))
{
// Find the value we want to change
std::string::size_type pos = cfgLine.find("\"0\"");
if(pos != std::string::npos)
{
// We found it, not let's change it and set a flag indicating the
// configuration needs to be saved back out.
cfgLine.replace(pos, 3, "\"1\"");
changed = true;
}
}
// Save the line for later.
cfgContents.push_back(cfgLine);
}
cfgm2.close();
if(changed == true)
{
// In the real world this would be saved to a temporary and the
// original replaced once saving has successfully completed. That
// step is omitted for simplicity of example.
std::ofstream outCfg("players\\config_m2.cfg", std::ios::out);
if(outCfg.is_open())
{
// iterate through every line we have saved in the vector and save it
for(auto it = cfgContents.begin();
it != cfgContents.end();
++it)
{
outCfg << *it << std::endl;
}
}
}
}
// Rest of your code
Process32First(proc_Snap , &pe32);
do {
// some loop doing something I don't even want to know about
} while ( /*...*/ );
I'm sorry, it would be extremely difficult to make a fully reproducible version of the error --- so please bare with my schematic code.
This program retrieves information from a web page, processes it, and saves output to an ASCII file. I also have a 'log' file (FILE *theLog---contained within a Manager object) for reporting errors, etc.
Some background methods:
// Prints string to log file
void Manager::logEntry(const string lstr) {
if( theLog != NULL ) { fprintf(theLog, "%s", lstr.c_str()); }
}
// Checks if file with given name already exists
bool fileExists(const string fname) {
FILE *temp;
if( temp = fopen(fname.c_str(), "r") ) {
fclose(temp);
return true;
} else { return false; }
}
// Initialize file for writing (some components omitted)...
bool initFile(FILE *&oFile, const string fname) {
if(oFile = fopen(fname.c_str(), "w") ) { return true; }
else { return false; }
}
The stuff causing trouble:
// Gets data from URL, saves to file 'dataFileName', input control flag 'foreCon'
// stu is some object that has string which i want
bool saveData(Manager *man, Stuff *stu, string dataFileName, const int foreCon) {
char logStr[CHARLIMIT_LARGE]; // CHARLIMIT_LARGE = 2048
sprintf(logStr, "Saving Data...\n");
man->logEntry( string(logStr) ); // This appears fine in 'theLog' correctly
string data = stu->getDataPrefixStr() + getDataFromURL() + "\n"; // fills 'data' with stuff
data += stu->getDataSuffixStr();
if( fileExists(dataFileName) ) {
sprintf(logStr, "save file '%s' already exists.", dataFileName.c_str() );
man->logEntry( string(logStr) );
if( foreCon == -1 ) {
sprintf(logStr, "foreCon = %d, ... exiting.", foreCon); // LINE 'A' : THIS LINE ENDS UP IN OUTPUT FILE
tCase->logEntry( string(logStr) );
return false;
} else {
sprintf(logStr, "foreCon = %d, overwriting file.", foreCon); // LINE 'B' : THIS LINE ENDS UP IN LOG FILE
tCase->logEntry( string(logStr) );
}
}
// Initialize output file
FILE *outFile;
if( !initFile(outFile, dataFileName) ) {
sprintf(logStr, "couldn't initFile '%s'", dataFileName.c_str());
tCase->logEntry( string(logStr) );
return false;
}
fprintf(outFile, "%s", data.c_str()); // print data to output file
if( fclose(outFile) != EOF) {
sprintf(logStr, "saved to '%s'", dataFileName.c_str());
tCase->logEntry( string(logStr) );
return true;
}
return false;
}
If the file already exists, AND 'int foreCon = -1' then the code should print out line 'A' to the logFile. If the file exists and foreCon != -1, the old file is overwritten with data. If the file doesn't exist, it is created, and the data is written to it.
The result however, is that a broken up version of line 'A' appears in the data file AND line 'B' is printed in the log file!!!!
What the data file looks like:
.. exiting.20130127 161456
20130127 000000,55,17,11,0.00
20130127 010000,54,17,11,0.00
... ...
The second line and onward look correct, but there is an extra line that contains part of line 'A'.
Now, the REALLY WEIRD PART. If I comment out everything in the if( foreCon == -1) { ... } block, then the data file looks like:
%d, ... exiting.20130127 161456
20130127 000000,55,17,11,0.00
20130127 010000,54,17,11,0.00
... ...
There is still an extra line, but it is the LITERAL CODE copied into the data file.
I think there is a poltergeist in my code. I don't understand how any of this could happen.
Edit: I've tried printing to console the data string, and it gives the same messed up values: i.e. %d, ... exiting.20130127 161456 - so it must be something about the string instead of the FILE *
Answer based on your latest comment:
getDataPrefixStr() ends up returning a string which starts with
something like string retStr = COMCHAR + " file created on ..."; such
that const char COMCHAR = '#';. Could the COMCHAR be the problem??
You can't add characters and string literals (which are arrays of char, not strings) like that.
You're adding 35 (the ASCII for "#") to the address of " file created on ... ", i.e. getDataPrefixStr() is whatever starts 35 characters from the start of that string. Since all literal strings are stored together in the same data area, you'll get strings from the program in the output.
Instead, you cold do
const string COMCHAR = "*";
string retStr = COMCHAR + " file created on ...";
It could be that logStr is too short and that it is causing data to be overwritten in other buffers (did you double check CHARLIMIT_LARGE?). You can diagnose this by commenting all writes to logStr (sprintf) and see if data is still corrupted. In general, your code is vulnerable to this if a user can set dataFileName (to be a very long string); use snprintf or ostringstream instead.
Otherwise, I would guess that either stu->getDataPrefixStr() or getDataFromURL() are returning corrupted results or return type char* instead of string. Try printing these values to the console directly to see if they are corrupted or not. If they return a char*, then data = stu->getDataPrefixStr() + getDataFromURL() will have undefined behavior.
if( temp = fopen(fname.c_str(), 'r') ) {
should be
if( temp = fopen(fname.c_str(), "r") ) {
Hello my question is why the following function fails to delete the file whose name is specified in dir1;
I use the function remove but it seems that there is some kind of a problem with it.
Please help me.
#include <stdio.h>
void test(char* dir1,char* dir2)
{
FILE * file1,* file2;
file1=fopen(dir1,"r");
file2=fopen(dir2,"w");
if(!file1){ return;}
int inpch;
char* string = new char[10];
string[9]='\0';
int br=0;
do
{
while((inpch=fgetc(file1))!=EOF)
{
string[br]=char(inpch);
br++;
if(br==9)break;
}
if(br!=9)
{
string[br]='\0';
fputs(string,file2);
return;
}
else
{
fputs(string,file2);
br=0;
}
}while(true);
fclose(file1);
remove(dir1);/// I DON"T UNDERSTAND WHY IT DOESN"T DELETE THE FILE.
fclose(file2);
}
I guess at some point before exiting the do-while loop, the following if condition becomes true, and the function returns before it reaches to the end of the function, without even calling the remove function.
if(br!=9)
{
string[br]='\0';
fputs(string,file2);
return; //<------------ here you're returning!
}
Did you want to write return or break? Looks like its there the problem lies.
Why don't you check for the return value and error code (errno) that tells you exactly why the function didn't succeed?
Replace your remove call with this :
if( remove( "myfile.txt" ) != 0 )
perror( "Error deleting file" );
else
puts( "File successfully deleted" );
and it should tell you what happened.