-1

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
gatcheca
  • 11
  • 3
  • 1
    If you want to write a text file, you don't use fwrite. – hobbs Jan 13 '23 at 02:26
  • 3
    Side note: [`#include ` is a no, no, no go](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h?r=Saves_AllUserSaves) – πάντα ῥεῖ Jan 13 '23 at 02:34
  • 1
    use fprintf to write text to a file – pm100 Jan 13 '23 at 02:34
  • 4
    also why use a 25 year old c++. And why not use c++ streams. And std::string too? – pm100 Jan 13 '23 at 02:35
  • For c++ `std::ofstream` is the preferable way. – πάντα ῥεῖ Jan 13 '23 at 02:35
  • 4
    Whichever C++ textbook taught you to use `` -- you should throw it away and get a different C++ textbook. If you copied that off some web site, without any explanation, don't visit that web site any more. If you saw this in some clown's Youtube video, unsubscribe from that channel, you're not learning proper C++. This is not a standard C++ header file, many C++ compilers don't have it and will not compile the shown code. – Sam Varshavchik Jan 13 '23 at 02:44
  • Do you know what opening the file in `"r"` mode means? – molbdnilo Jan 13 '23 at 06:49
  • "I am new in C++ 98" - that's an interesting one... joined that party a bit too late, huh? :-P – andreee Jan 13 '23 at 10:44
  • @pm100 Because 25 year old C++ performs as fast as C++23 and it has much less complexity. Sometimes like in high frequency trading those requirements are a must. – Something Something Jan 13 '23 at 18:28

1 Answers1

0

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);
}
Something Something
  • 3,999
  • 1
  • 6
  • 21