C语言读写文件求助

qq_27021363 2015-05-05 03:47:17
代码如下:

#include "stdafx.h"
#define FILENAME1 "filename1.txt"
#define FILENAME2 "filename2.txt"
#define NUM 10


int _tmain(int argc, _TCHAR* argv[])
{
FILE* p1=NULL;//文件指针,指向读入数据的文件
FILE* p2=NULL;//文件指针,指向需要修改的文件
int i; //循环控制变量,控制读写次数
char ch;
char buff[1024];
char read_fpxxfpje[9];
//write_fpxxfpje[10];
if ((p1 = fopen(FILENAME1, "rb")) == NULL)
{
printf("读取文件失败,按回车键退出!\n"); //输出错误提示
while (1) //死循环控制错误提示
{
ch = getchar();
if (ch == '\n')break; //满足条件退出循环
}
exit(1);
}
if ((p2 = fopen(FILENAME2, "rb+")) == NULL)
{
printf("写入文件失败,按回车键退出!\n"); //输出错误提示
while (1) //死循环控制错误提示
{
ch = getchar();
if (ch == '\n')break; //满足条件退出循环
}
exit(1);
}
memset(buff, 0, 1024); //数组元素置零
for (i = 0; i < NUM; i++)
{
if (fgets(buff, 1024, p1) != NULL) //检查能否读取read指向文件的第一行数据
{
memset(read_fpxxfpje, 0, 9);
memcpy(read_fpxxfpje, buff + 514, 8); //复制read指向文件第一行数据第514~523个数据,赋予read_fpxx.fpje
printf("read_fpxx.fpje:%s\n", read_fpxxfpje); //输出.fpje的值
}
else
printf("获取第%d行数据失败",i); //不能打开文件提示错误信息
if (fgets(buff, 1024, p2) != NULL) //检查能否读取数据
{
fseek(p2, 32L,0);
fputs(read_fpxxfpje, p2);
fseek(p2, 109L, 1);
}
else
printf("写入第%d行数据失败", i);
}
fclose(p1);
fclose(p2);
while (1) //死循环控制错误提示
{
printf("程序执行完成,请按回车键退出程序!");
ch = getchar();
if (ch == '\n')break; //满足条件退出循环
}
exit(1);
return 0;
}


想读filename1文件的固定内容,然后写入到filename2文件的固定位置,现在解决不了的问题是写的时候重复写在第一行。。

if (fgets(buff, 1024, p2) != NULL) //检查能否读取数据
{
fseek(p2, 32L,0);
fputs(read_fpxxfpje, p2);
fseek(p2, 109L, 1);
}
else
printf("写入第%d行数据失败", i);

我觉得应该是上面这个fseek(p2,32L,0)这部分出了问题,每循环一次都把光标移到文件开头位置往后32L的地方了,有没有办法让此句只运行一次?或者有其他办法解决光标的位置问题不?BAIDU了好久找不到办法来求助各位。。
...全文
111 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-05-05
  • 打赏
  • 举报
回复
fopen, _wfopen Open a file. FILE *fopen( const char *filename, const char *mode ); FILE *_wfopen( const wchar_t *filename, const wchar_t *mode ); Function Required Header Compatibility fopen <stdio.h> ANSI, Win 95, Win NT _wfopen <stdio.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version The c, n, and t mode options are Microsoft extensions for fopen and _fdopen and should not be used where ANSI portability is desired. Return Value Each of these functions returns a pointer to the open file. A null pointer value indicates an error. Parameters filename Filename mode Type of access permitted Remarks The fopen function opens the file specified by filename. _wfopen is a wide-character version of fopen; the arguments to _wfopen are wide-character strings. _wfopen and fopen behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tfopen fopen fopen _wfopen The character string mode specifies the type of access requested for the file, as follows: "r" Opens for reading. If the file does not exist or cannot be found, the fopen call fails. "w" Opens an empty file for writing. If the given file exists, its contents are destroyed. "a" Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist. "r+" Opens for both reading and writing. (The file must exist.) "w+" Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed. "a+" Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist. When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten. The "a" mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. The "a+" mode does remove the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker. When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired. In addition to the above values, the following characters can be included in mode to specify the translation mode for newline characters: t Open in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing with "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z, may cause fseek to behave improperly near the end of the file. Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the mbtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function). b Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed. If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL. For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes. c Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called. n Reset the commit flag for the associated filename to “no-commit.” This is the default. It also overrides the global commit flag if you link your program with COMMODE.OBJ. The global commit flag default is “no-commit” unless you explicitly link your program with COMMODE.OBJ. Valid characters for the mode string used in fopen and _fdopen correspond to oflag arguments used in _open and _sopen, as follows. Characters in mode String Equivalent oflag Value for _open/_sopen a _O_WRONLY | _O_APPEND (usually _O_WRONLY | _O_CREAT | _O_APPEND) a+ _O_RDWR | _O_APPEND (usually _O_RDWR | _O_APPEND | _O_CREAT ) r _O_RDONLY r+ _O_RDWR w _O_WRONLY (usually _O_WRONLY | _O_CREAT | _O_TRUNC) w+ _O_RDWR (usually _O_RDWR | _O_CREAT | _O_TRUNC) b _O_BINARY t _O_TEXT c None n None Example /* FOPEN.C: This program opens files named "data" * and "data2".It uses fclose to close "data" and * _fcloseall to close all remaining files. */ #include <stdio.h> FILE *stream, *stream2; void main( void ) { int numclosed; /* Open for read (will fail if file "data" does not exist) */ if( (stream = fopen( "data", "r" )) == NULL ) printf( "The file 'data' was not opened\n" ); else printf( "The file 'data' was opened\n" ); /* Open for write */ if( (stream2 = fopen( "data2", "w+" )) == NULL ) printf( "The file 'data2' was not opened\n" ); else printf( "The file 'data2' was opened\n" ); /* Close stream */ if( fclose( stream ) ) printf( "The file 'data' was not closed\n" ); /* All other files are closed: */ numclosed = _fcloseall( ); printf( "Number of files closed by _fcloseall: %u\n", numclosed ); } Output The file 'data' was opened The file 'data2' was opened Number of files closed by _fcloseall: 1 Stream I/O Routines See Also fclose, _fdopen, ferror, _fileno, freopen, _open, _setmode
苏叔叔 2015-05-05
  • 打赏
  • 举报
回复
修改如下:

if (fgets(buff, 1024, p2) != NULL)            //检查能否读取数据
{
	fseek(p2, 32L + i * 8, 0);          //主要对这句,进行修改
	fputs(read_fpxxfpje, p2);
	//fseek(p2, 109L, 1);   //这句没看出有什么作用
}
一.C语言基础 1.C语言特点(识记); 2.C语言程序基本组成(识记): 3.基本数据类型: 3.1 标识符与基本数据类型(识记), 3.2 常量与变量(领会) 3.3 内存的概念(识记) 4.基本输入、输出函数(领会): 5.运算符与表达式(简单应用): 5.1 运算符的优先级与结合性 二.程序控制结构 1.C语言的语句(识记): 2.顺序结构(领会): 3.分支结构(简单应用): 4.循环结构(综合应用): 5 算法特点 6 流程图 三.构造型数据 1.数组(综合应用): 1.1 定义和引用 1.2 字符数组 1.3 指针和数组 2.结构类型: 2.1 结构类型的概念 2.2 结构类型定义及结构变量说明 2.3 结构变量的初始化 2.4 结构数组的初始化 3.联合类型(识记): 3.1 联合类型的概念 3.2 联合类型定义和联合变量说明 3.3 联合类型的使用 3.4 Struct 和 Union区别 4.枚举型(识记): 4.1 枚举型的定义 4.2 使用枚举型变量 5.typedef的用途(识记): 四.指针 1.指针与指针变量(识记): 2.指针运算符(领会): 3. 指针与函数 4.指针数组与指向指针的指针(识记): 5.指针与结构(领会): 6. 难点和易混淆 五.函数 1.常见的系统库函数(识记): 2.用户自定义函数(简单应用): 2.1函数定义 2.2 函数调用 2.3 函数声明 2.4 函数返回值 2.5 函数参数 3.函数之间的数据传递(领会): 4.函数的嵌套调用及递归调用(领会): 5.局部变量与全局变量(识记): 6.变量的存储类型与变量的初始化(领会): 7.编译预处理(领会): 六.文件 1.文件的基本概念,C语言中的两种文件(识记) 2.文件的打开、关闭和文件结束测试,文件读写文件的定位(识记) 2.1文件操作函数 2.2 文件权限 七.算法与编程(综合应用) 1 使用Turbo C集成开发环境调试程序 1.1.源程序的编写、编辑与改错(领会); 1.2.集成环境下的求助Help(识记); 1.3.程序的编译与目标代码的生成(识记); 1.4.程序的调试(综合应用): 1.5.了解Turbo C程序的常见错误提示(识记)。 2 重点编程题 八 位运算 1. & 2. | 3. ^ 4. ~ 5. << 6. >> ———————————————— 版权声明:本文为CSDN博主「kaikai_sk」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/kaikai_sk/article/details/106061539

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧