我用CStdioFile讀取了一行檔案,然后想把讀取的那一行檔案以空格分開并分別把值賦給陣列,應該怎么做啊!!!
uj5u.com熱心網友回復:
讀取行放到字串陣列中,while判斷空格,然后在回圈中拷貝空格之前的字串到新的字串陣列中uj5u.com熱心網友回復:
就是不知道怎么把空格前的提取出來,能給個代碼嗎?謝謝
uj5u.com熱心網友回復:
CStdioFile 可以當成C標準庫檔案輸入輸出的封裝uj5u.com熱心網友回復:
無代碼,無真相!uj5u.com熱心網友回復:
-------------------------------------------------------------------------------------------------
CString filter;
filter="文本檔案(*.txt)|*.txt|PDF檔案(*.pdf)|*.pdf||";
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,filter);
if(dlg.DoModal()==IDOK)
{
CString str;
str=dlg.GetPathName();
MessageBox(str);
CStdioFile file;
file.Open(str,CFile::modeCreate|CFile::modeWrite);
CString strRead;
int pos=0;
int pos1;
number=0;
while(file.ReadString(strRead))
{
pos1=strRead.Find('\t',pos);
a=strRead.Left(pos1-pos);
pos=pos1;
pos1=strRead.Find('\t',pos);
b=strRead.Left(pos1-pos);
pos=pos1;
pos1=strRead.Find('\t',pos);
c=strRead.Left(pos1-pos);
pos=pos1;
pos1=strRead.Find('\t',pos);
d=strRead.Left(pos1-pos);
--------------------------------------------------------------------------------------------------------------
這樣可以嗎,先查找空格的位置,然后記錄,在把空格之前的取出來,然后再繼續
abcd是已定義的字串。
uj5u.com熱心網友回復:
-----------------------------------------------------------------------------------------------------------------
不好意思 不是\t 直接' '
uj5u.com熱心網友回復:
file.Open(str,CFile::modeCreate|CFile::modeWrite);這句有問題吧。
CFile::CFile
CFile( );
CFile( int hFile );
CFile( LPCTSTR lpszFileName, UINT nOpenFlags );
throw( CFileException );
Parameters
hFile
The handle of a file that is already open.
lpszFileName
A string that is the path to the desired file. The path can be relative or absolute.
nOpenFlags
Sharing and access mode. Specifies the action to take when opening the file. You can combine options listed below by using the bitwise-OR (|) operator. One access permission and one share option are required; the modeCreate and modeNoInherit modes are optional. The values are as follows:
CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length.
CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.
CFile::modeRead Opens the file for reading only.
CFile::modeReadWrite Opens the file for reading and writing.
CFile::modeWrite Opens the file for writing only.
CFile::modeNoInherit Prevents the file from being inherited by child processes.
CFile::shareDenyNone Opens the file without denying other processes read or write access to the file. Create fails if the file has been opened in compatibility mode by any other process.
CFile::shareDenyRead Opens the file and denies other processes read access to the file. Create fails if the file has been opened in compatibility mode or for read access by any other process.
CFile::shareDenyWrite Opens the file and denies other processes write access to the file. Create fails if the file has been opened in compatibility mode or for write access by any other process.
CFile::shareExclusive Opens the file with exclusive mode, denying other processes both read and write access to the file. Construction fails if the file has been opened in any other mode for read or write access, even by the current process.
CFile::shareCompat This flag is not available in 32 bit MFC. This flag maps to CFile::shareExclusive when used in CFile::Open.
CFile::typeText Sets text mode with special processing for carriage return–linefeed pairs (used in derived classes only).
CFile::typeBinary Sets binary mode (used in derived classes only).
Remarks
The default constructor does not open a file but rather sets m_hFile to CFile::hFileNull. Because this constructor does not throw an exception, it does not make sense to use TRY/CATCH logic. Use the Open member function, then test directly for exception conditions. For a discussion of exception-processing strategy, see the articleExceptions in Visual C++ Programmer's Guide.
The constructor with one argument creates a CFile object that corresponds to an existing operating-system file identified by hFile. No check is made on the access mode or file type. When the CFile object is destroyed, the operating-system file will not be closed. You must close the file yourself.
The constructor with two arguments creates a CFile object and opens the corresponding operating-system file with the given path. This constructor combines the functions of the first constructor and the Open member function. It throws an exception if there is an error while opening the file. Generally, this means that the error is unrecoverable and that the user should be alerted.
Example
//example for CFile::CFile
char* pFileName = "test.dat";
TRY
{
CFile f( pFileName, CFile::modeCreate | CFile::modeWrite );
}
CATCH( CFileException, e )
{
#ifdef _DEBUG
afxDump << "File could not be opened " << e->m_cause << "\n";
#endif
}
END_CATCH
CFile Overview | Class Members | Hierarchy Chart
uj5u.com熱心網友回復:
或許可以使用 strtok 分解字串uj5u.com熱心網友回復:
void StringSplit(const CString &str, CStringArray &arr, TCHAR chDelimitior)
{
int nStart = 0, nEnd = 0;
arr.RemoveAll();
while (nEnd < str.GetLength())
{
// determine the paragraph ("xxx,xxx,xxx;")
nEnd = str.Find(chDelimitior, nStart);
if( nEnd == -1 )
{
// reached the end of string
nEnd = str.GetLength();
}
CString s = str.Mid(nStart, nEnd - nStart);
if (!s.IsEmpty())
arr.Add(s);
nStart = nEnd + 1;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/132527.html
標籤:基礎類
上一篇:在MFC中 怎么 宣告這個 ZwCreateSection 函式呀?
下一篇:雙按鍵不能跳轉到執行函式
