在线等关于evc读取txt问题
有个txt文件,里面就一个数,如67,怎样通过evc读取这个数呢,我这里不用mfc,我用fread和fscanf都读不出,怎么办,请指教,最好能添下代码 问题点数:20、回复次数:13Top
1 楼yzx0023(无聊客)回复于 2006-06-02 09:05:23 得分 0
fread读不出?不会吧Top
2 楼xjgycc()回复于 2006-06-02 09:07:17 得分 0
是啊,不知道是不是我格式错了,fscanf也读不出
fp=fopen("debug.txt", "r");
pt.y= fscanf(fp, "%ld",&x);Top
3 楼xhenry()回复于 2006-06-02 09:40:38 得分 0
注意文件路径,最好写完整的路径和文件名Top
4 楼xjgycc()回复于 2006-06-02 09:47:00 得分 0
就是放在根目录,写的时候可以的,但读不可以Top
5 楼slek(我浮躁,可是我很厚道!)回复于 2006-06-02 09:49:40 得分 0
怎么会读不出来?跟踪一下,看到底是哪里出错,先用fread读到字符串里面试试Top
6 楼xjgycc()回复于 2006-06-02 09:56:37 得分 0
这是我的源码,在program里,xjgyccone.ys168.comTop
7 楼xjgycc()回复于 2006-06-02 10:54:19 得分 0
读取问题已解决,但读多个数时该怎么办呢如22,56,98Top
8 楼yujia120(永不停息)回复于 2006-06-02 14:51:27 得分 0
//////////////////////////////////////////////////////////////////////
//
// ------------------------------- //
// -------- Start of File -------- //
// ------------------------------- //
// ----------------------------------------------------------- //
// C++ Source Code File Name: StudioFile.h
// C++ Compiler Used: Microsoft eVC++ 3.0
// Produced By: SofTech Systems Inc., New Baden, Il 62265
// File Creation Date: 07/14/2002
// Date Last Modified: 07/14/2002
// Copyright (c) 2002 SofTech System Inc.
// ----------------------------------------------------------- //
// ------------- Program Description and Details ------------- //
// ----------------------------------------------------------- //
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
The CStudioFile class is intended for WinCE MFC applications because MFC for
WinCE did not implement the CStdioFile text file functions. You should
use the CStdioFile in Win32 programs.
This class implements both UNICODE and ASCII text file reads and writes. If the
format is UNICODE, it is assumed to contain 16-bit strings. Other UNICODE formats
cannot be read by WinCE programs. It is the caller's responsibility to know which format
the file is in.
Because of file buffering, programmers should not mix the CFile::Read and
CFile::Write with the ReadString methods in this class. Doing so will cause
unpredictable results.
CFile::typeText is stripped from the open flags when the Open method is called
to avoid the ASSERT in the base class. It is not necessary to include this
flag when using the CStudioFile class.
*/
// ----------------------------------------------------------- //
#if !defined(AFX_STUDIOFILE_H__FA17FC8C_296D_4E53_9BD9_54A071E7F724__INCLUDED_)
#define AFX_STUDIOFILE_H__FA17FC8C_296D_4E53_9BD9_54A071E7F724__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CStudioFile : public CFile
{
public:
// Constructors
CStudioFile();
// CStudioFile(HANDLE pOpenStream);
CStudioFile(LPCTSTR lpszFileName, UINT nOpenFlags) {}
virtual ~CStudioFile() {}
// m_hFile from base class is _fileno(m_pStream)
// Operations
virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags,
CFileException* pError = NULL);
virtual void WriteString(wchar_t* lpsz);
virtual void WriteString(char* lpsz);
virtual wchar_t* ReadString(wchar_t* lpsz, UINT nMax);
virtual char* ReadString(char* lpsz, UINT nMax);
virtual BOOL ReadString(CString& rString, bool bIsUnicode = true);
// Implementation
public:
// virtual DWORD GetPosition() const;
// virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags,
// CFileException* pError = NULL);
// virtual UINT Read(void* lpBuf, UINT nCount);
// virtual void Write(const void* lpBuf, UINT nCount);
// virtual LONG Seek(LONG lOff, UINT nFrom);
// virtual void Abort();
// virtual void Flush();
// virtual void Close();
protected:
#define _MAX_BUFSIZE 255
wchar_t m_ibuf[_MAX_BUFSIZE]; // input buffer
int m_nChars; // number of characters actaully in the buffer
int m_nextout; // current position in m_ibuf
};
#endif // !defined(AFX_STUDIOFILE_H__FA17FC8C_296D_4E53_9BD9_54A071E7F724__INCLUDED_)
// ----------------------------------------------------------- //
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //
Top
9 楼yujia120(永不停息)回复于 2006-06-02 14:52:20 得分 0
//////////////////////////////////////////////////////////////////////
//
// ------------------------------- //
// -------- Start of File -------- //
// ------------------------------- //
// ----------------------------------------------------------- //
// C++ Source Code File Name: StudioFile.cpp
// C++ Compiler Used: Microsoft eVC++ 3.0
// Produced By: SofTech Systems Inc., New Baden, Il 62265
// File Creation Date: 07/14/2002
// Date Last Modified: 07/14/2002
// Copyright (c) 2002 SofTech System Inc.
// ----------------------------------------------------------- //
// ------------- Program Description and Details ------------- //
// ----------------------------------------------------------- //
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
The CStudioFile class is intended for WinCE MFC applications because MFC for
WinCE did not implement the CStdioFile text file functions. You should
use the CStdioFile in Win32 programs.
This class implements both UNICODE and ASCII text file reads and writes. If the
format is UNICODE, it is assumed to contain 16-bit strings. Other UNICODE formats
cannot be read by WinCE programs. It is the caller's responsibility to know which format
the file is in.
Because of file buffering, programmers should not mix the CFile::Read and
CFile::Write with the ReadString methods in this class. Doing so will cause
unpredictable results.
CFile::typeText is stripped from the open flags when the Open method is called
to avoid the ASSERT in the base class. It is not necessary to include this
flag when using the CStudioFile class.
*/
Top
10 楼yujia120(永不停息)回复于 2006-06-02 14:52:30 得分 0
// ----------------------------------------------------------- //
/*
http://www.codeguru.com/cpp/w-p/ce/files/article.php/c3519/
Read and Write Text Files in WinCE
Environment: Windows CE 3.0
Introduction
The CStudioFile class is intended for WinCE MFC applications
because MFC for WinCE did not implement the CStdioFile text file functions.
You should use the CStdioFile in Win32 programs.
This class implements both UNICODE and ASCII text file reads and writes.
If the format is UNICODE, it is assumed to contain 16-bit strings.
Other UNICODE formats cannot be read by WinCE programs.
It is the caller's responsibility to know which format the file is in.
Because of file buffering, programmers should not mix the CFile::Read
and CFile::Write with the ReadString methods in this class.
Doing so will cause unpredictable results.
CFile::typeText is stripped from the open flags when the Open method is
called to avoid the ASSERT in the base class. It is not necessary
to include this flag when using the CStudioFile class.
Example 1, Illustrating How to Read a Text File Using CString
CStudioFile f;
CString s;
CString output;
f.Open(L"index.html",
CFile::modeRead | CFile::shareExclusive |
CFile::typeText);
while(f.ReadString(s,false))
{
output += s;
}
f.Close();
Example 2, Illustrating How to Read an ASCII-Formatted File
CStudioFile f;
char buf[255];
CString output;
f.Open(L"index.html",
CFile::modeRead | CFile::shareExclusive |
CFile::typeText);
while(f.ReadString(buf, sizeof(buf))
{
output += buf;
}
f.Close();
Example 3, Illustrating How to Read an UNICODE-Formatted File
CStudioFile f;
wchar_t buf[255];
CString output;
f.Open(L"index.html",
CFile::modeRead | CFile::shareExclusive |
CFile::typeText);
while(f.ReadString(buf, sizeof(buf))
{
output += buf;
}
f.Close();
*/
#include "stdafx.h"
#include "StudioFile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CStudioFile::CStudioFile()
{
m_nChars = 0;
m_nextout = 0;
memset(m_ibuf, 0, sizeof(m_ibuf));
}
BOOL CStudioFile::Open(LPCTSTR lpszFileName, UINT nOpenFlags,
CFileException* pError)
// Remove the CFile::typeText if it exists in nOpenFlags
// because this will cause an ASSERT in the base class.
{
if(nOpenFlags & CFile::typeText)
nOpenFlags ^= CFile::typeText;
return CFile::Open(lpszFileName,nOpenFlags,pError);
}
void CStudioFile::WriteString(wchar_t* lpsz)
// unbuffered write. Appends \r\n to the end of the string
{
ASSERT(lpsz);
if(lpsz != NULL)
Write(lpsz, wcslen(lpsz) * sizeof(TCHAR));
Write(L"\r\n", 2*sizeof(TCHAR));
}
void CStudioFile::WriteString(char* lpsz)
// unbuffered write. Appends \r\n to the end of the string
{
ASSERT(lpsz);
if(lpsz != NULL)
Write(lpsz, strlen(lpsz));
Write("\r\n", 2);
}
wchar_t* CStudioFile::ReadString(wchar_t* lpsz, UINT nMax)
// Reads text data into a buffer, up to a limit of nMax -1 characters,
// from the file associated with the CStudioFile object.
// Reading is stopped by the first newline character. If,
// in that case, fewer than nMax-1 characters have been read,
// a newline character ('\n') is stored in the buffer. A null character
// (慭0? is appended in either case.
{
wchar_t c;
for(UINT i = 0; i < nMax-1; i++)
{
// If the buffer is empty or all data has been
// returned, get more data from the file.
if(m_nextout == m_nChars)
{
m_nChars = Read(m_ibuf, _MAX_BUFSIZE * sizeof(wchar_t));
m_nChars /= sizeof(wchar_t);
m_nextout = 0;
}
// quit if end-of-file
if(m_nChars == 0) break;
c = m_ibuf[m_nextout++];
if(c == '\r' || c == '\n')
// End of string is detected, so append '\n' if
// lpsz is not full. and stop.
{
if(m_ibuf[m_nextout] == '\n' && (m_nextout < _MAX_BUFSIZE))
m_nextout++;
if(i < nMax-1)
lpsz[i++] = '\n';
break;
}
lpsz[i] = c;
}
// NULL terminate the string
lpsz[i] = 0;
return i == 0 ? NULL : lpsz;
}
char* CStudioFile::ReadString(char* lpsz, UINT nMax)
// Reads text data into a buffer, up to a limit of nMax -1 characters,
// from the file associated with the CStdioFile object.
// Reading is stopped by the first newline character. If,
// in that case, fewer than nMax-1 characters have been read,
// a newline character ('\n') is stored in the buffer. A null character
// (慭0? is appended in either case.
{
char c;
char *bufptr = (char *)m_ibuf;
int bufsize = _MAX_BUFSIZE * sizeof(wchar_t);
for(UINT i = 0; i < nMax-1; i++)
{
// If the buffer is empty or all data has been
// returned, get more data from the file.
if(m_nextout == m_nChars)
{
m_nChars = Read(m_ibuf, bufsize);
m_nextout = 0;
}
// quit if end-of-file
if(m_nChars == 0) break;
c = bufptr[m_nextout++];
if(c == '\r' || c == '\n')
// End of string is detected, so append '\n' if
// lpsz is not full. and stop.
{
if( (bufptr[m_nextout] == '\n') && (m_nextout < bufsize))
m_nextout++;
if(i < nMax-1)
lpsz[i++] = '\n';
break;
}
lpsz[i] = c;
}
// NULL terminate the string
lpsz[i] = 0;
return i == 0 ? NULL : lpsz;
}
BOOL CStudioFile::ReadString(CString& rString, bool bIsUnicode)
// Read a line into a CString object. The default is to use UNICODE file
// format. This function returns FALSE when end-of-file is reached, or
// TRUE is not at the end-of-file.
{
BOOL rval;
bool beof = false;
wchar_t buf[_MAX_BUFSIZE];
if(bIsUnicode)
{
// The file contains 16-bit UNICODE strings.
wchar_t* ptr = ReadString(buf,_MAX_BUFSIZE);
if(ptr == NULL)
rval = FALSE;
else
{
rString = buf;
rval = TRUE;
}
}
else
{
char* ptr = ReadString((char*)buf,_MAX_BUFSIZE * sizeof(wchar_t));
if(ptr == NULL)
rval = FALSE;
else
{
rString = (char*)buf;
rval = TRUE;
}
}
return rval;
}
// ----------------------------------------------------------- //
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //Top
11 楼Tycool(Xuco)回复于 2006-06-02 16:25:05 得分 0
看到楼上贴了那么长(还带那么长的开头版权),吐得不行,就不想参加讨论了。吐~Top
12 楼dsygr520yy(沙漠中的鱼)回复于 2006-06-08 10:11:20 得分 0
我也楼上的一样了。。。赫赫Top
13 楼yujia120(永不停息)回复于 2006-06-13 19:32:22 得分 0
http://www.codeguru.com/cpp/w-p/ce/files/article.php/c3519/
看这个就可以了Top




