所以,我有这个结构数组和一个文件,我需要将所有信息复制到结构中,我可以这样做吗?如果不是我怎么能这样做?
#include <stdio.h>
#include <iostream>
using namespace std;
typedef struct{
int x;
int y;
int z;
}coordinates;
int main(){
coordinates coordinate[100];
int i = 0;
FILE *f;
f = fopen("file.file","rb");
if(!f){
cout << "Error";
return 0;
}
while(!feof(f)){
fread(coordinate[i],sizeof(coordinates),1,f);
i++;
}
return 0;
}
答案 0 :(得分:1)
好吧,如果您使用的是C ++(顺便提一下C语言完全不同)。您只需为该类定义输入运算符。
struct coordinates{
int x;
int y;
int z;
// Personally I prefer serialization to a straight binary format.
// It is easy to read and validate by a human
// and not as brittle as a binary format when anything non
// trivial happens.
friend std::istream& operator>>(std::istream& str, coordinates& c)
{
return str >> c.x >> c.y >> c.z;
}
friend std::ostream& operator<<(std::ostream& str, coordinates const& c)
{
return str << c.x << " " << c.y << " " << c.z << " ";
}
};
现在您只需从文件中读取结构:
int main()
{
std::ifstream data("file.file");
coordinates value;
while(data >> value) {
std::cout << "I have a set of cordinates\n";
}
// To read them into an array (a vector is a resizable array available in C++)
std::vector<coordinates> vec;
std::copy(std::istream_iterator<coordinates>(file),
std::istream_iterator<coordinates>(),
std::back_inserter(vec));
}
答案 1 :(得分:0)
如何编写和读取结构到二进制文件
typedef struct{
int x;
int y;
int z;
}coordinates;
写入文件
FILE *f;
f = fopen("test.dat","wb");
for(int i = 0; i < 3; i++)
{
coordinates c;
c.x = 0;
c.y = 1;
c.z= 2;
fwrite(&c,sizeof(coordinates),1,f);
}
回读结构
FILE *f;
f = fopen("test.dat","rb");
if(!f){
cout << "Error";
return 0;
}
coordinates c;
while(fread(&c,sizeof(coordinates),1,f))
{
cout << c.x << " " << c.y << " "<< c.z << endl;
}
答案 2 :(得分:0)
如果可以,我可以从文件中检索结构吗?
是的,你可以。您可以使用read
和write
方法执行此操作。打开文件并以二进制模式编写结构。然后,您可以使用read方法以二进制模式从文件中读取。
您可以使用write方法将结构直接写入二进制模式的文件中:
#include <iostream>
#include <fstream>
using namespace std;
struct Player {
int age;
int score;
};
int main()
{
// You can write a structure directly into a file.
// Create an instance of 'Player':
Player rec;
rec.age = 10;
rec.score = 900;
// Create out file
fstream out_file("rec.bin",
ios::trunc | ios::binary | ios::in | ios::out
);
// write the whole structure into the file
out_file.write(reinterpret_cast<char*>( &rec ),
sizeof( rec )
);
}
我们在写入和读取方法中对char*
进行类型转换,因为write方法按顺序逐个写入每个字符,每个字节。因此,我们转换为char*
以直接引用这组字节。
要读取文件,请使用read方法。
以下是阅读我们刚刚写入文件struct
的{{1}}的示例:
rec.bin
输出:
#include <iostream>
#include <fstream>
using namespace std;
struct Player {
int age;
int score;
};
int main()
{
fstream in_file("rec.bin", ios::binary | ios::in | ios::out);
Player in_rec;
// read in age
if ( !in_file.read(
reinterpret_cast<char*>( &in_rec.age ),
sizeof( in_rec.age )
))
{
// handle error
}
// read in score
if ( !in_file.read(
reinterpret_cast<char*>( &in_rec.score ),
sizeof( in_rec.score )
))
{
// handle error
}
cout << in_rec.age << endl;
cout << in_rec.score << endl;
}
答案 3 :(得分:0)
在cord.txt文件中使用分隔符,因此可以轻松检索坐标。如果你有这种格式的文本文件,我的代码将很好用
cord.txt
4,5,6,
3,4,5,
7,8,9,
根据上述文本文件格式的代码是
typedef struct
{
int x;
int y;
int z;
}cord;
int main(int argc, char *argv[])
{
cord cr[100];
int i,j,k,l;
j=0;k=0;l=0;
char numbr[80];char numx[3];char numy[3];char numz[3];
FILE *p;
p = fopen("cord.txt","r");
if(!p) { cout<<"File is missing\n"; }
while(!feof(p))
{
for(i=0;i<80;i++)
numbr[i]=NULL;
for(i=0;i<3;i++)
{
numx[i]=NULL;
numy[i]=NULL;
numz[i]=NULL;
}
fgets(numbr,80,p);
for(i=0;i<strlen(numbr);i++)
{
if(numbr[i]!=',')
{
if(k==0) {numx[l]=numbr[i];}
else if(k==1) {numy[l]=numbr[i];}
else if(k==2) {numz[l]=numbr[i];}
l=l+1;
}
else
{
l=0;
if(k==0)
{cr[j].x=atoi(numx);k=k+1;}
else if(k==1)
{cr[j].y=atoi(numy);k=k+1;}
else if(k==2)
{cr[j].z=atoi(numz);k=0;break;}
}
}
j=j+1;
}
fclose(p);
for(i=0;i<j;i++)
{
cout<<cr[i].x<<" "<<cr[i].y<<" "<<cr[i].z<<endl;
}
cout<<"\n";
return EXIT_SUCCESS;
}
输出将是 4,5,6, 3,4,5, 7,8,9
答案 4 :(得分:0)
将结构写入FILE(或更一般地说,流)的过程称为序列化。你可以用二进制(这很难)或用文本来做。以下代码对类似的数据结构执行文本序列化。
int read_coords (FILE * f, coord_t * result);
void write_coords (FILE * f, coord_t const * write_me);
/**
* Reads a coordinate in the form x,y,z from f into result.
* Returns non-zero on success
*/
int read_coords (FILE * f, coord_t * result)
{
// Leading spaces in scanf cause us to elegantly handle weird whitespace
int read = fscanf (f, " %d , %d , %d", &result->x, &result->y, &result->z);
return read == 3;
}
void write_coords (FILE * f, coord_t const * result)
{
fprintf (f, "%d, %d, %d\n", result->x, result->y, result->z);
}
int main (void)
{
coord_t coord;
printf ("Gimme Coords: ");
if (read_coords (stdin, &coord))
{
printf ("I got: ");
write_coords(stdout, &coord);
}
else
{
printf ("I had an error reading those coords.");
}
return 0;
}
PS,看起来你在C级。我建议现在避免。您可以在同一程序中混合和匹配不同的I / O方法,但这会让事情变得混乱。