首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C 同一文件 不同文件文件指针有关问题.

2013-08-09 
C 同一文件不同文件文件指针问题....请教大牛们!!同一个文件, 用两个文件流打开, fopen(), 一个用来写, 一

C 同一文件 不同文件文件指针问题....
请教大牛们!!
  同一个文件, 用两个文件流打开, fopen(), 一个用来写, 一个用来读, 两个线程。写的过程,每次写的时候都打开,加锁, 写完关闭, 解锁。  读操作是一个文件指针, 只打开一次,顺序读取, 这个读取操作是由一定条件限制的。


这种边写边读方式不是很稳定, 出现现象: 有时感觉读的文件流没有更新到, 读到文件某个位置时 没有再往下读, 但实际文件已经写入了的。  这个读文件的 FILE* 如何更新?  这种方法有没有可以改善使之稳定的方法?
 
[解决办法]

什么时候写,什么时候打开,写完立即关闭。写几次打开并关闭几次
读的时候也是,什么时候读什么时候打开,读完后立即关闭,不要提前打开,读写操作后立即关闭

这样的话应该没什么问题了吧!

[解决办法]

在加读写锁呗!

在写的时候加写锁!这样读的线程就会阻塞在那里,直到写完成释放了写锁!
这个时候再读,文件已经更新完成!

读的时候加读锁,这样,写的线程就会阻塞,不会进行写操作的!
但是别的读线程也可以打开文件进行读操作!

[解决办法]
这很明显不能用stdio,必须直接使用底层系统API。

fread你要是读到末尾就标记eof不好使了,用底层API,定时stat检查文件大小,有新数据就去读就是了。
[解决办法]
引用:
Quote: 引用:

请教大牛们!!
  同一个文件, 用两个文件流打开, fopen(), 一个用来写, 一个用来读, 两个线程。写的过程,每次写的时候都打开,加锁, 写完关闭, 解锁。  读操作是一个文件指针, 只打开一次,顺序读取, 这个读取操作是由一定条件限制的。


这种边写边读方式不是很稳定, 出现现象: 有时感觉读的文件流没有更新到, 读到文件某个位置时 没有再往下读, 但实际文件已经写入了的。  这个读文件的 FILE* 如何更新?  这种方法有没有可以改善使之稳定的方法?
 


我觉得你不应该开两个文件流,你应该开一个可读可写文件流,然后用一个读写锁同步,注意写入后马上fflush一下。

同意!
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

[解决办法]
关闭当然要刷行数据了。
记事本,缓存了数据,如果不重新打开,直接使用缓存的数据。
没有使用文件中的新数据。
当然就看不到数据更新了。
[解决办法]

引用:
读的时候不能枷锁, 这样会导致数据没有写入, 导致数据丢失....

这个文件的每行数据开头都是时标,读操作完全是卡时间段来读取.每次的读取是通过文件的最后一行和当前读取时标进行判断,是否有10分钟的数据(时标差), 有就进行读取, 没有便进行等待, 如此反复操作。  
写的时候枷锁了, 读的过程没有枷锁。  有一段时间很稳定有时就卡在某个时标点数据了, 没有往下读取。

请大牛们分析下,有没有好的办法??

其实读也是要加锁的。但是这个锁不是你写加的锁。要使用读写锁!

如果不喜欢这样做,还有一种机制可供使用:RCU-读取-复制-更新!具体相关资料,lz自行查阅!

热点排行