Data saving and reading debugging for files caused by the number 26 in the C + + language.

Source: Internet
Author: User
Tags fread

The first problem encountered in VS2010 is the creation of a structural body

struct position{

int x;

int y;

};

Then declare an array with this struct rout[8];

for (int i=0;i<8;i++)

rout[i]=i+25;

And then store the structure data in a file.

Fwrite (&rout[i],sizeof (position), 1,FP);

The number of bytes directly observed from the file is sizeof (position) *8, but when the data is read

Fread (temp,sizeof (position), 1,FP);

Does not read to the end of the file.

It was later discovered that it was finished reading to rout[i].x=26.

Question 1, what is the reason for reading this 26 to end?

During the validation process, the following code

#include "Stdafx.h"
#include <iostream>
#include "Atlstr.h"
using namespace Std;

int rout[10];
void Main ()
{
int t=0;
int temp;
for (int i=0;i<10;i++)
rout[i]=i+25;
FILE *fp=fopen ("Rout_list13.txt", "w");
for (int i=0;i<10;i++)
Fwrite (&rout[i],sizeof (int), 1,FP);
Fclose (FP);
Fp=fopen ("Rout_list13.txt", "R");
while (T<10)
{
Fread (&temp,sizeof (int), 1,FP);
printf ("%d", temp);
t++;
}
Fclose (FP);
}

The result of the discovery output has been

Question 2: What happens when the value is not changed?

Make the following changes to the above code

#include "Stdafx.h"
#include <iostream>
#include "Atlstr.h"
using namespace Std;

int rout[10];
void Main ()
{
int t=0;
int temp;
for (int i=0;i<10;i++)
rout[i]=i+6;
FILE *fp=fopen ("Rout_list13.txt", "w");
for (int i=0;i<10;i++)
Fwrite (&rout[i],sizeof (int), 1,FP);
Fclose (FP);
Fp=fopen ("Rout_list13.txt", "R");
while (T<10)
{
Fread (&temp,sizeof (int), 1,FP);
printf ("%d", temp);
Fread (&temp,sizeof (int), 1,FP);
printf ("%d", temp);
t++;
}
Fclose (FP);
}

You can see the result.

The first analysis of the array exists 10 numbers from 6 to 15 end, but read 20 times, when the end of the reading data is unchanged.

#include "Stdafx.h"
#include <iostream>
#include "Atlstr.h"
using namespace Std;

int rout[10];
void Main ()
{
int t=0;
int temp;
for (int i=0;i<10;i++)
rout[i]=i+20;
FILE *fp=fopen ("Rout_list13.txt", "w");
for (int i=0;i<10;i++)
Fwrite (&rout[i],sizeof (int), 1,FP);
Fclose (FP);
Fp=fopen ("Rout_list13.txt", "R");
while (T<10)
{
Fread (&temp,sizeof (int), 1,FP);
printf ("%d", temp);
Fread (&temp,sizeof (int), 1,FP);
printf ("%d", temp);
t++;
}
Fclose (FP);
}

The result is

therefore, by the above debugging process can get the following conclusions : when the file data read after the end of the file read value is the last read results and remain unchanged, you can answer question 2.

Continue to discuss issue 1, by using the Fseek function to locate, found 26 this number in the file occupies 4 bytes of data.

After Baidu a bit 26 of the file read problem, got the solution.

A binary representation of the data that is written or read to the file. The code is as follows

#include "Stdafx.h"
#include <iostream>
#include "Atlstr.h"
using namespace Std;

int rout[10];
void Main ()
{
int t=0;
int temp;
for (int i=0;i<10;i++)
rout[i]=i+24;
FILE *fp=fopen ("Rout_list13.txt","WB");
for (int i=0;i<10;i++)
Fwrite (&rout[i],sizeof (int), 1,FP);
Fclose (FP);
Fp=fopen ("Rout_list13.txt", "RB");
while (T<5)
{
Fread (&TEMP,4,1,FP);
printf ("%d", temp);
t++;
}
Fclose (FP);
}

Result is

Question 1 has also been addressed.

Data saving and reading debugging for files caused by the number 26 in the C + + language.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.