【解決方法】fwriteを使用してファイルに書き込む方法は?


私は C++ 98 の初心者です。 ファイル を使用した C++ のデータ型 ネットビーンズ 8.0. ここにそれを示す私のコードがあります ロード フォーム内のデータ

こちらが 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);
}

これがフォームを更新するための私のコードです

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

}

書いてない 文字列、IP、フロート と int ですか? このテキストファイルを書き込むにはどうすればよいですか?

私が試したこと:

このテキストファイルを書き込むにはどうすればよいですか?

simple.conf

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

しかし、それは機能しません 書き込み

ストリング
知財
浮く

解決策 1

fwrite 呼び出しパラメーターが正しくありません:

C++
fwrite(name, sizeof(name) , sizeof(name) , fptr);

つまり、50 文字を 50 回書こうとすることになります。 そのはず:

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

そして、残りの呼び出しについても同じことが言えます。

コメント

タイトルとURLをコピーしました