要求使用CFile类读文本文件,CStdioFile不行!
每一行以回车结束,没有任何标记。
//本人试过这段代码但是无法打开文件。希望高手另赐方法
char *str;
str=(LPSTR)(LPCTSTR)FilePath;
FILE* pFile=fopen(str,"r");
while (!feof(pFile))
{
char cSql[2000];
memset(cSql,0,sizeof(cSql));
fgets(cSql,sizeof(cSql),pFile);
CString strTemp=cSql; //已经读出一行来
m_filename.Add(cSql);
}
fclose(pFile);
每一行以回车结束,没有任何标记。
//本人试过这段代码但是无法打开文件。希望高手另赐方法
char *str;
str=(LPSTR)(LPCTSTR)FilePath;
FILE* pFile=fopen(str,"r");
while (!feof(pFile))
{
char cSql[2000];
memset(cSql,0,sizeof(cSql));
fgets(cSql,sizeof(cSql),pFile);
CString strTemp=cSql; //已经读出一行来
m_filename.Add(cSql);
}
fclose(pFile);
#include <stdlib.h>
void main()
{
FILE * fp;
char s[256];
if((fp = fopen("c:\\comlog.txt", "r")) == NULL)
{
printf("Can't open file\n");
return;
} fgets(s, 256, fp); //第一行 printf("Line1 : %s\n", s);
//**********************************
//for (int i =0; i<n-1; i++)//n就是第几行了
for (int i =0; i<3; i++)
{
fseek(fp, 0, SEEK_CUR);
fgets(s, 256, fp);
printf("Line1 : %s\n", s); }
//***************************************88
fclose(fp);
}
FILE* file;
file=fopen("cicqserver.ini","r");
char inibuf[23];
memset(inibuf,0,23);
fscanf(file,"%s",inibuf);
CString serverip=inibuf;
fscanf(file,"%s",inibuf);
CString serverport=inibuf;
fclose(file);
{
CStdioFile timereadfile1;
if(timereadfile1.GetFileName()=="")
{CFileDialog dlg(true,NULL,NULL,NULL,"*.arictext|*.arictext|All files|*.*||",NULL);
if(!timereadfile1.Open(filename,CFile::modeRead))
{
MessageBox(NULL,"文件打开出错,请确保路径正确,并且有足够权限!","注意",0);}
}
CString strr;if(timereadfile1.ReadString(strr))
return strr;
else
return "";}
楼主可以参看一下这个代码!
CStdioFile file;
file.open(******);
cstring str;
while(file.read(str))
{
....
}
CString m_strData2 = m_strData;
m_strData2.Replace("Upload","translog"); HANDLE hFile;
HANDLE hAppend;
DWORD dwBytesRead, dwBytesWritten, dwPos;
BYTE buff[4096]; // Open the existing file. hFile = CreateFile(TEXT(m_strData), // open One.txt
GENERIC_READ, // open for reading
0, // do not share
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template if (hFile == INVALID_HANDLE_VALUE)
{
//printf("Could not open One.txt.");
return;
} // Open the existing file, or if the file does not exist,
// create a new file. hAppend = CreateFile(TEXT(m_strData2), // open Two.txt
GENERIC_WRITE, // open for writing
FILE_SHARE_READ, // allow multiple readers
NULL, // no security
OPEN_ALWAYS, // open or create
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template if (hAppend == INVALID_HANDLE_VALUE)
{
//printf("Could not open Two.txt.");
return;
} // Append the first file to the end of the second file.
// Lock the second file to prevent another process from
// accessing it while writing to it. Unlock the
// file when writing is finished. do
{
if (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL))
{
dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
LockFile(hAppend, dwPos, 0, dwBytesRead, 0);
WriteFile(hAppend, buff, dwBytesRead, &dwBytesWritten, NULL);
UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0);
}
} while (dwBytesRead == sizeof(buff)); // Close both files. CloseHandle(hFile);
CloseHandle(hAppend); remove(m_strData);}