Home > Net >  How to write to file using fwrite?
How to write to file using fwrite?

Time:01-14

I am new in C 98 .I want to write to FILE data type in C using netbeans 8.0. Here is my code that show that load the data in form

Here is the main.cpp

    #include <QApplication>
    #include <newForm.h>
    int main(int argc, char *argv[]) {
    
        QApplication app(argc, argv);
        newForm *a = new newForm();
        a->show();
        return app.exec();
    }
    
    #include "newForm.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <QDebug>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    #define MAX 128
    int i, n = 2;
    
    char str[50], Name[50], Class[50], Grade[50], Section[50], Number[50], Total[50], var1[50], var2[50];
    FILE *fptr;
    int count = 0;
    
    newForm::newForm() {
        widget.setupUi(this);
        connect(widget.pushButton_1, SIGNAL(clicked()), this, SLOT(Load()));
        connect(widget.pushButton_2, SIGNAL(clicked()), this, SLOT(Update()));
    }
    
    void newForm::Load() {
        fptr = fopen("/root/Desktop/simple.conf", "r");
    
        if (fptr == NULL) {
            printf("Could not open file");
        }
        for (int i = 0; i < 10; i  ) {
            if (EOF == fscanf(fptr, "%s", var1)) {
                break;
            }
    
            if (EOF == fscanf(fptr, "%s", var2)) {
                break;
            }
    
            if (strcmp(var2, "Name") == 0) {
                sprintf(Name, "%s", var1);
                widget.lineEdit_1->setText(QString::fromStdString(Name));
            }
            if (strcmp(var2, "Class") == 0) {
                sprintf(Class, "%s", var1);
                widget.lineEdit_2->setText(QString::fromStdString(Class));
            }
            if (strcmp(var2, "Section") == 0) {
                sprintf(Section, "%s", var1);
                widget.lineEdit_3->setText(QString::fromStdString(Section));
            }      
            if (strcmp(var2, "Number") == 0) {
                sprintf(Number, "%s", var1);
                widget.lineEdit_4->setText(QString::fromStdString(Number));
            } 
            if (strcmp(var2, "Total") == 0) {
                sprintf(Total, "%s", var1);
                widget.lineEdit_5->setText(QString::fromStdString(Total));
            }
        }
        fclose(fptr);
    }

enter image description here

Here is my code for updating the form


#include <stdio.h>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <bits/stdc  .h>

#include <stdio.h>

const int StrSize = 49;
typedef char textStr[StrSize   1];

void newForm::Update(){
    fptr = fopen("/root/Desktop/simple.conf","r" );
    textStr str,name,section,number,class1,total,grade,var1,var2;    
   
    strcpy(name,     widget.lineEdit_1->text().toLocal8Bit());
    strcpy(class1,   widget.lineEdit_2->text().toLocal8Bit());   
    strcpy(section, widget.lineEdit_3->text().toLocal8Bit());
    strcpy(number,  widget.lineEdit_4->text().toLocal8Bit());
    strcpy(total,   widget.lineEdit_5->text().toLocal8Bit());

    const char * text = NULL;
    for (int i = 0; i < 10; i  ){
        if( EOF == fscanf(fptr, "%s", var1)){
            break;
        }

        if( EOF == fscanf(fptr, "%s", var2)){
            break;
        }
        const char * text = NULL;
        if(strcmp(var2,"Name") == 0){
           fwrite(name, sizeof(name) , sizeof(name) , fptr);
        }
        if(strcmp(var2,"Class") == 0){                      
            fwrite(class1, sizeof(class1) , sizeof(class1) , fptr);
        }if(strcmp(var2,"Name") == 0){
           fwrite(section, sizeof(section) , sizeof(section) , fptr);
        }
        if(strcmp(var2,"Section") == 0){                      
            fwrite(class1, sizeof(class1) , sizeof(class1) , fptr);
        }
        if(strcmp(var2,"Number") == 0){
            fwrite(grade, sizeof(grade) , sizeof(grade) , fptr);
        }
        if(strcmp(var2,"Total") == 0){
            fwrite(grade, sizeof(grade) , sizeof(grade) , fptr);
        }
        else if((strcmp(var2, "Name") != 0)  &&  (strcmp(var2, "Class") != 0)  &&  (strcmp(var2, "Grade") != 0) && strcmp(var2,"Number") !=0 && strcmp(var2,"Total") != 0) {
            fwrite(var1, sizeof(var1) , sizeof(var1) , fptr);
        }       
    }    
    fclose(fptr);

}

It is not writing string,IP,float and int at all ? How to make it write thw this text file ?

simple.conf

AAA     Name
192.168.9.33    Class
A10     Section
72.777      Number
100     Total

CodePudding user response:

Your approach to updating the file is dangerous and confusing. You should update the file completely, rewriting it from scratch.

First, you need to open the file for writing instead of reading. When you issue an fopen(...,"w"); it will truncate the file to zero length for you so you don't need to care about resizing it.

Then you can just use fprintf() instead of fwrite() to deal better with text outputs, like in the example below.

Note that I have NOT compiled the example below and the actual format you presented is actually confusing so adapt to it.

#include <fstream>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>

static void writeField( FILE* f, const char* name, const char* value ) {
    fprintf( f, "%s,%s\n", name, value );
}

void newForm::Update(){
    const char* filename = "/root/Desktop/simple.conf";
    FILE* fptr = fopen( filename,"w" );
    writeField( fptr, "Name",    widget.lineEdit_1->text().toLocal8Bit());
    writeField( fptr, "Class",   widget.lineEdit_2->text().toLocal8Bit());   
    writeField( fptr, "Section", widget.lineEdit_3->text().toLocal8Bit());
    writeField( fptr, "Number",  widget.lineEdit_4->text().toLocal8Bit());
    writeField( fptr, "Total",   widget.lineEdit_5->text().toLocal8Bit());

    fclose(fptr);
}
  • Related