簡體   English   中英

fread 正在讀取文件中的垃圾

[英]fread is reading garbage in file

我正在處理我的任務,並在 C++ 中遇到了 fread() 的問題。 當我修改文件中的名稱時,它會根據需要完美地修改它,但是當我嘗試讀取文件之后會出現問題,它會讀取整個文件,但之后它並沒有停止,它總共運行了 146 次,而只有3個名字。

我的代碼:-

#include <bits/stdc++.h>
using namespace std;

struct person{
    int id; 
    string fname; 

}s;

void write(){
    FILE *outfile;
    struct person input; 
    int num,ident;
    string sname[] = {"a","b","c"};

    outfile = fopen ("C:\\Users\\Amritesh\\Desktop\\students.txt","wb");

    if (outfile == NULL) 
    { 
        fprintf(stderr, "\nError opend file\n"); 
        exit (1); 
    } 

    scanf("%d",&num);

    for(int i=0;i<num;i++){

        s.fname = sname[i];
        cin >> s.id;

        fwrite (&s, sizeof(s), 1, outfile);
    }
    fclose(outfile);
}

void read(){

    FILE *file1;
    int i=0;
    file1 = fopen ("C:\\Users\\Amritesh\\Desktop\\students.txt","r");

    while(fread(&s, sizeof(s), 1, file1) == 1) {    

        cout << "ID " << s.id << "  Name " <<s.fname << endl;
    }

    fclose (file1); 


} 

void modify(){
    FILE *file;
    file = fopen ("C:\\Users\\Amritesh\\Desktop\\students.txt","r+");

    while(fread(&s, sizeof(s), 1, file)) {

        if(s.fname == "a"){
            s.fname = "d";
            fseek(file,-sizeof(s),SEEK_CUR);
            fwrite (&s, sizeof(s), 1,file);
        }
    }

    fclose (file); 
}

int main(){
    write();
    modify();
    read();
}

編輯代碼:-

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct person 
{ 
    int id; 
    string fname; 
}s,temp; 



void read() 
{ 
    int num;
    
    ifstream fin;
    fin.open("C:\\Users\\Amritesh\\Desktop\\student.txt",ios::in);
    fin.seekg(0,ios::beg);
    

    //scanf("%d",&num);

    while(fin){

        cout << s.fname << s.id << endl;
    }

    fin.close();
}

void write(){

    int i=0;
    ofstream fout;

    fout.open("C:\\Users\\Amritesh\\Desktop\\student.txt");
    
    
    while(i!=2) {   
        cin >> s.id >> s.fname;
        fout << "ID " << s.id << "  Name " <<s.fname << endl;
        i++;
    }

    
    fout.close();
    
} 

void modify(){

    fstream mod;
    mod.open ("C:\\Users\\Amritesh\\Desktop\\student.txt");
    
    while(mod) {

        if(s.fname == "a"){
            s.fname = "d";
            mod.seekg(-sizeof(s),ios::cur);
            mod << s.fname;
        }
    }

    
    mod.close();
}

int main(){
    
    write();
    read();
    modify();
    
}

感謝您的任何回答!

以下是基於我們討論的三個想法。 我將從讀寫一個person的免費函數 object 開始,因為看起來你現在正處於那個階段。 我將繼續在您的person class 中添加成員函數,並以添加 stream 運算符結束以方便。

免費(非成員) read函數的write

#include <iostream>
#include <string>
#include <fstream>

struct person {
    int id; 
    std::string fname; 
};

std::ostream& write(std::ostream& os, const person& p) {
    os << p.id << ',' << p.fname << '\n'; // stream out the properties of a person
    return os; // look at the next example for an alternative doing the same thing
}

std::istream& read(std::istream& is, person& p) {
    // extract "id" and if it succeeds, check if the next char is a , char
    if(is >> p.id && is.peek() == ',') { 
        is.ignore();               // step over the , char
        std::getline(is, p.fname); // read the rest of the line into p.fname
    } else {
        // we didn't get id or the , char, so set the stream in a failed state
        is.setstate(is.failbit);       
    }
    return is;
}

int main() {
    // write to file
    {
        std::ofstream file("C:\\Users\\Amritesh\\Desktop\\student.txt");
        person test1{10, "Foo Bar"};
        person test2{20, "Apa Bepa"};
        write(file, test1);
        write(file, test2);
    }
    // read from file
    {
        std::ifstream file("C:\\Users\\Amritesh\\Desktop\\student.txt");
        person test;

        while(read(file, test)) {
            std::cout << test.fname << '\n';
        }
    }
}

write read函數的person

#include <iostream>
#include <string>
#include <fstream>

struct person {
    int id; 
    std::string fname;

    std::ostream& write(std::ostream& os) const {
        // this does the same thing as in the first example
        return os << id << ',' << fname << '\n';        
    }

    std::istream& read(std::istream& is) {
        if(is >> id && is.peek() == ',') {
            is.ignore(); // step over the , char
            std::getline(is, fname);
        } else {
            is.setstate(is.failbit); // we didn't get id or the , char
        }
        return is;
    }    
};

int main() {
    // write to file
    {
        std::ofstream file("C:\\Users\\Amritesh\\Desktop\\student.txt");
        person test1{10, "Foo Bar"};
        person test2{20, "Apa Bepa"};
        test1.write(file);
        test2.write(file);
    }
    // read from file
    {
        std::ifstream file("C:\\Users\\Amritesh\\Desktop\\student.txt");
        person test;

        while(test.read(file)) {
            std::cout << test.fname << '\n';
        }
    }
}

stream算子支持的成員函數:

#include <iostream>
#include <string>
#include <fstream>

struct person {
    int id; 
    std::string fname;

    std::ostream& write(std::ostream& os) const {
        return os << id << ',' << fname << '\n';        
    }

    std::istream& read(std::istream& is) {
        if(is >> id && is.peek() == ',') {
            is.ignore(); // step over the , char
            std::getline(is, fname);
        } else {
            is.setstate(is.failbit); // we didn't get id or the , char
        }
        return is;
    }    
};

// stream operators calling member functions
std::ostream& operator<<(std::ostream& os, const person& p) { return p.write(os); }
std::istream& operator>>(std::istream& is, person& p) { return p.read(is); }

int main() {
    // write to file
    {
        std::ofstream file("C:\\Users\\Amritesh\\Desktop\\student.txt");
        person test1{10, "Foo Bar"};
        person test2{20, "Apa Bepa"};
        file << test1 << test2;  // calling operator<< for each person object
    }
    // read from file
    {
        std::ifstream file("C:\\Users\\Amritesh\\Desktop\\student.txt");
        person test;

        while(file >> test) {    // extract one person at a time using operator>>
            std::cout << test.fname << '\n';
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM