为了账号安全,请及时绑定邮箱和手机立即绑定

c++怎么用fseek函数定位?

/ 猿问

c++怎么用fseek函数定位?

尚方宝剑之说 2019-03-06 11:07:44

#include<iostream>
#include <fstream>
#include <string>
#include <string.h>
using namespace std;

void main()
{
//在文件中写入学号和科目
string number;
string name;
ofstream outstuf ; 
outstuf.open( "test.txt" , ios::app|ios::binary ) ; 
if ( !outstuf ) 
{ cerr << "文件不存在" << endl ; abort(); }
cin>>number;
number=number+" ";
outstuf.seekp(0,ios::end); //多这名,文件指针移到尾
outstuf<<number.c_str();

怎么用fseek()使得指针能指向到number后面,指向文件里面number的后面?

查看完整描述

3 回答

?
慕姐4208626

函数名: fseek

功 能: 是把文件指针指向文件的开头,需要包含头文件stdio.h

用 法: int fseek(FILE *stream, long offset, int fromwhere);

描 述: 函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere为基准,偏移offset个字 节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。
返回值: 成功,返回0,否则返回其他值。
fseek position the file position pointer for the file referenced by stream to the byte location calculated by offset.
程序例:

[cpp] view plaincopy
#include <stdio.h>
long filesize(FILE *stream);
int main(void)
{
FILE *stream;
stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream));
fclose(stream);
return 0;
}
long filesize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}

int fseek( FILE *stream, long offset, int origin );
第一个参数stream为文件指针
第二个参数offset为偏移量,整数表示正向偏移,负数表示负向偏移
第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END 或 SEEK_SET
SEEK_SET: 文件开头
SEEK_CUR: 当前位置
SEEK_END: 文件结尾
其中SEEK_SET,SEEK_CUR和SEEK_END和依次为0,1和2.
简言之:
fseek(fp,100L,0);把fp指针移动到离文件开头100字节处;
fseek(fp,100L,1);把fp指针移动到离文件当前位置100字节处;
fseek(fp,100L,2);把fp指针退回到离文件结尾100字节处。
使用实例:
[cpp] view plaincopy
#include <stdio.h>
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, STU n)
{
FILE *fp;
fp = fopen(filename, "rb+");
fseek(fp, -1L*sizeof(STU),SEEK_END);
fwrite(&n, sizeof(STU), 1, fp);
fclose(fp);
}
void main()
{
STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},
{10005,"ZhangSan", 95, 80, 88}};
STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), N, fp);
fclose(fp);
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
printf("\nThe original data :\n\n");
for (j=0; j<N; j++)
{
printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++)
[cpp] view plaincopy
printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
fun("student.dat", n);
printf("\nThe data after modifing :\n\n");
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
for (j=0; j<N; j++)
{
printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++)
[cpp] view plaincopy
printf("%6.2f ", ss[j].score[i]);
printf("\n");
}



查看完整回答
反对 回复 2019-03-26
?
qq_笑_17

int fseek(FILE *stream, long offset, int fromwhere);函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere(偏移起始位置:文件头0(SEEK_SET),当前位置1(SEEK_CUR),文件尾2(SEEK_END))为基准,偏移offset(指针偏移量)个字节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。
也就是写成 fseek(outstuf,0,2);

查看完整回答
反对 回复 2019-03-26
?
繁星点点滴滴

函数fseek使用指南

1.函数原型:

int fseek ( FILE * stream, long int offset, int origin );

2. 函数功能:

Sets the position indicator associated with the stream to a new position.

For streams open in binary mode, the new position is defined by adding offset to a reference position specified by origin.

For streams open in text mode, offset shall either be zero or a value returned by a previous call to ftell, and origin shall necessarily be SEEK_SET.

If the function is called with other values for these arguments, support
depends on the particular system and library implementation
(non-portable).

The end-of-file internal indicator of the stream is cleared after a successful call to this function, and all effects from previous calls to ungetc on this stream are dropped.

On streams open for update (read+write), a call to fseek allows to switch between reading and writing.

3. 函数参数

stream
Pointer to a FILE object that identifies the stream.

offset
Binary files: Number of bytes to offset from origin.
Text files: Either zero, or a value returned by ftell.

origin
Position used as reference for the offset. It is specified by one of the following constants defined in <cstdio> exclusively to be used as arguments for this function:

Constant
Reference position

SEEK_SET Beginning of file

SEEK_CUR Current position of the file pointer

SEEK_END End of file *

* Library implementations are allowed to not meaningfully support SEEK_END (therefore, code using it has no real standard portability).

4. 返回值

If successful, the function returns zero.
Otherwise, it returns non-zero value.
If a read or write error occurs, the error indicator (ferror) is set.

5. 例程

#include <stdio.h>

int main ()
{
FILE * pFile;
pFile = fopen ( "example.txt" , "wb" );
fputs ( "This is an apple." , pFile );
fseek ( pFile , 9 , SEEK_SET );
fputs ( " sam" , pFile );
fclose ( pFile );
return 0;
}
After this code is successfully executed, the file example.txt contains:
This is a sample



查看完整回答
反对 回复 2019-03-26

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信