VS2013 处理文件 逐行读取test.log ,文档还没有结束 却跳出了循环

初入Cplusplus 2014-02-13 04:00:03
在VS2013 处理文件 ,如下代码,却出现文档还没有结束 却跳出了循环 ,原文档200行,只读了120行就结束了,
ifstream  readfile("test.log");  //1----读入文件mail2.log
if (!readfile) //打开文件失败!
{
cout << "Fail to open file in the first 1---!" << endl;
return 1;
}
else
{
int line = 0;
cout << readfile.peek() << endl;
if (!readfile.eof())
{
while (getline(readfile, strLine))//while (getline(readfile, strLine))
{ //判断空行,1-27,修改,空行不处理
if (strLine.size() != 0)
{
line = line + 1;
//cout << "this is the line number" << line << endl;
}

}
cout << strLine << endl;
}
}


显示结束行如

在notepad++下查看的文档,怎么是这种符号?同时在右下角,显示文档是UNIX ANSI 的状态
...全文
521 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
初入Cplusplus 2014-02-21
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
摒弃fstream 使用FILE *
不知道为啥流文件使用选择FILE*,我使用fstream 读取二进制文件,也能够识别出所需结果,只是之前我读取的是字符格式,可能导致windows下面的出错,而字节格式的的读取就没有错误了
初入Cplusplus 2014-02-18
  • 打赏
  • 举报
回复
引用 21 楼 zhao4zhong1 的回复:
不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
这样做的依据是什么?
赵4老师 2014-02-18
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。
赵4老师 2014-02-18
  • 打赏
  • 举报
回复
不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
初入Cplusplus 2014-02-18
  • 打赏
  • 举报
回复
引用 19 楼 cjzzmdn 的回复:
你试试用getline的返回值判断是否读完
while(getline(ifs,strget))
没有效果,我最初就是这么写的,后面不能读,我就加了eof判断,结果还是一样的
赵4老师 2014-02-18
  • 打赏
  • 举报
回复
上帖内容来自MSDN98的mk:@MSITStore:C:\MSDN98\98VS\2052\vccore.chm::/html/_crt_fopen.2c_._wfopen.htm
赵4老师 2014-02-18
  • 打赏
  • 举报
回复
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
赵4老师 2014-02-18
  • 打赏
  • 举报
回复
引用 23 楼 pingxu1987 的回复:
[quote=引用 21 楼 zhao4zhong1 的回复:] 不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
这样做的依据是什么?[/quote] 不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实! 有人说一套做一套,你相信他说的还是相信他做的? 其实严格来说这个世界上古往今来所有人都是说一套做一套,不是吗? 不要写连自己也预测不了结果的代码!
赵4老师 2014-02-17
  • 打赏
  • 举报
回复

#include <stdio.h>
int main() {
    FILE *f;
    static char ln[10000];
    int i;

    if (0==fopen_s(&f,"test.log","rb")) {
        while (1) {
            if (NULL==fgets(ln,10000,f)) break;
            i++;
            printf("%08d: %s",i,ln);
        }
        fclose(f);
    }
    return 0;
}
初入Cplusplus 2014-02-17
  • 打赏
  • 举报
回复
引用 13 楼 ddzz521 的回复:
直接去掉if (!readfile.eof())试试,试试吧。嘿嘿
我试过了,一样的,还是在那结束了
cjzzmdn 2014-02-17
  • 打赏
  • 举报
回复
你试试用getline的返回值判断是否读完
while(getline(ifs,strget))
赵4老师 2014-02-17
  • 打赏
  • 举报
回复
引用 17 楼 pingxu1987 的回复:
[quote=引用 16 楼 zhao4zhong1 的回复:]

#include <stdio.h>
int main() {
    FILE *f;
    static char ln[10000];
    int i;

    if (0==fopen_s(&f,"test.log","rb")) {
        while (1) {
            if (NULL==fgets(ln,10000,f)) break;
            i++;
            printf("%08d: %s",i,ln);
        }
        fclose(f);
    }
    return 0;
}
我使用这个可以实现全部行输出了,非常感谢。
	static char ln[10000];
但是对于预先设定的静态数组大小还是不能保证正确,后面我测试了一个行有60000多个字符的,结果就被分割为了6行,所以我增加大小[/quote]
引用 17 楼 pingxu1987 的回复:
[quote=引用 16 楼 zhao4zhong1 的回复:]

#include <stdio.h>
int main() {
    FILE *f;
    static char ln[10000];
    int i;

    if (0==fopen_s(&f,"test.log","rb")) {
        while (1) {
            if (NULL==fgets(ln,10000,f)) break;
            i++;
            printf("%08d: %s",i,ln);
        }
        fclose(f);
    }
    return 0;
}
我使用这个可以实现全部行输出了,非常感谢。
	static char ln[10000];
但是对于预先设定的静态数组大小还是不能保证正确,后面我测试了一个行有60000多个字符的,结果就被分割为了6行,所以我增加大小[/quote] 参考这个:
#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char ln[MAXLEN];
FILE *f;
int i,z;
int b,n,L;
int main(int argc,char **argv) {
    if (argc<2) {
        printf("Usage:%s fullpathfilename.ext\nget total blank/non-blank/total linenumbers.\n",argv[0]);
        return 1;
    }
    f=fopen(argv[1],"r");
    if (NULL==f) {
        printf("Can not open file [%s]!\n",argv[1]);
        return 2;
    }
    z=0;
    b=0;
    n=0;
    L=0;
    while (1) {
        if (NULL==fgets(ln,MAXLEN,f)) break;
        L=strlen(ln);
        if ('\n'==ln[L-1]) {
            if (0==z) {
                for (i=0;i<L-1;i++) {
                    if (!(' '==ln[i] || '\t'==ln[i])) break;
                }
                if (i<L-1) z=1;//当前行不是空行
            }
            if (0==z) b++; else n++;
            z=0;
        } else {
            if (0==z) {
                for (i=0;i<L;i++) {
                    if (!(' '==ln[i] || '\t'==ln[i])) break;
                }
                if (i<L) z=1;//当前行不是空行
            }
        }
    }
    fclose(f);
    if (L>0 && '\n'!=ln[L-1]) {
        if (0==z) b++; else n++;//最后一行末尾无'\n'也计算
    }
    printf("File:[%s] total blank/non-blank/total linenumbers is %d/%d/%d\n",argv[1],b,n,b+n);
    return 0;
}
初入Cplusplus 2014-02-17
  • 打赏
  • 举报
回复
引用 16 楼 zhao4zhong1 的回复:

#include <stdio.h>
int main() {
    FILE *f;
    static char ln[10000];
    int i;

    if (0==fopen_s(&f,"test.log","rb")) {
        while (1) {
            if (NULL==fgets(ln,10000,f)) break;
            i++;
            printf("%08d: %s",i,ln);
        }
        fclose(f);
    }
    return 0;
}
我使用这个可以实现全部行输出了,非常感谢。
	static char ln[10000];
但是对于预先设定的静态数组大小还是不能保证正确,后面我测试了一个行有60000多个字符的,结果就被分割为了6行,所以我增加大小
wxf54318 2014-02-16
  • 打赏
  • 举报
回复
有两种可能1、 getline用错了,可以用readfile.getline(...)试试 2、strline分配的不够大,溢出了。
Dzlua 2014-02-16
  • 打赏
  • 举报
回复
直接去掉if (!readfile.eof())试试,试试吧。嘿嘿
初入Cplusplus 2014-02-14
  • 打赏
  • 举报
回复
引用 11 楼 pingxu1987 的回复:
怎么读取 [quote=引用 10 楼 mujiok2003 的回复:] [quote=引用 8 楼 pingxu1987 的回复:] [quote=引用 7 楼 zhao4zhong1 的回复:] 摒弃fstream 使用FILE *
好吧 我试试 谢谢[/quote] 先找到问题, 不要着急找解决方案。 [/quote] 怎么使用FILE 中的 函数读取一行,char *fgets(char *s, int n, FILE *stream);没法设置第二个参数?[/quote] 怎么设?
初入Cplusplus 2014-02-14
  • 打赏
  • 举报
回复
怎么读取
引用 10 楼 mujiok2003 的回复:
[quote=引用 8 楼 pingxu1987 的回复:] [quote=引用 7 楼 zhao4zhong1 的回复:] 摒弃fstream 使用FILE *
好吧 我试试 谢谢[/quote] 先找到问题, 不要着急找解决方案。 [/quote] 怎么使用FILE 中的 函数读取一行,fopen_s(&fp, "D:\\log\\test.log", "wb");没法设置第二个参数?
mujiok2003 2014-02-14
  • 打赏
  • 举报
回复
引用 8 楼 pingxu1987 的回复:
[quote=引用 7 楼 zhao4zhong1 的回复:] 摒弃fstream 使用FILE *
好吧 我试试 谢谢[/quote] 先找到问题, 不要着急找解决方案。
mujiok2003 2014-02-14
  • 打赏
  • 举报
回复
引用 6 楼 pingxu1987 的回复:
[quote=引用 2 楼 zhuobattle 的回复:] getline最后一行内容打印出来看下,是不是文件结尾的那行
我已经打印了,程序中显示的内容,有特殊字符的,不是文章结尾,结尾就200行全部读完了[/quote] 使用正确的编码,中文字符可能被当成EOF了。
初入Cplusplus 2014-02-14
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
摒弃fstream 使用FILE *
好吧 我试试 谢谢
加载更多回复(7)

64,690

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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