Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
    }
Here is the newForm.h

    
    #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);
    }


Form

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 the this text file ?

What I have tried:

How to make it write the this text file ?

simple.conf
AAA		Name        
192.168.9.33	Class 
72.777		Number   
A10	Section          
100		Total        



with data type like this.
AAA		Name          ----------------------->String/ 
192.168.9.33	Class ----------------------->I.p
72.777		Number    ----------------------->float
A10	Section           ----------------------->string
100		Total         ----------------------->int


fwrite(name, sizeof(name) , 1, fptr); // write the name once only

fwrite(Class , sizeof(Class ) , 1, fptr); // write the name once only

fwrite(Number, sizeof(Number) , 1, fptr); // write the name once only

fwrite(Section, sizeof(Section) , 1, fptr); // write the name once only

fwrite(Total, sizeof(Total) , 1, fptr); // write the name once only


but it is no working for fwrite

string
IP
float
Posted
Updated 13-Jan-23 1:55am
v9

1 solution

Your fwrite call parameters are incorrect:
C++
fwrite(name, sizeof(name) , sizeof(name) , fptr);

That means you will try to write 50 characters 50 times. It should be:
C++
fwrite(name, sizeof(name) , 1, fptr); // write the name once only

And the same goes for the remaining calls.

[edit]
You open the file "/root/Desktop/simple.conf" for reading, but you use the same file pointer in your fwrite calls. You read from the input file to var1, and var2, but you then try to write from name,section,number,class1,total and grade, but you have never read anything into those fields.
[/edit]
 
Share this answer
 
v2
Comments
CPallini 13-Jan-23 7:11am    
5.
Member 8840306 13-Jan-23 7:48am    
please see my edited question?
Richard MacCutchan 13-Jan-23 8:05am    
Please read the edit in my Solution above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900