积极答复者
文件读写操作

问题
答案
-
你可以在函数体中添加以下代码,实现你想要的功能。
Node nodes[3]; FILE *fp = fopen("file.txt", "rt"); for(int i=0;i<3;i++) { fscanf(fp, "%c\r\n", &nodes[i].data); fscanf(fp, "%d\r\n", &nodes[i].NO); if(feof(fp)) {break;} } fclose(fp);
Helen Zhao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已建议为答案 Helen Zhao 2011年11月3日 1:53
- 已标记为答案 Helen Zhao 2011年11月7日 2:53
-
int _tmain(int argc, _TCHAR* argv[]) { struct Node { char data; int NO; }; #define MAX_COUNT 3 Node nodes[MAX_COUNT]; try { CStdioFile file(_T("E:\\data.txt"), CFile::modeRead); CString strLine(_T("")); int nIndex = 0; int i = 0; while(file.ReadString(strLine)) { if(nIndex >= MAX_COUNT) break; if(++i % 2) { nodes[nIndex].data = strLine.GetAt(0); } else { nodes[nIndex].NO = _ttoi(strLine); nIndex++; } } file.Close(); } catch (CFileException* e) { e->ReportError(); e->Delete(); } return 0; }
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.- 已建议为答案 Helen Zhao 2011年11月3日 1:53
- 已标记为答案 Helen Zhao 2011年11月7日 2:53
全部回复
-
FILE *fp = fopen("data.txt", "rt");
while (!feof(fp))
{
Node node;
fscanf(fp, "%c\r\n", &node.data);
fscanf(fp, "%d\r\n", &node.NO);printf("Node.data = %c, node.NO = %d.\r\n", node.data, node.NO);
}
fclose(fp);- 已建议为答案 Jesse JiangModerator 2011年11月7日 2:24
-
你可以在函数体中添加以下代码,实现你想要的功能。
Node nodes[3]; FILE *fp = fopen("file.txt", "rt"); for(int i=0;i<3;i++) { fscanf(fp, "%c\r\n", &nodes[i].data); fscanf(fp, "%d\r\n", &nodes[i].NO); if(feof(fp)) {break;} } fclose(fp);
Helen Zhao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已建议为答案 Helen Zhao 2011年11月3日 1:53
- 已标记为答案 Helen Zhao 2011年11月7日 2:53
-
int _tmain(int argc, _TCHAR* argv[]) { struct Node { char data; int NO; }; #define MAX_COUNT 3 Node nodes[MAX_COUNT]; try { CStdioFile file(_T("E:\\data.txt"), CFile::modeRead); CString strLine(_T("")); int nIndex = 0; int i = 0; while(file.ReadString(strLine)) { if(nIndex >= MAX_COUNT) break; if(++i % 2) { nodes[nIndex].data = strLine.GetAt(0); } else { nodes[nIndex].NO = _ttoi(strLine); nIndex++; } } file.Close(); } catch (CFileException* e) { e->ReportError(); e->Delete(); } return 0; }
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.- 已建议为答案 Helen Zhao 2011年11月3日 1:53
- 已标记为答案 Helen Zhao 2011年11月7日 2:53