打开文件

来源:百度文库 编辑:神马文学网 时间:2024/06/03 14:15:23
//
#include "stdafx.h"
#include
#include
#include int main()
{
 FILE   *in;
 in = fopen("E:\\123.txt", "r");
 char line[256], buf[32];
 char *token = 0;
 while(fgets(line, 255, in))
 {
  token = strtok(line, "   ");
  while(token)
  { 
   printf("%s ", token);
   token = strtok(NULL, "   ");
  }
 }
 return 0;
}    char *strtok(char *s, char *delim);//s为要分解的字符串,delim为分隔符字符串。    首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。   strtok在s中查找包含在delim中的字符并用NULL('')来替换,直到找遍整个字符串。   char * p = strtok(s,";");   p = strtok(null,";");   在调用的过程中,字串s被改变了,这点是要注意的。
//***********************
#include "stdafx.h"
#include
#includeusing namespace std;int main()
{
 ifstream myFile;
 myFile.open("E:\\123.txt");
 char output[100];
 if (myFile.is_open())
 {
 while (!myFile.eof())
 {
    myFile >> output;
    cout< }
}
myFile.close();
return 0;
}
//***************************************#include "stdafx.h"
#include
#include "stdlib.h"
int main( )
{
int a[10][4]; //假定不超过10行,每行一定有4个元素
int i,j;
FILE *fp;fp=fopen("E:\\124.txt","r"); //打开文件
//fopen_s(&fp,"E:\\123.txt", "r"); if(!fp)exit(0);for(j=0;j<4;j++) //*假定有j行
for(i=0;i<4;i++)
fscanf(fp,"%d",&a[j][i]); //*读一个数据fclose(fp);//*显示运行结果
for(j=0;j<4;j++) //*假定有j行
{for(i=0;i<4;i++)
printf("%4d",a[j][i]);
printf("\n");
}
return 0;
}//*+++++++++++++++++++++++++++++++++++++++++++++++++
#include "stdafx.h"
#include "afx.h"
#include "string"
#include "fstream"
#include
using namespace std;int main()
{
float a[10];
int i=0,line_num;
fstream file;
file.open(_T("E:\\123.txt"),ios::in);
if (!file)
{
   cout<<"cannot open\n";
   return 0;
}
string str;
while(getline(file,str))
{
   file>>a[i];
   i++;
}
line_num=i+1;file.close();
cout<for (int j=0;j{
   cout<<"a["<}
return 0;
}
//*************************在一个按钮的单击响应函数里面写了如下代码:
void CFileTestDlg::OnTestfile()
{
// TODO: Add your control notification handler code here
CString strText = "";
CString szLine = "";

//打开文件
CStdioFile file;
if (!file.Open("test.txt",CFile::modeRead))
{
  AfxMessageBox("无法打开文件!");
  return;
}
//逐行读取字符串
while( file.ReadString( szLine ) )
{
  strText += szLine;
}
file.Close();
    AfxMessageBox(strText);
}
/////////////////////////////////////////////////////////////////////
结果:AfxMessageBox能够读出文本中的内容。