I have an .ini file of this form:
#init file:
[files]
fileAmount=2
file1=string1, string2, 0
file2=string1, string2, 1
//cpp file:
settings=new QSettings(QString(":resources/configuration"), QSettings::IniFormat);
int n=set("files/fileAmount").toInt();
for(int i=1; i<=n; i++){
QStringList list=settings->value("files/file"+QString::number(i)).toStringList();
out<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl;
}
//output:
string1 string2 0
string1 string2 1
Is there any way to set whitespaces as delimiter for QStringList instead of komma?
i.e have this file:
#init file:
[files]
fileAmount=2
file1=string1 string2 0
file2=string1 string2 1
and get the same output?
Ini File:
C:\Users\username\AppData\Roaming\MyOrg\MyApp.ini
[files]
file1=string1 string2 0
Sample code for space delimited stringlist in ini file:
#include <QCoreApplication>
#include <QStringList>
#include <QSettings>
#include <QDebug>
void readSettings();
void writeSettings();
QStringList list;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSettings::setDefaultFormat(QSettings::IniFormat);
qApp->setApplicationName("MyApp");
qApp->setOrganizationName("MyOrg");
readSettings();
qDebug() << list;
writeSettings();
return a.exec();
}
void readSettings()
{
// read a space delimited value from QSettings string
// into a stringlist
QSettings s;
s.beginGroup("files");
QString temp = s.value("file1","string1 string2 0").toString();
list << temp.split(" ");
s.endGroup();
}
void writeSettings()
{
// write a space delimited value from QStringList
QSettings s;
s.beginGroup("files");
s.setValue("file1",list.join(" "));
s.endGroup();
}
Output:
("string1", "string2", "0")
Hope that helps.
Related
I have one csv file in which 3 column and 866300 lines. I have try to write this data into other csv file. when i try to write it has write 866248 lines in file after that remaining 52 lines are not write in file. what is the problem I do not understand it. I have try to debug this problem using print that data on console then it has print till last line on the console. only the problem in write the data in file.
#include <QCoreApplication>
#include <QFile>
#include <QStringList>
#include <QDebug>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile file("C:/Users/hello/Downloads/hello.csv");
QFile write("new_data.csv");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug()<<file.errorString();
return 1;
}
if(!write.open(QIODevice::WriteOnly |QIODevice::Append))
{
qDebug()<<file.errorString();
return 1;
}
QTextStream out(&write);
QStringList data;
while (!file.atEnd())
{
QString line = file.readLine();
data = line.split(',');
if (data[0]=="v1")
{
out<<line;
continue;
}
else
{
int seq = (data[0].toInt())-1;
QString str = QString::number(seq)+","+data[1]+","+data[2].trimmed();
qDebug()<<str;
out<<str<<"\n";
}
}
return a.exec();
}
please help.
Please close the file before the return a.exec(); this line and after the while loop. add this line below line.
write.close();
I have an Xml file:
<?xml version="1.0" encoding="utf-8" ?>
<AppConfig>
<DefaultCulture></DefaultCulture>
<StartupMessageTitle></StartupMessageTitle>
<StartupMessageText></StartupMessageText>
<StartupMessageIcon>Information</StartupMessageIcon>
<DefaultProfileSettings>
<DriverName>wia</DriverName>
<UseNativeUI>false</UseNativeUI>
<IconID>0</IconID>
<MaxQuality>false</MaxQuality>
<AfterScanScale>OneToOne</AfterScanScale>
<Brightness>0</Brightness>
<Contrast>0</Contrast>
<BitDepth>C24Bit</BitDepth>
<PageAlign>Left</PageAlign>
<PageSize>Letter</PageSize>
<Resolution>Dpi100</Resolution>
<PaperSource>Glass</PaperSource>
</DefaultProfileSettings>
<!--
<AutoSaveSettings>
<FilePath></FilePath>
<ClearImagesAfterSaving>false</ClearImagesAfterSaving>
<Separator>FilePerPage</Separator>
</AutoSaveSettings>
-->
</AppConfig>
I need to check if root elements "AutoSaveSettings" exists in xml using qt c++ . if the root elements exists then remove the commented line before and after the Auto save settings? How can we do it in qt c++. How do I perform this operation in c++.Check if exists start element or root element
#include <QtCore>
#include <QtXml/QDomDocument>
#include <QDebug>
#include <QFile>
#include <QXmlStreamReader>
bool elementExists(const QFile &file, const QString &elementName)
{
QXmlStreamReader reader(&file);
while (reader.readNextStartElement())
{
if(reader.name() == elementName)
{
return true;
}
}
return false;
}
int main(int argc,char *argv[])
{
QCoreApplication a(argc,argv);
QDomDocument document;
QFile file = "C:/Program Files (x86)/NAPS2/appsettings.xml";
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug()<<"Failed to open the file";
return -1;
}
else{
if(!document.setContent(&file))
{
qDebug()<< "Failed to load Document";
return -1;
}
file.close();
}
elementExists(file,"AutoSaveSettings");
qDebug()<< "Finished";
return a.exec();
}
Try something like this :
bool elementExists( QFile & file, const QString & elementName){
QXmlStreamReader reader (&file);
while (reader.readNextStartElement()){
if(reader.name() == elementName) return true;
//elementName should be "AutoSaveSettings"
}
return false;
}
Edit : an alternative way could be using QDomDocument
Not recomanded because QDomDocument is not actively maintained anymore
bool elementExists( QFile & file,const QString & elementName){
QDomDocument reader;
reader.setContent(&file, true);
QDomNodeList elementList = reader.elementsByTagName(elementName);
return elementList.size()>0;
}
Im trying to read from csv file into another file. There aren't any errors, I just don't understand why it is not writing into the file. Any help is appreciated. Im very new to QT.
QString arr[581][6];
while (!file.atEnd()) {
QByteArray line = file.readLine();
for (int i=0; i<581; i++){
for(int j=0; j<6; j++){
arr[i][j]=line;
}
}
}
QString Hfilename="c:\Data.txt";
QFile fileH( Hfilename );
if ( fileH.open(QIODevice::ReadWrite) )
{
QTextStream stream( &fileH );
for (int i=0; i<581; i++){
for(int j=0; j<6; j++){
stream<<arr[i][j]<<endl;
}
}
}
Based on the code you provided, please find a working example for the write step (you specified that the read step is alrady working):
#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString arr[3];
arr[0] = "xxxxxx";
arr[1] = "yyyy";
arr[2] = "zzzz";
QString Hfilename="/home/jhondoe/toto.txt";
QFile fileH( Hfilename );
if ( fileH.open(QIODevice::ReadWrite) )
{
QTextStream stream( &fileH );
for (int i=0; i<3; i++){
stream << arr[i] << endl;
}
}
return a.exec();
}
What changed:
I (my user account) has sufficient rights to write into the output file
If you work on Windows, as #igor-tandetnik said, you have to use double backslashes as separator. You can also use QDir::separator() as native separator for your file path.
I have two FASTA files:
file1.fasta
>foo
ATCGGGG
>bar
CCCCCC
file2.fasta
>qux
ATCGGAAA
What I want to do now is to concatenating them into one file that results:
>foo
ATCGGGG
>bar
CCCCCC
>qux
ATCGGAAA
Thus preserving the name of each sequence that started with ">".
Currently my code below replace that name with index, namely:
>0
ATCGGGG
>1
CCCCCC
>0
ATCGGAAA
What's the right way to modify my code below?
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include<stdio.h>
#include<string>
using namespace std;
#define MAX_LINE_SIZE 1024
int mk_joint_file(char *ctrlFile, char *tgtFile, char *outFile){
char s[MAX_LINE_SIZE];
FILE *ofp = fopen(outFile,"w");
FILE *cfp = fopen(ctrlFile,"r");
FILE *tfp = fopen(tgtFile,"r");
// char *p;
int flg=false;
int line=0;
while(fgets(s,MAX_LINE_SIZE,cfp) != NULL){
if(s[0]=='>'){
flg=true;
fprintf(ofp,">%d\n",line);
line++;
}else{
if(flg==true){
fprintf(ofp,"%s",s);
}
flg=false;
}
}
flg=false;
line=0;
while(fgets(s,MAX_LINE_SIZE,tfp) != NULL){
if(s[0]=='>'){
flg=true;
fprintf(ofp,">%d\n",line);
line++;
}else{
if(flg==true)
fprintf(ofp,"%s",s);
flg=false;
}
}
fclose(cfp);
fclose(tfp);
fclose(ofp);
return(0);
}
int main(int argc, char **argv)
{
string ifname_control = argv[1];
string ifname_target = argv[2];
string ofname = "newjoin.txt";
mk_joint_file((char *)ifname_control.c_str(), (char *)ifname_target.c_str(), (char *)ofname.c_str());
}
Is it any harder than just changing these lines
fprintf(ofp,">%d\n",line);
to
// TODO check fgets() handling of EOL - may not need the \n
fprintf(ofp, %s\n", s);
just change line 29 and 40 to
fprintf(ofp,"%s",s);
I have written a source code like:
int main(int argc, char *argv[]) {
QFile File (directory + "/File");
if(File.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream Stream (&File);
QString FileText;
do
{
FileText = Stream.readLine();
QString s = "start";
QString e = "end here";
int start = FileText.indexOf(s, 0, Qt::CaseInsensitive);
int end = FileText.indexOf(e, Qt::CaseInsensitive);
if(start != -1){ // we found it
QString y = FileText.mid(start + s.length(), (end - (start + s.length())));
qDebug() << y << (start + s.length()) << (end - (start + s.length()));
}
}
My problem here is, that the int end = FileText.indexOf(e, Qt::CaseInsensitive);
with QString e = "end here"; is just found when there are exactly three spaces between the word "end" and "here". This is problematical, because in the text I read the spaces between these two words will surely differ from time to time. Furthermore I need to write both words "end" and "here". I tried to reduce the problem to the basis and hope someone has an idea/solution.
Reduce the number of inter-spaces to 1 using QString::simplified() method.
You could also try QRegExp:
#include <QDebug>
#include <QString>
#include <QRegExp>
int main()
{
QString text("start ABCDE1234?!-: end here foo bar");
// create regular expression
QRegExp rx("start\\s+(.+)\\s+end\\s+here", Qt::CaseInsensitive);
int pos=0;
// look for possible matches
while ((pos=rx.indexIn(text, pos)) != -1) {
qDebug() << rx.cap(1); // get first match in (.+)
pos+=rx.matchedLength();
}
return 0;
}