周长一定 面积最大:有关vc 读取txt文件的问题

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/29 13:18:11
我想用fread语句将c:\zhuang\result.txt读到xyz数组里,然后画xyz的图,但运行时从c:\zhuang\result.txt.txt中读不出数.请教哪位高手帮忙解决!程序如下:
// EZ.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "stdlib.h"

double eez;
double x=1,y=1,z=1,q=1;
double xyz[10];
//float xyz[10];
FILE *fp1;

double EZ(double xx,double yy,double zz)
{
double ad=0.0, r=0.0;
ad=8.989e9;
r=sqrt((xx-x)*(xx-x)+(yy-y)*(yy-y)+(zz-z)*(zz-z));
r=1.0/(r*r*r);
eez=2*ad*q*(z-zz)*r;

return(0);
}

int main(int argc, char* argv[])
{
FILE *fp;
int n1,n2,i,j;
float rl,rw;
double xx,yy,zz;
char name[20];
double step1,step2;
//////////////////打开结果文件并从中读取 x,y,z,q
if((fp1=fopen("c:\\zhuang\\result.txt","r"))==NULL)
{printf("cannot open file\n"); exit(0);}
// fread(xyz,8,1,fp1);
fread(&x,8,1,fp1);
fread(&y,8,1,fp1);
fread(&z,8,1,fp1);
fread(&q,18,1,fp1);
fclose(fp1);
// for(i=0;i<4;i++)
// fread(&xyz[i],2,1,fp1);
// fclose(fp1);
// x=0.0;y=0.0;z=100.0;q=1.0;//试算用

// x=xyz[0];y=xyz[1];z=xyz[2];q=xyz[3];
printf("%f %f %f %f\n",x,y,z,q);
/////////////////
// x=0.0;y=0.0;z=100.0;q=1.0;//试算用
printf("用于绘图的文件名是(*.dat) :\n");
scanf("%s",name);
printf("长(Rl)= 宽(Rw)= \n");
scanf("%f %f",&rl,&rw);
n1=25,n2=25;
step1=0.5*rl/n1;step2=0.5*rw/n2;
fp=fopen(name,"wb");
zz=0.0;
for(i=-n2;i<n2+1;i++)
{yy=i*step2;
for(j=-n1;j<n1+1;j++)
{xx=j*step1;
EZ(xx,yy,zz);
if(j<n1) fprintf(fp,"%8.4e\t",eez);
if(j==n1) fprintf(fp,"%8.4e\n" ,eez);
}
}fclose(fp);

return(0);
}

你的fread函数用法是否正确呢?我提供了一份参考资料给你看,这是fread的完整正确用法。呵呵

Reads data from a stream.

size_t fread(
void *buffer,
size_t size,
size_t count,
FILE *stream
);

Parameters
buffer
Storage location for data.

size
Item size in bytes.

count
Maximum number of items to be read.

stream
Pointer to FILE structure.

Return Value
fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged. If stream or buffer is a null pointer, fread invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function sets errno to EINVAL and returns 0.

See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.

Remarks
The fread function reads up to count items of size bytes from the input stream and stores them in buffer. The file pointer associated with stream (if there is one) is increased by the number of bytes actually read. If the given stream is opened in text mode, carriage return–linefeed pairs are replaced with single linefeed characters. The replacement has no effect on the file pointer or the return value. The file-pointer position is indeterminate if an error occurs. The value of a partially read item cannot be determined.

This function locks out other threads. If you need a non-locking version, use _fread_nolock.

Requirements
Function Required header Compatibility
fread
<stdio.h>
ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For additional compatibility information, see Compatibility in the Introduction.

Example
Copy Code
// crt_fread.c
// This program opens a file named FREAD.OUT and
// writes 25 characters to the file. It then tries to open
// FREAD.OUT and read in 25 characters. If the attempt succeeds,
// the program displays the number of actual items read.

#include <stdio.h>

int main( void )
{
FILE *stream;
char list[30];
int i, numread, numwritten;

// Open file in text mode:
if( fopen_s( &stream, "fread.out", "w+t" ) == 0 )
{
for ( i = 0; i < 25; i++ )
list[i] = (char)('z' - i);
// Write 25 characters to stream
numwritten = fwrite( list, sizeof( char ), 25, stream );
printf( "Wrote %d items\n", numwritten );
fclose( stream );

}
else
printf( "Problem opening the file\n" );

if( fopen_s( &stream, "fread.out", "r+t" ) == 0 )
{
// Attempt to read in 25 characters
numread = fread( list, sizeof( char ), 25, stream );
printf( "Number of items read = %d\n", numread );
printf( "Contents of buffer = %.25s\n", list );
fclose( stream );
}
else
printf( "File could not be opened\n" );
}

Output

Wrote 25 items
Number of items read = 25
Contents of buffer = zyxwvutsrqponmlkjihgfedcb

楼上的答案很好