gpt4 book ai didi

c++ - fread正在读取文件中的垃圾

转载 作者:行者123 更新时间:2023-12-02 10:11:29 25 4
gpt4 key购买 nike

我正在分配作业,并且在C++中遇到了fread()问题。当我修改文件中的名称时,它会根据需要进行完美地修改,但是在此之后尝试读取文件时会出现问题,它会读取整个文件,但在运行了146次之后并没有停止,而仅运行了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对象,因为看起来您现在就在这个阶段。为了方便起见,我将继续在person类中添加成员函数,最后添加流运算符。
免费(非成员)readwrite函数的示例:

#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';
}
}
}
read中制作 writeperson成员函数的示例:
#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';
}
}
}
流运算符支持的成员函数:
#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';
}
}
}

关于c++ - fread正在读取文件中的垃圾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63375702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com