有TXT文件,格式如下:
1 2
3 4
5 6
...如何读取该文件并将每行的数据装换为CPoint类型的x、y坐标,然后再将这些CPoint存入一个数组?
如上述例子得到结果应为:
CPoint a[p1(1,2),p2(3,4),p3(5,6)...]急用,在线等...
1 2
3 4
5 6
...如何读取该文件并将每行的数据装换为CPoint类型的x、y坐标,然后再将这些CPoint存入一个数组?
如上述例子得到结果应为:
CPoint a[p1(1,2),p2(3,4),p3(5,6)...]急用,在线等...
使用CStdioFile读取,然后根据空格分解,解析,创建CPoint对象
array.Add
int i, nTemp, x, y;
CStdioFile fileRead, fileSave;
CString strRead, strTemp;
fileRead.Open(filename, CFile::modeRead);
while(fileRead.GetPosition()!=fileRead.GetLength())
{
fileRead.ReadString(strRead);
nTemp = strRead.Find(" ");
strTemp = strRead.Left(nTemp);
x = atoi(strTemp);
strTemp = strRead.Right(strRead.GetLength()-nTemp-1);
y = atoi(strTemp);
fileRead.Close();
}
大概就是这么个意思
数组的问题你应该能搞定吧
char *pszNum;
CPoint pt;
pszRow = strtok("\r\n", pszFileText);
while(NULL != pszRow)
{
pszNum = strchr(pszRow, ' ');
pt.x = atoi(pszRow);
pt.y = atoi(pszNum);
pszRow = strtok(NULL, pszFileText);
}
ifstream in(filename.c_str());
int x,y;
CPoint a[256];
int i=0;
while (!in.eof())
{
string line;
getline(in,line);
istringstream iss(line);
iss>>x>>y;
a[i] = CPoint(x,y);
i++;
}
HANDLE hFile;
DWORD dwBytesToRead, dwBytesRead;
TCHAR * pcBuffer;
hFile = ::CreateFile(TEXT("XXX.txt"),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
dwBytesToRead = ::GetFileSize(hFile, NULL);
pcBuffer = new TCHAR[dwBytesToRead];
if (pcBuffer)
{
if (::ReadFile(hFile, pcBuffer, dwBytesToRead, &dwBytesRead, NULL))
{
// 找空格再找回车和换行符,逐行提取
} delete [] pcBuffer;
} ::CloseHandle(hMapFile);
}
判断是不是'\n'
如果是就是一行
这就是Readline
const num = 20;
FILE* pf = fopen("data.txt", "r");
int x, y;
CPoint p[num];
for(int i=0; i<num; i++)
{
fscanf(pf, "%d %d", &x, &y);
p[i].x = x;
p[i].y = y;
}
fclose(pf);
希望对你有帮助
#include <string>std::vector <std::Point> pt;
Point _pt;
CString strRead, strTemp;CStdioFile fileRead;
if(!infile.Open(filename, CFile::modeRead))
{
AfxMessageBox("没有找到该文件!");
return;
} infile.SeekToBegin(); while(fileRead.ReadString(midFile))
{
nTemp = strRead.Find(" ");
strTemp = strRead.Left(nTemp);
x = atoi(strTemp);
_pt.x = x;
strTemp = strRead.Right(strRead.GetLength()-nTemp-1);
y = atoi(strTemp);
_pt.y = y;
pt.push_back(_pt);
}
}
#include <string> std::vector <std::Point> pt;
Point _pt;
CString strRead, strTemp;CStdioFile fileRead;
if(!fileRead.Open(filename, CFile::modeRead))
{
AfxMessageBox("没有找到该文件!");
return;
} fileRead.SeekToBegin(); while(fileRead.ReadString(strRead))
{
nTemp = strRead.Find(" ");
strTemp = strRead.Left(nTemp);
x = atoi(strTemp);
_pt.x = x;
strTemp = strRead.Right(strRead.GetLength()-nTemp-1);
y = atoi(strTemp);
_pt.y = y;
pt.push_back(_pt);
}
CFileException fileException;
CString pszFileName;//路径
if(myfile.Open(pszFileName ,CFile::typeText|CFile::modeCreate|CFile::modeReadWrite), & fileException)
{
myfile.SeekToBegin();
pointData.Add(new CPoint(x坐标, y坐标 ) );
}