Cannot write to file in qt
-
void fileReceivefromServer()
{
char filename[256];
sockaddr_in p;
char rec[50] = "";
QFile file;
// QFileDevice x;
int size;
char bb[2];
char bf2[100];p = readFromSocketWithBlock(filename, 32); qDebug()<<"filename: " << filename <<Qt::endl ; // read size of file p = readFromSocketWithBlock(rec, sizeof(rec)); size = atoi(rec); qDebug()<<"size: " << size <<Qt::endl ; string v(filename); QString filepath(("C:/Users/hp/OneDrive/Documents/PrD/"+v).c_str()); qDebug()<<"filepath: " << filepath <<Qt::endl ; file.setFileName(filepath); // Check if file is opened if (!file.open(QIODevice::ReadWrite)) { qDebug()<< "Could not open file "; } else qDebug() << "file opened"; // QTextStream out(&file); QByteArray arr; QString filecontent=""; for (int i = size; i>0 ; i--) { memset(bb, 0, 2); readFromSocketWithBlock(bb, sizeof(bb)); QString ty(bb); filecontent+=ty; } filecontent+="\00"; arr.fromStdString(filecontent.toStdString()); file.openMode(); file.setTextModeEnabled(true); if (int u =file.write(arr) > 0) { qDebug()<<"Wrote to file "; } else qDebug()<<"can't write to file "; ; qDebug()<<"W3 \n"; file.close(); }
};
-
@sam12
Hello and welcome.As @ChrisW67 said, this is not a question, it is a paste of some code. If you have a question you must state what it is. If it is not doing what you want/expect, you must state what it does/does not do and how that compares with whatever you expected.
I don't mean to be rude, but your code is a terrible mishmash of C, C++,
std::
and Qt. You really should tidy it up and use things consistently. These days there is no longer any need to use C-type constructs,char []
s,memset()
, and half the other stuff.Your statement
if (int u =file.write(arr) > 0)
is not good, and does not do what you (probably) think it does.
Another problem comes from
arr.fromStdString(filecontent.toStdString());
What do you think this does? If you think it sets
arr
it does not. Consequently yourarr
remains empty --- test for that immediately above yourfile.write(arr)
statement.Finally I don't think your opening your file for
ReadWrite
and then writing to it does what you might be expecting it to do. If that file already exists before you open it, it may not have the content you expect after you have finished writing to it. -
@ChrisW67 Whenever I try to write to the file at the file.write() I get an error which states the inferior stopped because it received a signal from the operating system (Signal meaning. segmentation fault). The file exists and opens however when it writes the program crashes. I can't tell where the problem is I've been trying to debug and that's why my code doesn't seem clean and proper. I apologize for this. If you can help me figure out what can be wrong I will really appreciate it
-
Simply use a debugger and see where it crashes exactly.
-
@JonB has already pointed out that this:
arr.fromStdString(filecontent.toStdString());
does not do what you think it does. QByteArray::fromStdString() is a static function that returns a QByteArray.. and you are discarding the return value. Nevertheless,
arr
should remain a valid, empty QByteArray. Then you do this:if (int u =file.write(arr) > 0)
This time you try to capture the return value. I suggest you look at the prototype for QFile::write() (actually QIODevice::write()). Ask yourself:
- What type does this function return and where am I storing it?
- What warning message(s) did my compiler issue when it compiled this line?
- Are there other problems with this line?
-
@Christian-Ehrlicher I did it crashes exactly at File.write
-
@sam12 said in Cannot write to file in qt:
// Check if file is opened
You don't care whether the file was opened or not.
So, was the file opened before you wrote into it?