subject_14990.htm

来自「一些关于vc的问答」· HTM 代码 · 共 102 行

HTM
102
字号
<p>
序号:14990 发表者:glark 发表日期:2002-09-13 12:35:39
<br>主题:我的文件里有多行str,我想每读一行执行另外一个程序,然后自动指向下一行读
<br>内容:CFile里好像没有一次读一行的函数,该用什么呢?
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:74LS373 回复日期:2002-09-13 12:44:01
<br>内容:CStdioFile
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:光阴似剑 回复日期:2002-09-13 12:48:30
<br>内容:&nbsp;&nbsp;&nbsp;&nbsp;char szLine[1024];<BR>&nbsp;&nbsp;&nbsp;&nbsp;memset(szLine, 0, sizeof(szLine));<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;while ((fscanf(m_pFile, "%[^\n]", szLine)) != EOF)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fgetc(m_pFile);&nbsp;&nbsp;&nbsp;&nbsp;// Reads in '\n' character and moves file<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:glark 回复日期:2002-09-13 13:25:13
<br>内容:CStdioFile能举个例子吗? <BR>char *ch<BR>file.Open(FileName,CFile::modeRead|CFile::modeWrite);<BR>file.ReadString(ch,50);<BR>好像不对,打开时出现错误!<BR>为什么ch要为char型而不是CString?<BR>ReadString能自动读入string的长度吗?50是不是只要设成比那一行的string大就行?<BR>谢谢。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Aikoc 回复日期:2002-09-13 13:38:59
<br>内容:CStdioFile *pFile = NULL;<BR><BR>try<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;pFile = new CStdioFile(_T("c:\1.txt"),CFile::modeRead|CFile::typeText);<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(pFile)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CString strTmp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pFile-&gt;ReadString(strTmp)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pFile-&gt;Close();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete pFile;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pFile = NULL;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}<BR>catch(CFileException* e)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;delete pFile;<BR>&nbsp;&nbsp;&nbsp;&nbsp;pFile = NULL;<BR>&nbsp;&nbsp;&nbsp;&nbsp;delete e;<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:74LS373 回复日期:2002-09-13 13:44:51
<br>内容://virtual LPTSTR ReadString( LPTSTR lpsz, UINT nMax );<BR>//throw( CFileException );<BR><BR>//BOOL ReadString(CString& rString);<BR>//throw( CFileException );<BR><BR>////////////////////////////////////////////////////////////<BR>CStdioFile mFile; <BR><BR>CFileException mExcept; <BR><BR>mFile.Open( “d:\\ss.bat”, CFile::modeWrite, &mExcept); <BR><BR>////////////////......CFileException<BR><BR>CString string;<BR><BR>mFile.ReadString(string); <BR><BR>mFile.Close(); <BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:glark 回复日期:2002-09-13 14:18:22
<br>内容:谢谢Aikoc 、74LS373 ,那怎样才能获得有多少行呢?<BR>还有,我在打开文件后Close,运行时报错,??<BR>谢谢。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:glark 回复日期:2002-09-13 15:04:56
<br>内容:我的意思是让他读一行执行一个程序,接着自动读下一行,当他读完时自动结束,不再执行程序。(我现在想到的是用for循环,也就是循环那个行数,可我友不只怎样获得行数,或者有更好的方法吗)<BR>谢谢。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:小戟 回复日期:2002-09-13 15:13:35
<br>内容:CStdioFile file;<BR>&nbsp;&nbsp;&nbsp;&nbsp;CString str;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int i=0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(file.Open("*.*",CFile::modeRead))<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.Seek(i,CFile::begin);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(file.ReadString(str))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i+=str.GetLength();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i+=2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.Seek(i,CFile::begin);//下一行,你的操作执行程序<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>//退出程序。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:glark 回复日期:2002-09-13 15:58:44
<br>内容:i=+2<BR>2是什么?是移到下一行吗?我怎么始终在执行一行?<BR>void CMfcPingDlg::OnTimer(UINT nIDEvent) <BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;// TODO: Add your message handler code here and/or call default<BR>&nbsp;&nbsp;&nbsp;&nbsp;int i=0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(file.Open("ping.txt",CFile::modeRead))<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.Seek(i,CFile::begin);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(file.ReadString(string))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i+=string.GetLength();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i+=2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file.Seek(i,CFile::begin);//下一行,操作执行程序<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MidPro();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>//&nbsp;&nbsp;&nbsp;&nbsp;file.ReadString(string);<BR>//&nbsp;&nbsp;&nbsp;&nbsp;MidPro();<BR>&nbsp;&nbsp;&nbsp;&nbsp;CDialog::OnTimer(nIDEvent);<BR>}
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:小戟 回复日期:2002-09-13 16:04:26
<br>内容:2是什么“\n”的长度;file.Seek(i,CFile::begin)是移到下一行了;你怎么始终在执行一行,那就是你的文件就一行;<BR><BR>改文件写入:CStdioFile file;<BR>&nbsp;&nbsp;&nbsp;&nbsp;CString str;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(file.Open("*.*", CFile::modeCreate | CFile::modeWrite | CFile::typeText))<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i=0;i&lt;大小(行数);i++ )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.SeekToEnd();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.WriteString(str);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.WriteString("\n");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.Close();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:glark 回复日期:2002-09-13 16:17:36
<br>内容:我的文件很简单<BR>127.0.0.1<BR>127.0.0.2<BR>可是每次程序都直接ping第二行
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Aikoc 回复日期:2002-09-13 16:18:15
<br>内容:int nLineCount = 0;&nbsp;&nbsp;&nbsp;&nbsp;// get line number of the text file if needed<BR>while(pFile-&gt;ReadString(strTmp))<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;nLineCount ++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;// do you work here<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:小戟 回复日期:2002-09-13 16:22:12
<br>内容:glark,你是如何写文件的。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:glark 回复日期:2002-09-13 16:24:30
<br>内容:我是直接在记事本上写的,每次它都从最后一行读
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:小戟 回复日期:2002-09-13 16:46:45
<br>内容:<BR>127.0.0.1<BR>127.0.0.2<BR>写成上面的,看行不行.<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:glark 回复日期:2002-09-13 16:55:22
<br>内容:小戟,你的方法是对的,我想应该是ping程序的问题
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?